pub struct XPathExpr { /* private fields */ }Expand description
A compiled XPath expression that can be evaluated multiple times.
XPathExpr owns its AST and contains no lifetimes, so it can be stored
in structs, sent across threads (if using appropriate synchronization),
or cached for repeated evaluation.
§Compilation vs Evaluation
Compilation (compile() or compile_with_vars()) parses and binds the expression,
resolving function names, variable slots, and namespace prefixes. This is the
expensive step.
Evaluation (evaluator().run()) executes the compiled AST, which is much faster.
You can evaluate the same compiled expression many times with different variable
values or context nodes.
§External Variables
Use compile_with_vars() to declare variables that will be provided at evaluation time:
let names = NameTable::new();
let ctx = XPathContext::new(&names);
// Declare $x and $y as external variables
let expr = XPathExpr::compile_with_vars("$x + $y", &ctx, &["x", "y"]).unwrap();Implementations§
Source§impl XPathExpr
impl XPathExpr
Sourcepub fn compile(expr: &str, ctx: &XPathContext<'_>) -> Result<Self, XPathError>
pub fn compile(expr: &str, ctx: &XPathContext<'_>) -> Result<Self, XPathError>
Compile an XPath expression without external variables.
Use this when your expression doesn’t reference any variables, or when
all variables are provided by the expression itself (e.g., for $x in 1 to 10).
§Errors
Returns an error if:
- The expression has syntax errors
- A function is not found
- A variable is referenced but not defined
- A namespace prefix is not bound
§Example
let names = NameTable::new();
let ctx = XPathContext::new(&names);
let expr = XPathExpr::compile("1 + 2 * 3", &ctx).unwrap();Sourcepub fn compile_with_vars(
expr: &str,
ctx: &XPathContext<'_>,
vars: &[&str],
) -> Result<Self, XPathError>
pub fn compile_with_vars( expr: &str, ctx: &XPathContext<'_>, vars: &[&str], ) -> Result<Self, XPathError>
Compile an XPath expression with declared external variables.
External variables must be provided at evaluation time via with_variable().
Variable names should be provided without the $ prefix.
§Errors
Returns an error if:
- The expression has syntax errors
- A function is not found
- A variable is referenced that wasn’t declared
- A namespace prefix is not bound
§Example
let names = NameTable::new();
let ctx = XPathContext::new(&names);
// Compile expression that uses $x and $y
let expr = XPathExpr::compile_with_vars("$x + $y", &ctx, &["x", "y"]).unwrap();Sourcepub fn span(&self) -> SourceSpan
pub fn span(&self) -> SourceSpan
Get the source span of the expression.
Sourcepub fn external_vars(&self) -> &[ExternalVar]
pub fn external_vars(&self) -> &[ExternalVar]
Get the external variables declared for this expression.
Sourcepub fn arena(&self) -> &AstArena
pub fn arena(&self) -> &AstArena
Borrow the bound AST arena of this expression.
Useful for callers that want to inspect the compiled tree (e.g. CTA schema-time validation walking type expressions).
Sourcepub fn evaluator<'a, 'ctx>(
&'a self,
ctx: &'ctx XPathContext<'ctx>,
) -> XPathEvaluator<'a, 'ctx>
pub fn evaluator<'a, 'ctx>( &'a self, ctx: &'ctx XPathContext<'ctx>, ) -> XPathEvaluator<'a, 'ctx>
Create an evaluator for this expression.
The evaluator uses a builder pattern to set variables and other options before running the expression.
§Example
let names = NameTable::new();
let ctx = XPathContext::new(&names);
let expr = XPathExpr::compile_with_vars("$x * 2", &ctx, &["x"]).unwrap();
let result = expr.evaluator(&ctx)
.with_variable("x", 21).unwrap()
.run_number::<RoXmlNavigator<'static>>().unwrap();
assert_eq!(result, 42.0);