Skip to main content

Crate uppsala

Crate uppsala 

Source
Expand description

§Uppsala

A zero-dependency pure Rust XML library implementing the core XML stack from parsing through schema validation.

  • XML 1.0 (Fifth Edition) parsing and well-formedness checking
  • Namespaces in XML 1.0 (Third Edition) with prefix resolution and scoping
  • Arena-based DOM with tree mutation (insert, remove, replace nodes)
  • XPath 1.0 evaluation (all axes, core functions, predicates)
  • XML Schema (XSD) 1.1 validation (structures + datatypes)
  • XSD regex engine for pattern facets (custom NFA matcher)
  • Serialization with round-trip fidelity, pretty-printing, and streaming output
  • XmlWriter for imperative XML construction without a DOM
  • UTF-16 auto-detection (LE/BE with or without BOM)

§Quick Start

§Parsing and querying

use uppsala::{parse, NodeId, XPathEvaluator, XPathValue};

let xml = r#"<library>
  <book category="fiction"><title>Dune</title></book>
  <book category="science"><title>Cosmos</title></book>
</library>"#;

let mut doc = parse(xml).unwrap();

// DOM traversal
let titles = doc.get_elements_by_tag_name("title");
assert_eq!(titles.len(), 2);

// XPath queries
doc.prepare_xpath();
let eval = XPathEvaluator::new();
let root = doc.root();
if let Ok(XPathValue::NodeSet(nodes)) =
    eval.evaluate(&doc, root, "//book[@category='fiction']/title")
{
    assert_eq!(doc.text_content_deep(nodes[0]), "Dune");
}

§XSD validation

use uppsala::{parse, XsdValidator};

let schema_xml = r#"
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="age" type="xs:positiveInteger"/>
</xs:schema>"#;

let schema_doc = parse(schema_xml).unwrap();
let validator = XsdValidator::from_schema(&schema_doc).unwrap();

let doc = parse("<age>25</age>").unwrap();
assert!(validator.validate(&doc).is_empty());

let doc = parse("<age>-5</age>").unwrap();
assert!(!validator.validate(&doc).is_empty());

§Building XML

use uppsala::XmlWriter;

let mut w = XmlWriter::new();
w.write_declaration();
w.start_element("root", &[("xmlns", "urn:example")]);
w.text("hello");
w.end_element("root");

assert!(w.into_string().contains("<root"));

§Resource limits

To fail closed on hostile input, the parser enforces fixed resource caps by default (see parser for the associated constants and the Parser::with_* overrides):

  • Element nesting depth — capped at DEFAULT_MAX_DEPTH (128). Documents nested deeper are rejected with an error rather than risking a stack overflow.
  • Entity expansion byte budget — capped at DEFAULT_MAX_ENTITY_EXPANSION (1 MiB). Total bytes produced by entity expansion per parse cannot exceed this, defeating billion-laughs / quadratic-blowup amplification.
  • Entity expansion nesting depth — capped at DEFAULT_MAX_ENTITY_DEPTH (256), so a deep linear entity chain fails closed instead of overflowing the stack even before the byte budget is reached.

The depth and expansion-byte limits are configurable via builder methods on Parser (Parser::with_max_depth and Parser::with_max_entity_expansion) when a workload legitimately needs a different bound.

Re-exports§

pub use dom::Attribute;
pub use dom::ChildrenIter;
pub use dom::Document;
pub use dom::Element;
pub use dom::NodeId;
pub use dom::NodeKind;
pub use dom::ProcessingInstruction;
pub use dom::QName;
pub use dom::XmlDeclaration;
pub use dom::XmlWriteOptions;
pub use error::NamespaceError;
pub use error::ParseError;
pub use error::ValidationError;
pub use error::WellFormednessError;
pub use error::XPathError;
pub use error::XmlError;
pub use error::XmlResult;
pub use namespace::NamespaceResolver;
pub use parser::Parser;
pub use pull::NamespaceDeclaration;
pub use pull::PullEvent;
pub use pull::PullParser;
pub use writer::XmlWriter;
pub use xpath::XPathEvaluator;
pub use xpath::XPathValue;
pub use xsd::XsdValidator;
pub use xsd::XSI_NAMESPACE;
pub use xsd::XS_NAMESPACE;
pub use xsd_regex::XsdRegex;
pub use xslt::Stylesheet;
pub use xslt::DEFAULT_MAX_XSLT_DEPTH;
pub use xslt::XSLT_NAMESPACE;

Modules§

dom
Arena-based DOM representation of XML documents. DOM (Document Object Model) based on the XML Information Set specification.
error
Error types: XmlError, XmlResult, and per-domain error structs. Error types for the Uppsala XML library.
namespace
Namespace prefix resolution with scope stack. Namespaces in XML 1.0 (Third Edition) implementation.
parser
XML 1.0 (Fifth Edition) recursive-descent parser. XML 1.0 (Fifth Edition) parser with well-formedness checking.
pull
XML pull parser event stream. Pull-based XML event parser.
writer
Imperative XmlWriter for streaming XML construction. Imperative XML writer for constructing XML documents and fragments.
xpath
XPath 1.0 evaluation engine. XPath 1.0 evaluation engine.
xsd
XML Schema (XSD) validation. XML Schema (XSD) 1.1 validation.
xsd_regex
XSD regular expression engine for pattern facets. XSD Regular Expression engine.
xslt
XSLT 1.0 transformation engine. XSLT 1.0 transformation engine.

Functions§

parse
Parse an XML string into a Document.
parse_bytes
Parse XML from raw bytes, auto-detecting encoding (UTF-8, UTF-16 LE, UTF-16 BE).
transform
Transform an XML document with an XSLT 1.0 stylesheet, returning the serialized result.