pub struct XPathContext<'doc> {
pub index: DocIndex<'doc>,
/* private fields */
}Expand description
Reusable XPath context for an arena-allocated ArenaDocument.
Build once, evaluate many times. Internally the XPath evaluator is
generic over DocIndexLike.
Fields§
§index: DocIndex<'doc>The flat index used by the evaluator. Exposed so external
adapters (e.g. the libxml2 C-ABI shim in sup-xml-compat)
can build their own result-object wrappers without rebuilding
the index.
Implementations§
Source§impl<'doc> XPathContext<'doc>
impl<'doc> XPathContext<'doc>
Sourcepub fn new(doc: &'doc Document) -> XPathContext<'doc>
pub fn new(doc: &'doc Document) -> XPathContext<'doc>
Build a strict-mode evaluation context for doc. O(n) in
the number of nodes. Equivalent to
new_with with default XPathOptions.
Sourcepub fn new_with(
doc: &'doc Document,
options: XPathOptions,
) -> XPathContext<'doc>
pub fn new_with( doc: &'doc Document, options: XPathOptions, ) -> XPathContext<'doc>
Build an evaluation context for doc with explicit options.
Use this to opt into libxml2-compatible behaviour for the
three spec deviations called out on XPathOptions.
pub fn eval(&self, src: &str) -> Result<Value, XmlError>
Sourcepub fn eval_at(&self, src: &str, context_node: usize) -> Result<Value, XmlError>
pub fn eval_at(&self, src: &str, context_node: usize) -> Result<Value, XmlError>
Evaluate src against a specific context node. XPath 1.0
allows the context-node to be any node in the document; the
libxml2 ABI exposes this via xmlXPathContext.node.
Sourcepub fn eval_with(
&self,
src: &str,
context_node: usize,
bindings: &dyn XPathBindings,
) -> Result<Value, XmlError>
pub fn eval_with( &self, src: &str, context_node: usize, bindings: &dyn XPathBindings, ) -> Result<Value, XmlError>
Evaluate src against context_node, consulting bindings
for user-registered functions (extensions= in lxml),
variables ($varname), and namespace prefix resolution. This
is the entry point the libxml2 C-ABI shim
(sup-xml-compat::xpath) uses when its xmlXPathContext
carries registered namespaces, functions, or variables.
Sourcepub fn id_for_element(&self, ptr: *const Node<'_>) -> Option<usize>
pub fn id_for_element(&self, ptr: *const Node<'_>) -> Option<usize>
Find the index ID for a node identified by its arena pointer.
Returns None if the pointer doesn’t match any indexed node.
Linear scan; O(n) in document size — call once per evaluation,
not per expression step.
Handles attribute pointers too: libxslt sets the XPath context
node to an xmlAttr* while iterating @* (<xsl:for-each select="@*">), and the C-ABI layer hands that pointer here cast
to *const Node. The match is by raw address, so comparing the
indexed &Attribute against ptr is sound regardless of the
nominal pointer type.
pub fn eval_bool(&self, src: &str) -> Result<bool, XmlError>
pub fn eval_str(&self, src: &str) -> Result<String, XmlError>
pub fn eval_num(&self, src: &str) -> Result<f64, XmlError>
pub fn eval_strings(&self, src: &str) -> Result<Vec<String>, XmlError>
pub fn eval_count(&self, src: &str) -> Result<usize, XmlError>
Sourcepub fn eval_node_xml(&self, src: &str) -> Result<Vec<String>, XmlError>
pub fn eval_node_xml(&self, src: &str) -> Result<Vec<String>, XmlError>
Evaluate src and return each matched node serialized as XML
(the XPath equivalent of xmllint --xpath’s default output).
- Element / Text / Comment / CData / PI nodes serialize to their
subtree XML form, identical to [
crate::serialize_node_to_string] with default options. - Attribute nodes render as
name="value"with the value attribute-escaped. - Namespace nodes render as
xmlns:prefix="uri"(orxmlns="…"for the default namespace). - The synthetic Document node (returned by
/) serializes as the whole document viacrate::serialize_to_string.
Non-NodeSet results (string / number / boolean) collapse to a
single-element vec containing their XPath 1.0 string form,
matching Self::eval_strings.