Skip to main content

Crate parsec_xml

Crate parsec_xml 

Source
Expand description

XML for Parsec: XML 1.0 (Fifth Edition) and Namespaces in XML 1.0 (Third Edition), parsed to a tree with namespace-resolved names and a span on every node and attribute.

let feed = r#"<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
  <entry><title>Ships &amp; ports</title></entry>
</feed>"#;
let doc = parsec_xml::parse(feed).unwrap();
let root = doc.root_element();
let name = doc.name(root).unwrap();
assert_eq!((name.local, name.namespace), ("feed", "http://www.w3.org/2005/Atom"));
assert_eq!(doc.xml_lang(root), Some("en"));
assert_eq!(doc.text(root).trim(), "Ships & ports");

XML is a trust boundary, and the parser treats it as one:

  • It doesn’t validate, and it never fetches or opens anything. The external subset and external entities are left unread, and the document’s warnings say so, as the spec asks of a processor that doesn’t read them.
  • Entities the internal subset declares are expanded, no deeper than Options::max_entity_depth and to no more text in all than Options::entity_allowance plus Options::entity_ratio bytes per byte of input, so a billion laughs or a quadratic blowup stops with an error. An attribute default counts each time an element gets it, so a big default on many elements stops the same way.
  • Elements nest no deeper than Limits::max_depth: 2,048 unless you say otherwise. The parser keeps open elements on a stack of its own, so even a million levels with no limit cost time in proportion and no recursion.
  • Anything that isn’t well-formed is an Error, fatal as the spec says, with a code, a span, and the line and column.

§XPath

With the xpath feature (parsec-xml = { version = "0.3", features = ["xpath"] }), a Document is a tree xpath can query, and parsec_xml::xpath is parsec-xpath re-exported. No HTML parser comes with it. Names compare exactly, a query’s prefixes are bound by the namespaces it’s run with, xmlns and xmlns:* aren’t attributes, id() finds xml:id and attributes the internal subset declares an ID, and lang() reads xml:lang. The namespace axis has the namespaces in scope at each element, from its xmlns attributes and its ancestors’.

use parsec_xml::xpath::{Doc, Index, Item, NoExtensions, Value, XPath};

let doc = parsec_xml::parse(r#"<feed xmlns="http://www.w3.org/2005/Atom">
  <entry><title>Ships</title></entry><entry><title>Ports</title></entry>
</feed>"#).unwrap();
// Build the index once per document; every query reads it.
let index = Index::build(&doc, &[0]);
let atom = [("a".to_string(), "http://www.w3.org/2005/Atom".to_string())];
let query = XPath::new("//a:entry[2]/a:title").unwrap();
let Ok(Value::Nodes(hits)) =
    query.evaluate(Doc::new(&doc, &index), 0, &[], &atom, &NoExtensions)
else {
    panic!("a node-set")
};
let Item::Node(title) = hits[0] else { panic!("a node") };
let title = parsec_xml::NodeId::from_index(title as usize);
assert_eq!(doc.text(title), "Ports");

Modules§

xpath
XPath 1.0 over any document tree that implements Tree, written from the W3C recommendation: every axis, node test and function of the core library, with the number, string and boolean rules of sections 3 and 4.

Structs§

Attribute
An attribute of an element.
Diagnostic
Something a parser has to say about its input: a stable code to match on (like unterminated-string), where, and a sentence for people. With a 'static message and no labels, making one allocates nothing.
Doctype
What the <!DOCTYPE> said.
Document
A parsed XML document.
Encoding
An encoding as defined in the Encoding Standard.
EntityDecl
An entity the internal subset declared.
Error
Why a document isn’t well-formed XML, or couldn’t be parsed within its limits.
Limits
The most a parse may cost. None is no limit.
LineCol
A line and a column, both counted from 1.
LineIndex
Where each line starts in a text, for turning byte offsets into lines and columns, in characters or in UTF-16 code units as editors and the language server protocol count them.
Name
An element’s or attribute’s name: as written, and resolved.
NodeId
A node of a Document.
Notation
A notation the internal subset declared.
Options
How to parse.
Parsed
A document parsed from bytes, and the text it was decoded to.
Span
A byte range in the input, start..end.
XmlDecl
The XML declaration, <?xml version="1.0"?>.

Enums§

NodeKind
What a node is.
Severity
How bad a Diagnostic is.

Constants§

DEFAULT_MAX_DEPTH
How elements may nest unless you say otherwise: lxml’s limit with huge_tree, deeper than any document we know of that isn’t an attack.
ENTITY_DEPTH_CEILING
However deep Options::max_entity_depth says entities may nest, they nest no deeper than this, so expanding them can’t run the parser’s own stack out.
XMLNS_NS
The namespace of xmlns and xmlns:* attributes.
XML_NS
The namespace the xml prefix is bound to, always.

Functions§

parse
Parses a document with the default Options.
parse_bytes
Parses a document given as bytes.
parse_with
Parses a document. A byte order mark at the start is skipped; an encoding the XML declaration names is ignored, since the text is decoded already.