Skip to main content

einvoice_xpath_eval/
lib.rs

1//! Node model and evaluator of the XPath 2.0 subset that the official e-invoicing
2//! validation stylesheets use, with the behaviour of the reference engine, Saxon-HE
3//! 12.10.
4//!
5//! * [`tree`]: immutable XML trees ([`Document`], [`Node`]), parsed from text with every
6//!   whitespace text node kept, or built with a [`TreeBuilder`].
7//! * [`compile`]: an expression parsed by [`einvoice_xpath`] is resolved against a
8//!   [`StaticContext`] (variables, host functions, whether the XSLT functions exist) into
9//!   a [`CompiledExpr`]. Unknown functions and variables are errors here, not at
10//!   evaluation. The static types Saxon infers are inferred too, where they change a
11//!   result: `xs:decimal` of an argument statically typed `xs:integer` strips its
12//!   trailing zeros, as Saxon-HE 12.10's up-casting converter does, and returns one
13//!   statically typed `xs:decimal` unchanged (Saxon removes the cast); `div` of operands
14//!   statically typed `xs:decimal` (or one `xs:integer`) is Saxon's decimal division. The
15//!   host gives the static types of its variables and functions as [`ValueType`]s, which
16//!   infer them as Saxon does in a stylesheet from a `select` expression and an `as`
17//!   type.
18//! * [`CompiledExpr::evaluate`] and [`CompiledExpr::effective_boolean_value`] evaluate it
19//!   in a [`DynamicContext`] (the [`Host`] and the [`Clock`]: the implicit timezone and
20//!   the current date and time, given by the caller, so that the evaluator reads no clock,
21//!   which `wasm32-unknown-unknown` does not have) with a [`Context`] (focus and current
22//!   item). Atomic values and their operations are those of [`einvoice_xdm`].
23//!
24//! Sequences are evaluated lazily, so that consumers that stop early (effective boolean
25//! values, `exists`, general comparisons, `some`/`every`, `[1]`) raise only the errors
26//! of the items they read, as Saxon does.
27//!
28//! Functions: `not`, `true`, `false`, `boolean`, `count`, `sum`, `normalize-space`,
29//! `string-length`, `string`, `number`, `contains`, `starts-with`, `ends-with`,
30//! `substring`, `substring-before`, `substring-after`, `concat`, `translate`,
31//! `upper-case`, `round`, `abs`, `name`, `local-name`, `namespace-uri`, `last`,
32//! `position`, `exists`, `document-uri` (also with no argument, as in XPath 3.0), `root`,
33//! `base-uri`, `matches`, `replace`, `tokenize`, `string-to-codepoints`,
34//! `codepoints-to-string`, `string-join`, `reverse`, `current-dateTime`,
35//! `distinct-values`, the constructors `xs:decimal`, `xs:date`, `xs:integer`,
36//! `xs:string`, and in XSLT `current`, `document` and `system-property` (the last two
37//! through the [`Host`]).
38//!
39//! ```
40//! use einvoice_xpath::{NamespaceMap, XS_NAMESPACE, parse_expr};
41//! use einvoice_xpath_eval::{Clock, Context, Document, DynamicContext, Item, NoHost, SimpleStaticContext, compile};
42//!
43//! let doc = Document::parse("<i><l a='19.99'/><l a='0.01'/></i>", None)?;
44//! let ns = NamespaceMap::new().bind("xs", XS_NAMESPACE);
45//! let expr = compile(&parse_expr("sum(//l/xs:decimal(@a))", &ns)?, &SimpleStaticContext::new())?;
46//! // UTC, and the current date and time read by the host: 2026-09-10T00:26:40Z.
47//! let dc = DynamicContext::new(&NoHost, Clock::from_unix_millis(0, 1_789_000_000_000)?);
48//! let result = expr.evaluate(&dc, &Context::for_item(Item::Node(doc.root())))?;
49//! assert_eq!(result[0].string_value(), "20");
50//! # Ok::<(), Box<dyn std::error::Error>>(())
51//! ```
52
53mod compile;
54mod context;
55mod error;
56mod eval;
57mod functions;
58mod item;
59mod library;
60mod limit;
61pub mod tree;
62
63pub use compile::{BindingValue, CompiledExpr, ValueType, compile};
64pub use context::{
65    Cardinality, Clock, Context, DynamicContext, Focus, FunctionId, Host, NoHost,
66    SimpleStaticContext, StaticContext, VariableId, VariableInfo,
67};
68pub use error::{Error, ErrorCode, Result};
69pub use item::Item;
70pub use limit::{DEFAULT_MAX_DEPTH, LimitExceeded};
71pub use tree::{Document, DocumentError, Node, NodeAxis, NodeKind, TreeBuilder, file_uri};