Expand description
Node model and evaluator of the XPath 2.0 subset that the official e-invoicing validation stylesheets use, with the behaviour of the reference engine, Saxon-HE 12.10.
tree: immutable XML trees (Document,Node), parsed from text with every whitespace text node kept, or built with aTreeBuilder.compile: an expression parsed byeinvoice_xpathis resolved against aStaticContext(variables, host functions, whether the XSLT functions exist) into aCompiledExpr. Unknown functions and variables are errors here, not at evaluation. The static types Saxon infers are inferred too, where they change a result:xs:decimalof an argument statically typedxs:integerstrips its trailing zeros, as Saxon-HE 12.10’s up-casting converter does, and returns one statically typedxs:decimalunchanged (Saxon removes the cast);divof operands statically typedxs:decimal(or onexs:integer) is Saxon’s decimal division. The host gives the static types of its variables and functions asValueTypes, which infer them as Saxon does in a stylesheet from aselectexpression and anastype.CompiledExpr::evaluateandCompiledExpr::effective_boolean_valueevaluate it in aDynamicContext(theHostand theClock: the implicit timezone and the current date and time, given by the caller, so that the evaluator reads no clock, whichwasm32-unknown-unknowndoes not have) with aContext(focus and current item). Atomic values and their operations are those ofeinvoice_xdm.
Sequences are evaluated lazily, so that consumers that stop early (effective boolean
values, exists, general comparisons, some/every, [1]) raise only the errors
of the items they read, as Saxon does.
Functions: not, true, false, boolean, count, sum, normalize-space,
string-length, string, number, contains, starts-with, ends-with,
substring, substring-before, substring-after, concat, translate,
upper-case, round, abs, name, local-name, namespace-uri, last,
position, exists, document-uri (also with no argument, as in XPath 3.0), root,
base-uri, matches, replace, tokenize, string-to-codepoints,
codepoints-to-string, string-join, reverse, current-dateTime,
distinct-values, the constructors xs:decimal, xs:date, xs:integer,
xs:string, and in XSLT current, document and system-property (the last two
through the Host).
use einvoice_xpath::{NamespaceMap, XS_NAMESPACE, parse_expr};
use einvoice_xpath_eval::{Clock, Context, Document, DynamicContext, Item, NoHost, SimpleStaticContext, compile};
let doc = Document::parse("<i><l a='19.99'/><l a='0.01'/></i>", None)?;
let ns = NamespaceMap::new().bind("xs", XS_NAMESPACE);
let expr = compile(&parse_expr("sum(//l/xs:decimal(@a))", &ns)?, &SimpleStaticContext::new())?;
// UTC, and the current date and time read by the host: 2026-09-10T00:26:40Z.
let dc = DynamicContext::new(&NoHost, Clock::from_unix_millis(0, 1_789_000_000_000)?);
let result = expr.evaluate(&dc, &Context::for_item(Item::Node(doc.root())))?;
assert_eq!(result[0].string_value(), "20");Re-exports§
pub use tree::Document;pub use tree::DocumentCache;pub use tree::DocumentError;pub use tree::Node;pub use tree::NodeAxis;pub use tree::NodeKind;pub use tree::TreeBuilder;pub use tree::TreeView;pub use tree::file_uri;
Modules§
- tree
- The node model: immutable XML trees stored in an arena, one per document.
Structs§
- Argument
- One evaluation: the dynamic context and the current item.
An argument of a call to a host function (
Host::call_function): its value, or, for an argument Saxon evaluates lazily, what computes it when the host asks for it (Argument::evaluate), any number of times later during the call. - Clock
- The clock of an evaluation: the implicit timezone and the value of
fn:current-dateTime(). Both are inputs: the evaluator never reads a clock itself, and the current date and time stays the same for every evaluation made with the clock, as Saxon reads its clock once per transformation. - Compiled
Expr - An expression ready to be evaluated: functions and variables resolved against a static context. It holds no node and can be shared between threads.
- Context
- Where an expression is evaluated: its focus, and the item
current()returns. - Dynamic
Context - The dynamic context shared by evaluations: the host, the
Clock(implicit timezone and current date and time), a cache of compiled regular expressions (shared with other contexts when the host gives one,DynamicContext::with_regex_cache) and, if the host sets one, the bound of the call stack (DynamicContext::with_stack_limit). - Error
- A failed compilation or evaluation. One pointer wide: the results of the evaluator, one on every level of its recursion, stay small.
- Focus
- The focus of an evaluation: the context item, position and size.
- Function
Id - The identity of a function declared by the host (an
xsl:function). - NoHost
- A host for plain XPath: no variables, functions or documents.
- Node
Selection - The nodes of an expression read in place (
CompiledExpr::select_in_place): a document and node indices in document order. - Regex
Cache - The regular expressions compiled at evaluation (a pattern or flags that are not
literals), kept for the evaluations of any number of dynamic contexts, on any thread
(
DynamicContext::with_regex_cache): the transformations of a stylesheet compile each one once. At mostRegexCache::MAX_ENTRIESare kept; the others are compiled each time a dynamic context first needs them. - Selection
Storage - Storage for a
NodeSelection, reused from one selection to the next. - Simple
Static Context - A static context built by hand: namespaces, variables and functions declared one by one.
- Stack
Limit - A bound on the call stack: at most
maxbytes deeper than the position of the stack where it was taken (StackLimit::from_here). Stacks grow downwards on every supported target, the WebAssembly shadow stack included; the distance is taken either way. - Value
Type - What is statically known of the value of a variable, or of the result of a host function, as Saxon-HE 12.10 infers it in a stylesheet: its item type at each moment of the compilation and, when Saxon replaces the references to the variable by a literal, its value. A reference compiled with it has that type, and is that literal (the same value as the variable’s, which is then context-free).
- Variable
Id - The identity of a variable declared by the host, given back to it at evaluation.
- Variable
Info - A variable in scope.
Enums§
- Binding
Value - What a variable is bound to, for
ValueType::global_variableandValueType::local_variable. - Cardinality
- What is statically known of the number of items of a value.
- Error
Code - The error code of a failed compilation or evaluation.
- Item
- An item of a sequence.
- Limit
Exceeded - A limit that a document or a transformation exceeded: the input is refused rather than processed with an unbounded call stack.
Constants§
- DEFAULT_
MAX_ DEPTH - The maximum element depth of a parsed document, unless the caller sets another one: the document element is at depth 1, its child elements at depth 2, and so on.
Traits§
- Host
- What the host (the XSLT layer) provides at evaluation. The compiler only lets through references to variables and functions the static context declared, so a host is only asked for those.
- Static
Context - The static context of an expression (XPath 2.0 section 2.1.1), as far as the compiler needs it: names are already resolved by the parser.
Functions§
- compile
- Compiles an expression in a static context.
Type Aliases§
- Result
- The result of a compilation or evaluation.