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()andlast(). - 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 _xmlDocThe current XML document.
context_node: *mut _xmlNodeThe current context node.
context_position: i32Position of the context node within the context list (1-based).
context_size: i32Size 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: i32Current proximity position (for last() / position()).
context_list: Vec<*mut _xmlNode>The context list for position() / last().
recursion_depth: u32Recursion depth counter (to prevent infinite recursion).
var_lookup_func: Option<VarLookupFunc>C callback for variable lookup.
var_lookup_data: *mut c_voidOpaque data pointer passed to var_lookup_func.
func_lookup_func: Option<FuncLookupFunc>C callback for function lookup.
func_lookup_data: *mut c_voidOpaque data pointer passed to func_lookup_func.
Implementations§
Source§impl XPathContext
impl XPathContext
Sourcepub fn new(doc: *mut _xmlDoc) -> Self
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.
Sourcepub fn set_context_node(&mut self, node: *mut _xmlNode)
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.
Sourcepub fn set_context_list(&mut self, nodes: Vec<*mut _xmlNode>)
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.
Sourcepub fn resolve_variable(&self, name: &str) -> Option<XPathValue>
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.
Sourcepub fn resolve_namespace(&self, prefix: &str) -> Option<String>
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.
Sourcepub fn lookup_function(&self, name: &str) -> Option<XPathFunction>
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.
Sourcepub fn register_function(&mut self, name: &str, func: XPathFunction)
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.
Sourcepub fn register_variable(&mut self, name: &str, value: XPathValue)
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.
Sourcepub fn register_namespace(&mut self, prefix: &str, uri: &str)
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.
Sourcepub fn set_error(&mut self, msg: &str)
pub fn set_error(&mut self, msg: &str)
Record an error message.
Overwrites any previously recorded error. Use clear_error to reset.
Sourcepub fn clear_error(&mut self)
pub fn clear_error(&mut self)
Clear any recorded error.
Sourcepub fn push_recursion(&mut self) -> Result<(), String>
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.
Sourcepub fn pop_recursion(&mut self)
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).
Sourcepub fn has_context_node(&self) -> bool
pub fn has_context_node(&self) -> bool
Returns true if a context node is set (non-null).
Sourcepub fn reset(&mut self)
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.
Sourcepub fn position(&self) -> i32
pub fn position(&self) -> i32
Returns the current proximity position (1-based).
Equivalent to the XPath position() function.
Sourcepub fn advance_position(&mut self)
pub fn advance_position(&mut self)
Advance the proximity position by one.
Called when iterating over the context list during predicate evaluation.
Sourcepub fn reset_position(&mut self)
pub fn reset_position(&mut self)
Rewind the proximity position to 1.
Trait Implementations§
Source§impl Clone for XPathContext
impl Clone for XPathContext
Source§fn clone(&self) -> XPathContext
fn clone(&self) -> XPathContext
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for XPathContext
impl Debug for XPathContext
Source§impl Default for XPathContext
impl Default for XPathContext
Source§fn default() -> Self
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.