pub struct XPath { /* private fields */ }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
impl XPath
Sourcepub fn compile(expr: &str) -> Result<Self, XPathError>
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")?;Sourcepub fn compile_with_namespaces(
expr: &str,
namespaces: &[(&str, &str)],
) -> Result<Self, XPathError>
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");Sourcepub fn evaluate(&self, doc: &Document) -> Value
pub fn evaluate(&self, doc: &Document) -> Value
Evaluate against a document, starting at its root.
Sourcepub fn evaluate_from(&self, doc: &Document, context: NodeId) -> Value
pub fn evaluate_from(&self, doc: &Document, context: NodeId) -> Value
Evaluate with an explicit context node.