Skip to main content

XPathExpr

Struct XPathExpr 

Source
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

Source

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();
Source

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();
Source

pub fn source(&self) -> &str

Get the original source expression.

Source

pub fn span(&self) -> SourceSpan

Get the source span of the expression.

Source

pub fn external_vars(&self) -> &[ExternalVar]

Get the external variables declared for this expression.

Source

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

Source

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);

Trait Implementations§

Source§

impl Clone for XPathExpr

Source§

fn clone(&self) -> XPathExpr

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for XPathExpr

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> MaybeSendSync for T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.