Skip to main content

XPathContext

Struct XPathContext 

Source
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>

Source

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.

Source

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.

Source

pub fn eval(&self, src: &str) -> Result<Value, XmlError>

Source

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.

Source

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.

Source

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.

Source

pub fn eval_bool(&self, src: &str) -> Result<bool, XmlError>

Source

pub fn eval_str(&self, src: &str) -> Result<String, XmlError>

Source

pub fn eval_num(&self, src: &str) -> Result<f64, XmlError>

Source

pub fn eval_strings(&self, src: &str) -> Result<Vec<String>, XmlError>

Source

pub fn eval_count(&self, src: &str) -> Result<usize, XmlError>

Source

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" (or xmlns="…" for the default namespace).
  • The synthetic Document node (returned by /) serializes as the whole document via crate::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.

Auto Trait Implementations§

§

impl<'doc> !Freeze for XPathContext<'doc>

§

impl<'doc> !RefUnwindSafe for XPathContext<'doc>

§

impl<'doc> !Send for XPathContext<'doc>

§

impl<'doc> !Sync for XPathContext<'doc>

§

impl<'doc> !UnwindSafe for XPathContext<'doc>

§

impl<'doc> Unpin for XPathContext<'doc>

§

impl<'doc> UnsafeUnpin for XPathContext<'doc>

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> 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> Same for T

Source§

type Output = T

Should always be Self
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.