Skip to main content

Crate oxml

Crate oxml 

Source
Expand description

§oxml

A pure Rust XML toolkit. Zero unsafe code. Parsing, an ergonomic tree, and XPath 1.0.

§Why this exists

Rust’s XML ecosystem is strong at one end and empty at the other. quick-xml and roxmltree parse quickly; nothing maintained offers what lxml gives Python. The only XPath crate, sxd-xpath, has not shipped a release since 2018, and XSLT and XSD validation have no pure-Rust implementation at all.

oxml closes the query gap first, because that is the one people actually hit.

§Quick Start

use oxml::{parse, XPath};

let doc = parse(r#"
    <library>
        <book lang="en"><title>Dune</title></book>
        <book lang="fr"><title>Germinal</title></book>
    </library>
"#).unwrap();

let titles = XPath::compile("//book[@lang='en']/title").unwrap();
let found = titles.evaluate(&doc);

assert_eq!(found.to_str(&doc), "Dune");

§Walking the tree directly

XPath is optional. The tree stands on its own:

use oxml::parse;

let doc = parse("<a><b id='1'>text</b></a>")?;
let root = doc.root_element().expect("a root element");

assert_eq!(doc.element_name(root).unwrap().local, "a");

let b = doc.children(root)[0];
assert_eq!(doc.attribute(b, "id"), Some("1"));
assert_eq!(doc.text(b), "text");

§Design

  • Zero unsafe#![forbid(unsafe_code)], enforced at compile time. The tree is an arena of index-addressed nodes, so parent links cost no Rc, no RefCell, and no raw pointers.

  • No entity expansion — only the five predefined entities and numeric character references are resolved. External and custom entities are not, which forecloses XXE and billion-laughs by construction rather than by configuration. A parser that cannot expand them cannot be talked into leaking a file.

  • Namespace-correct — names compare by URI and local part, never by prefix. An unprefixed element takes the default namespace; an unprefixed attribute is in no namespace. That asymmetry is the classic source of namespace bugs, so it is explicit in the parser rather than assumed.

§Feature flags

  • std (default) — standard library integration, including std::error::Error.
  • xpath (default) — the XPath engine. Turn it off if you only need to parse.

Re-exports§

pub use error::Error;
pub use error::ErrorKind;
pub use error::Result;
pub use tree::Attribute;
pub use tree::Document;
pub use tree::ExpandedName;
pub use tree::NodeId;
pub use tree::NodeKind;
pub use xpath::XPath;xpath
pub use xpath::XPathError;xpath

Modules§

error
Errors, with source positions.
tree
The document tree.
xpathxpath
XPath 1.0.

Functions§

parse
Parse an XML document.