Expand description
Provide APIs for compiling and evaluating XPath.
The current implementation supports only XPath 1.0.
§Example
use anyxml::xpath::evaluate_str;
const DOCUMENT: &str = r#"<root>
<greeting xml:lang='en'>Hello</greeting>
<greeting xml:lang='ja'>こんにちは</greeting>
<greeting xml:lang='ch'>你好</greeting>
</root>
"#;
const XPATH: &str = "//greeting[lang('ja')]/text()";
let text = evaluate_str(XPATH, DOCUMENT, None)
.unwrap()
.as_string()
.unwrap();
assert_eq!(text.as_ref(), "こんにちは");If a single expression is reused multiple times,
it can be precompiled using the compile function.
use anyxml::{
sax::parser::XMLReaderBuilder,
tree::TreeBuildHandler,
xpath::compile,
};
const DOCUMENT: &str = r#"<root>
<greeting xml:lang='en'>Hello</greeting>
<greeting xml:lang='ja'>こんにちは</greeting>
<greeting xml:lang='ch'>你好</greeting>
</root>
"#;
const XPATH: &str = "//greeting[lang('ja')]/text()";
let mut reader = XMLReaderBuilder::new()
.set_handler(TreeBuildHandler::default())
.build();
reader.parse_str(DOCUMENT, None).unwrap();
let document = reader.handler.document;
let mut expression = compile(XPATH).unwrap();
let text = expression.evaluate(document)
.unwrap()
.as_string()
.unwrap();
assert_eq!(text.as_ref(), "こんにちは");Structs§
- XPath
Context - XPath evaluation context.
- XPath
Expression - Precompiled XPath expression.
- XPath
Node Set
Enums§
- XPath
Compile Error - XPath
Error - XPath
Object - XPath object types.
Functions§
- compile
- Compile
xpathas a XPath expression. - evaluate
- Evaluate
xpathas an XPath expression withdocumentas the initial context node. - evaluate_
reader - Parse the XML documents using
readeras the source and evaluatexpathas an XPath expression. - evaluate_
str - Parse
xmlas the XML document and evaluatexpathas an XPath expression. - evaluate_
uri - Parse the XML document where
uriis the base URI of the source, and evaluatexpathas an XPath expression.