Skip to main content

Crate einvoice_xpath_eval

Crate einvoice_xpath_eval 

Source
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 a TreeBuilder.
  • compile: an expression parsed by einvoice_xpath is resolved against a StaticContext (variables, host functions, whether the XSLT functions exist) into a CompiledExpr. Unknown functions and variables are errors here, not at evaluation. The static types Saxon infers are inferred too, where they change a result: xs:decimal of an argument statically typed xs:integer strips its trailing zeros, as Saxon-HE 12.10’s up-casting converter does.
  • CompiledExpr::evaluate and CompiledExpr::effective_boolean_value evaluate it in a DynamicContext (the Host and the Clock: the implicit timezone and the current date and time, given by the caller, so that the evaluator reads no clock, which wasm32-unknown-unknown does not have) with a Context (focus and current item). Atomic values and their operations are those of einvoice_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::DocumentError;
pub use tree::Node;
pub use tree::NodeAxis;
pub use tree::NodeKind;
pub use tree::TreeBuilder;
pub use tree::file_uri;

Modules§

tree
The node model: immutable XML trees stored in an arena, one per document.

Structs§

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.
CompiledExpr
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.
DynamicContext
The dynamic context shared by evaluations: the host, the Clock (implicit timezone and current date and time) and a cache of compiled regular expressions.
Error
A failed compilation or evaluation.
Focus
The focus of an evaluation: the context item, position and size.
FunctionId
The identity of a function declared by the host (an xsl:function).
NoHost
A host for plain XPath: no variables, functions or documents.
SimpleStaticContext
A static context built by hand: namespaces, variables and functions declared one by one.
VariableId
The identity of a variable declared by the host, given back to it at evaluation.
VariableInfo
A variable in scope.

Enums§

Cardinality
What is statically known of the number of items of a value.
ErrorCode
The error code of a failed compilation or evaluation.
Item
An item of a sequence.

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