Skip to main content

Crate einvoice_xml

Crate einvoice_xml 

Source
Expand description

A streaming (pull) parser of XML 1.0 documents with namespaces, in pure Rust: no tree is built, so that well-formedness checking and validation can run in one pass over a document in memory that does not grow with the document.

Reader::next_event returns the Events of a document one by one. Names, and text and attribute values without references or characters to normalise, are slices of the input; the reader keeps only the open elements, the namespace declarations in scope, the attributes of the current start tag and the distinct namespace declarations seen (at most Limits::namespaces).

use einvoice_xml::{Event, Reader};

let mut reader = Reader::new(r#"<i:Invoice xmlns:i="urn:i" id="1">a &amp; b</i:Invoice>"#);
let mut events = Vec::new();
while let Some(event) = reader.next_event()? {
    match event {
        Event::Start(start) => {
            let name = start.name();
            assert_eq!((name.namespace, name.local), (Some("urn:i"), "Invoice"));
            assert_eq!(start.attribute(None, "id").map(|v| v.as_ref()), Some("1"));
            events.push("start");
        }
        Event::Text(text) => {
            assert_eq!(text.value, "a & b");
            events.push("text");
        }
        Event::End(_) => events.push("end"),
        _ => {}
    }
}
assert_eq!(events, ["start", "text", "end"]);
assert!(einvoice_xml::check("<a><b></a>").is_err());

§What is accepted

The text is UTF-8 (a &str; a byte order mark at the start is skipped; the encoding of the XML declaration is checked for syntax only). A document given as bytes is decoded to such a text first with decode (see Encodings). Supported: the XML declaration, elements and attributes, namespaces (prefixes, the default namespace and its undeclaration with xmlns="", the predefined xml prefix for attributes), comments, processing instructions, CDATA sections, character references and the five predefined entity references, line-end normalisation and attribute-value normalisation for the CDATA type. A document type declaration is refused (Error::DtdDetected), and so is a reference to any other entity.

The reader accepts the documents that the roxmltree crate (0.21, document type declarations refused) accepts and that are namespace-well-formed: on a document it refuses, the reader reports the same error, with the same message and position, unless the document contains one of the constructs below, which roxmltree accepts although XML 1.0 or Namespaces in XML 1.0 forbids them; each is refused with its own error:

The errors that only these constructs cause are raised once the checks that roxmltree makes on the same start tag, text, processing instruction or XML declaration have passed, so that a document with an error of both kinds in one tag gets the error that roxmltree reports.

As with roxmltree, an element name with the xml prefix is refused (Error::UnknownNamespace), although XML allows it.

The data differs from roxmltree’s, following XML 1.0, in three cases: a carriage return written next to a reference in character data is a line end like any other (roxmltree can keep it as a carriage return); comments and processing instructions have their line ends normalised; and <?xml followed by a tab, a line feed or a carriage return at the start of the text is an XML declaration (for roxmltree, a processing instruction).

§Encodings

decode finds the encoding of a document given as bytes as XML 1.0 (4.3.3 and Appendix F) says, from its byte order mark, its first bytes and its XML declaration, and decodes it to UTF-8 (without a copy when it is UTF-8 already). It reads the encodings that the reference parsers of e-invoice validation, libxml2 (xmllint) and the XML parser of Saxon-HE (Xerces, in the JDK), both read in the same way, under the names both accept (encoding_names): UTF-8, UTF-16 (with a byte order mark, or without one when the document starts with <?), US-ASCII, ISO-8859-1, ISO-8859-15 and windows-1252 (Encoding). A document in any other encoding is refused with Error::UnsupportedEncoding, one whose first bytes and declaration disagree with Error::EncodingMismatch, and bytes that are not a character of the encoding with Error::InvalidEncodedBytes, where both reference parsers, or one of them, refuse them too (the test tests/encoding.rs checks each case against what they do, as the example encoding-oracle records it). A position in a decoded text counts its characters, as for any text.

§Limits

Limits bounds the number of nodes, the depth, the number of attributes and the number of distinct namespace declarations; the defaults are roxmltree’s. Time is linear in the length of the text: duplicate attributes and prefixes are found through hash sets when an element has many.

Structs§

Attribute
An attribute (namespace declarations are not attributes).
Comment
A comment.
EndElement
The end of an element.
Limits
Bounds on what a document may contain, against hostile input. The defaults are those of roxmltree 0.21, so that a document is refused by a limit exactly when it refuses it.
Name
A name with its namespace.
ProcessingInstruction
A processing instruction.
Reader
A streaming parser of an XML 1.0 document with namespaces, read from a string.
StartElement
The start of an element.
Text
Character data or a CDATA section.
TextPos
A position in the text: 1-based row (lines are counted by line feeds) and 1-based column (counted in characters from the last line feed).

Enums§

Encoding
A character encoding that decode reads.
Error
Why a text is not a document the reader accepts.
Event
One event of a document, in document order. 'r is the borrow of the reader (names’ namespaces and attributes live in it until the next event); 'a is the text read.

Constants§

XMLNS_NAMESPACE
The namespace of the xmlns prefix.
XML_NAMESPACE
The namespace of the xml prefix.

Functions§

check
Checks that text is a well-formed document that the Reader accepts, with the default Limits.
decode
The text of a document given as bytes, in UTF-8, without its byte order mark: the bytes themselves when they are UTF-8 (no copy), decoded otherwise.
decode_owned
decode on a buffer, which becomes the text without a copy when it is UTF-8 (the byte order mark, if any, is removed in place).
encoding_names
Every name of an encoding read that decode accepts in an XML declaration, with the encoding (None for UTF-16, whose byte order is that of the byte order mark or of the first bytes).
encoding_of
The encoding decode reads bytes in, without decoding them: what the first bytes say, and, unless they say UTF-16, what the XML declaration names.