Skip to main content

XPathContext

Struct XPathContext 

Source
pub struct XPathContext {
Show 15 fields pub document: *mut _xmlDoc, pub context_node: *mut _xmlNode, pub context_position: i32, pub context_size: i32, pub variables: HashMap<String, XPathValue>, pub namespaces: HashMap<String, String>, pub functions: HashMap<String, XPathFunction>, pub error: Option<String>, pub proximity_position: i32, pub context_list: Vec<*mut _xmlNode>, pub recursion_depth: u32, pub var_lookup_func: Option<VarLookupFunc>, pub var_lookup_data: *mut c_void, pub func_lookup_func: Option<FuncLookupFunc>, pub func_lookup_data: *mut c_void,
}
Expand description

XPath 1.0 evaluation context.

Carries all state required to evaluate an XPath expression:

  • Document and node — the current XML document and context node.
  • Context position/size — for position() and last().
  • Variable bindings — in-scope XPath variables.
  • Namespace bindings — prefix-to-URI mappings.
  • Extension functions — registered extension functions.
  • Recursion guard — depth counter to prevent infinite recursion.
  • C callbacks — hooks for variable and function lookup from the C ABI.

§Lifetime / Safety

The context borrows raw pointers to the document and nodes. It is the caller’s responsibility to ensure those pointers remain valid for the duration of evaluation. The context does not own the document tree.

Fields§

§document: *mut _xmlDoc

The current XML document.

§context_node: *mut _xmlNode

The current context node.

§context_position: i32

Position of the context node within the context list (1-based).

§context_size: i32

Size of the context list.

§variables: HashMap<String, XPathValue>

Bound variables (name → value).

§namespaces: HashMap<String, String>

Namespace bindings (prefix → URI).

§functions: HashMap<String, XPathFunction>

Registered extension functions (name → function).

§error: Option<String>

Last error message, if any.

§proximity_position: i32

Current proximity position (for last() / position()).

§context_list: Vec<*mut _xmlNode>

The context list for position() / last().

§recursion_depth: u32

Recursion depth counter (to prevent infinite recursion).

§var_lookup_func: Option<VarLookupFunc>

C callback for variable lookup.

§var_lookup_data: *mut c_void

Opaque data pointer passed to var_lookup_func.

§func_lookup_func: Option<FuncLookupFunc>

C callback for function lookup.

§func_lookup_data: *mut c_void

Opaque data pointer passed to func_lookup_func.

Implementations§

Source§

impl XPathContext

Source

pub fn new(doc: *mut _xmlDoc) -> Self

Create a new XPath evaluation context for the given document.

The context is initialised with:

  • The document pointer set to doc.
  • No context node (null).
  • Context position = 1, context size = 1 (defaults per XPath 1.0).
  • Empty variable, namespace, and function tables.
  • No error.
  • Proximity position = 1.
  • Empty context list.
  • Recursion depth = 0.
  • No C callbacks registered.
  • Callback data pointers set to null.
Source

pub fn set_context_node(&mut self, node: *mut _xmlNode)

Set the context node and update context position / size.

If node is non-null, the context list is set to a single-element list containing only that node, and both context_position and context_size are set to 1.

If node is null, the context list is cleared and both context_position and context_size are set to 1.

Source

pub fn set_context_list(&mut self, nodes: Vec<*mut _xmlNode>)

Set the context list for position() / last().

Updates context_list, context_size, and resets context_position and proximity_position to 1.

The context node is not changed by this call; use set_context_node to update it.

Source

pub fn resolve_variable(&self, name: &str) -> Option<XPathValue>

Look up a variable by name.

Checks the local variables map first. If the variable is not found there, and a var_lookup_func callback is registered, the callback is invoked with the variable name and its namespace (currently passed as null since our Rust-side variables have no namespace component).

Returns None if the variable is not bound.

§Note

When the C callback path is used, the returned _xmlXPathObject is converted into an XPathValue. Currently this path is a placeholder; a full implementation would call into xmlXPathObject conversion routines.

Source

pub fn resolve_namespace(&self, prefix: &str) -> Option<String>

Look up a namespace URI by prefix.

Checks the local namespaces map first. If the prefix is not found there, it falls back to scanning the namespace definitions on the context node (nsDef chain) and its ancestors.

Returns None if the prefix is not bound.

Source

pub fn lookup_function(&self, name: &str) -> Option<XPathFunction>

Look up a registered extension function by name.

Checks the local functions map first. If not found, and a func_lookup_func callback is registered, the callback is invoked.

Returns None if no such function is registered.

Source

pub fn register_function(&mut self, name: &str, func: XPathFunction)

Register an extension function.

The function is stored in the local functions map under name. It will be found by lookup_function before any C callback is consulted.

Source

pub fn register_variable(&mut self, name: &str, value: XPathValue)

Register a variable binding.

The variable is stored in the local variables map under name. It will be found by resolve_variable before any C callback is consulted.

Source

pub fn register_namespace(&mut self, prefix: &str, uri: &str)

Register a namespace binding.

Maps prefix to uri in the local namespaces map. An empty prefix registers the default namespace.

Source

pub fn set_error(&mut self, msg: &str)

Record an error message.

Overwrites any previously recorded error. Use clear_error to reset.

Source

pub fn clear_error(&mut self)

Clear any recorded error.

Source

pub fn push_recursion(&mut self) -> Result<(), String>

Push onto the recursion stack.

Increments recursion_depth. If the depth exceeds a reasonable limit (currently 1000), returns Err with an overflow message.

Callers should invoke this before recursing into expression evaluation and call pop_recursion after returning.

Source

pub fn pop_recursion(&mut self)

Pop from the recursion stack.

Decrements recursion_depth. Must be called after a corresponding push_recursion.

§Panics

Panics if recursion_depth is already 0 (indicating unbalanced push/pop calls).

Source

pub fn has_context_node(&self) -> bool

Returns true if a context node is set (non-null).

Source

pub fn reset(&mut self)

Reset the context to its initial state, keeping the document pointer.

Clears the context node, context list, error, and recursion depth. Variable, namespace, and function bindings are preserved.

Source

pub fn position(&self) -> i32

Returns the current proximity position (1-based).

Equivalent to the XPath position() function.

Source

pub fn last(&self) -> i32

Returns the context size.

Equivalent to the XPath last() function.

Source

pub fn advance_position(&mut self)

Advance the proximity position by one.

Called when iterating over the context list during predicate evaluation.

Source

pub fn reset_position(&mut self)

Rewind the proximity position to 1.

Trait Implementations§

Source§

impl Clone for XPathContext

Source§

fn clone(&self) -> XPathContext

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 XPathContext

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for XPathContext

Source§

fn default() -> Self

Create a default context with a null document pointer.

This is useful when you need a context for testing or when the document will be set later via set_context_node.

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> 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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.