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 & 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). 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:
- a character reference to a surrogate or to a number above
0x10FFFF(Error::InvalidCharacterReference;roxmltreereads U+FFFD); - a processing instruction whose target is
xmlin any case or contains a colon, or that has no whitespace between its target and its data (Error::InvalidPiTarget,Error::PiWithoutWhitespace); - an XML declaration with a pseudo-attribute other than
version,encodingandstandaloneas written, a version other than1.and digits, an encoding that is not an encoding name, or astandaloneother thanyesandno(Error::InvalidXmlDeclaration); - a name that starts with a colon (
Error::InvalidQName); xmlns:p=""(Error::EmptyPrefixedNamespace), a declaration of thexmlnsprefix (Error::XmlnsPrefixDeclared), the default namespace or thexmlprefix declared twice on an element (Error::DuplicatedNamespaceDeclaration);- an attribute named
p:xmlns, whichroxmltreetakes for a declaration of the default namespace (Error::PrefixedXmlnsAttribute).
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).
§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
roxmltree0.21, so that a document is refused by a limit exactly when it refuses it. - Name
- A name with its namespace.
- Processing
Instruction - A processing instruction.
- Reader
- A streaming parser of an XML 1.0 document with namespaces, read from a string.
- Start
Element - 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§
- Error
- Why a text is not a document the reader accepts.
- Event
- One event of a document, in document order.
'ris the borrow of the reader (names’ namespaces and attributes live in it until the next event);'ais the text read.
Constants§
- XMLNS_
NAMESPACE - The namespace of the
xmlnsprefix. - XML_
NAMESPACE - The namespace of the
xmlprefix.