Skip to main content

evaluate

Function evaluate 

Source
pub fn evaluate(
    doc: &Document,
    context_node: NodeId,
    expression: &str,
) -> Result<XPathValue, XPathError>
Expand description

Evaluates an XPath 1.0 expression against a document node.

This is a convenience function that parses the expression and evaluates it in a single call. For evaluating the same expression against multiple context nodes, use parser::parse and XPathContext::evaluate separately to avoid re-parsing.

§Examples

use xmloxide::Document;
use xmloxide::xpath::{evaluate, XPathValue};

let doc = Document::parse_str("<root><child>Hello</child></root>").unwrap();
let root = doc.root_element().unwrap();

// Count child elements
let result = evaluate(&doc, root, "count(*)").unwrap();
assert_eq!(result.to_number(), 1.0);

// Get text content
let result = evaluate(&doc, root, "string(child)").unwrap();
assert_eq!(result.to_xpath_string(), "Hello");

§Errors

Returns XPathError if the expression is malformed or evaluation fails.