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.
13//! * [`CompiledExpr::evaluate`] and [`CompiledExpr::effective_boolean_value`] evaluate it
14//! in a [`DynamicContext`] (the [`Host`] and the [`Clock`]: the implicit timezone and
15//! the current date and time, given by the caller, so that the evaluator reads no clock,
16//! which `wasm32-unknown-unknown` does not have) with a [`Context`] (focus and current
17//! item). Atomic values and their operations are those of [`einvoice_xdm`].
18//!
19//! Sequences are evaluated lazily, so that consumers that stop early (effective boolean
20//! values, `exists`, general comparisons, `some`/`every`, `[1]`) raise only the errors
21//! of the items they read, as Saxon does.
22//!
23//! Functions: `not`, `true`, `false`, `boolean`, `count`, `sum`, `normalize-space`,
24//! `string-length`, `string`, `number`, `contains`, `starts-with`, `ends-with`,
25//! `substring`, `substring-before`, `substring-after`, `concat`, `translate`,
26//! `upper-case`, `round`, `abs`, `name`, `local-name`, `namespace-uri`, `last`,
27//! `position`, `exists`, `document-uri` (also with no argument, as in XPath 3.0), `root`,
28//! `base-uri`, `matches`, `replace`, `tokenize`, `string-to-codepoints`,
29//! `codepoints-to-string`, `string-join`, `reverse`, `current-dateTime`,
30//! `distinct-values`, the constructors `xs:decimal`, `xs:date`, `xs:integer`,
31//! `xs:string`, and in XSLT `current`, `document` and `system-property` (the last two
32//! through the [`Host`]).
33//!
34//! ```
35//! use einvoice_xpath::{NamespaceMap, XS_NAMESPACE, parse_expr};
36//! use einvoice_xpath_eval::{Clock, Context, Document, DynamicContext, Item, NoHost, SimpleStaticContext, compile};
37//!
38//! let doc = Document::parse("<i><l a='19.99'/><l a='0.01'/></i>", None)?;
39//! let ns = NamespaceMap::new().bind("xs", XS_NAMESPACE);
40//! let expr = compile(&parse_expr("sum(//l/xs:decimal(@a))", &ns)?, &SimpleStaticContext::new())?;
41//! // UTC, and the current date and time read by the host: 2026-09-10T00:26:40Z.
42//! let dc = DynamicContext::new(&NoHost, Clock::from_unix_millis(0, 1_789_000_000_000)?);
43//! let result = expr.evaluate(&dc, &Context::for_item(Item::Node(doc.root())))?;
44//! assert_eq!(result[0].string_value(), "20");
45//! # Ok::<(), Box<dyn std::error::Error>>(())
46//! ```
47
48mod compile;
49mod context;
50mod error;
51mod eval;
52mod functions;
53mod item;
54mod library;
55pub mod tree;
56
57pub use compile::{CompiledExpr, compile};
58pub use context::{
59 Cardinality, Clock, Context, DynamicContext, Focus, FunctionId, Host, NoHost,
60 SimpleStaticContext, StaticContext, VariableId, VariableInfo,
61};
62pub use error::{Error, ErrorCode, Result};
63pub use item::Item;
64pub use tree::{Document, DocumentError, Node, NodeAxis, NodeKind, TreeBuilder, file_uri};