Skip to main content

Module xpath

Module xpath 

Source
Expand description

XPath 1.0 expression parsing and evaluation.

For repeated queries against the same document, create an XPathContext once — it builds the document index on construction and amortises that cost across every subsequent eval_* call. The free functions (xpath_eval, xpath_str, etc.) are one-shot convenience wrappers that build a fresh context each time; use them when you only need a single result.

§Example — one-shot helpers

use sup_xml_core::{parse_str, xpath_str, xpath_count, xpath_bool, ParseOptions};

let doc = parse_str(r#"<catalog><book id="1"/><book id="2"/></catalog>"#, &ParseOptions::default()).unwrap();

assert_eq!(xpath_count(&doc, "/catalog/book").unwrap(), 2);
assert_eq!(xpath_str(&doc, "name(/catalog)").unwrap(), "catalog");
assert!(xpath_bool(&doc, "/catalog/book[@id='1']").unwrap());

§Example — reusable context

use sup_xml_core::{parse_str, XPathContext, ParseOptions};

let doc = parse_str(r#"<catalog><book id="1"/><book id="2"/></catalog>"#, &ParseOptions::default()).unwrap();
let ctx = XPathContext::new(&doc);

assert_eq!(ctx.eval_count("/catalog/book").unwrap(), 2);
assert!(ctx.eval_bool("/catalog/book[@id='1']").unwrap());

Re-exports§

pub use ast::Axis;
pub use ast::Expr;
pub use ast::LocationPath;
pub use ast::NodeTest;
pub use ast::Step;
pub use ast::FunctionSig;
pub use ast::ItemType;
pub use ast::Occurrence;
pub use ast::SequenceType;
pub use context::DocIndex;
pub use context::INodeKind;
pub use context::is_synthetic_id;
pub use eval::Value as XPathValue;
pub use eval::compile_xpath_2_0_regex;

Modules§

ast
context
Arena-tree counterpart to super::context::DocIndex.
eval
exslt
EXSLT extension function families — the math, date, str, and set namespaces every real-world XSLT stylesheet (and many bare XPath consumers) reach for.
pattern
A small subset of XPath 1.0 sized for fast per-node matching.
rtf
XSLT result-tree-fragment storage for XPath navigation.

Structs§

XPathBindingsBuilder
Builder + XPathBindings impl for caller-supplied namespaces, variables, and extension functions. Cheap to clone (functions are stored in Arc). Pass &builder (or any &dyn XPathBindings) to super::XPathContext::eval_with.
XPathContext
Reusable XPath context for an arena-allocated ArenaDocument.
XPathOptions
Knobs for the XPath 1.0 lexer + evaluator. Default is strict XPath 1.0 spec conformance; flipping libxml2_compatible relaxes three points where libxml2 historically deviates from the spec:

Enums§

XPathNodeKind
Simple, tree-agnostic enum. Used by eval for node-test matching. The per-tree INodeKind / INodeKind types map onto this via DocIndexLike::kind.

Constants§

MAX_PREDICATE_NESTING_DEPTH
Maximum predicate-nesting depth accepted by parse_xpath_with.

Traits§

DocIndexLike
Methods the XPath evaluator needs from any backing tree representation.

Functions§

parse_sequence_type_str
Parse a SequenceType from its lexical form ("xs:long", "element(e)", "function(*)?", …). Used to reconstruct a user function’s declared signature for function-subtyping checks. Returns None on a lex/parse error or trailing input.
parse_xpath
Parse an XPath 1.0 expression string into its AST representation using the default strict options. See parse_xpath_with to opt into libxml2-compatible lexing.
parse_xpath_with
Parse an XPath expression with explicit XPathOptions. Today the only knob that affects parsing is libxml2_compatible, which makes the lexer accept exponent-notation in number literals.
xpath_bool
xpath_count
xpath_eval
One-shot XPath evaluation against an arena document. Builds a fresh index per call — use XPathContext to amortise.
xpath_num
xpath_str
xpath_strings

Type Aliases§

NodeId
Index into a flat per-document node table. Zero is the synthetic Document node; every other index points to the corresponding entry in the per-tree implementation’s nodes vector.