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"));

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 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;

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.
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.

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).