Skip to main content

XPath

Struct XPath 

Source
pub struct XPath { /* private fields */ }
Available on crate feature xpath only.
Expand description

A compiled XPath expression.

Compiling is separated from evaluation because the compiled form is document-independent and reusable — evaluating the same expression across a thousand documents should parse it once, not a thousand times.

Implementations§

Source§

impl XPath

Source

pub fn compile(expr: &str) -> Result<Self, XPathError>

Compile an expression.

§Errors

Returns XPathError if the expression is malformed, names a function outside XPath 1.0’s library, or passes a function the wrong number of arguments. The last two are compile errors rather than empty results on purpose: an unknown name that evaluated to nothing was indistinguishable from a document with no match.

§Examples
use oxml::XPath;
let xp = XPath::compile("//book[@lang='en']/title")?;
Source

pub fn compile_with_namespaces( expr: &str, namespaces: &[(&str, &str)], ) -> Result<Self, XPathError>

Compile with namespace prefixes bound to URIs.

XPath 1.0 resolves a prefix in an expression against the expression’s context rather than the document’s declarations. The same prefix can mean different things in the two, and resolving against the document would make an expression’s meaning depend on which document it ran against.

A prefix the expression uses and this list does not bind is a compile error. xml is bound by specification and need not be given.

§Errors

Returns XPathError as XPath::compile does, and additionally if the expression uses a prefix these bindings do not cover.

§Examples
use oxml::{XPath, parse};

let doc = parse(r#"<r xmlns:m="urn:u"><m:a>yes</m:a><a>no</a></r>"#)?;
let xp = XPath::compile_with_namespaces("//m:a", &[("m", "urn:u")])?;
assert_eq!(xp.evaluate(&doc).to_str(&doc), "yes");

// The prefix in the expression is independent of the one in the
// document: only the URI has to match.
let xp = XPath::compile_with_namespaces("//q:a", &[("q", "urn:u")])?;
assert_eq!(xp.evaluate(&doc).to_str(&doc), "yes");
Source

pub fn evaluate(&self, doc: &Document) -> Value

Evaluate against a document, starting at its root.

Source

pub fn evaluate_from(&self, doc: &Document, context: NodeId) -> Value

Evaluate with an explicit context node.

Source

pub const fn expr(&self) -> &Expr

The compiled expression tree.

Exposed for tooling that wants to inspect or rewrite queries rather than only run them.

Trait Implementations§

Source§

impl Clone for XPath

Source§

fn clone(&self) -> XPath

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 XPath

Source§

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

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

impl PartialEq for XPath

Source§

fn eq(&self, other: &XPath) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for XPath

Auto Trait Implementations§

§

impl Freeze for XPath

§

impl RefUnwindSafe for XPath

§

impl Send for XPath

§

impl Sync for XPath

§

impl Unpin for XPath

§

impl UnsafeUnpin for XPath

§

impl UnwindSafe for XPath

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> 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> 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 = !

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.