Skip to main content

Module xpath

Module xpath 

Source
Expand description

§Parse XPath expressions

An XPath expression parser using the xrust parser combinator that produces a xrust transformation.

use xrust::parser::xpath::parse;
let t = parse::<N>("/child::A/child::B/child::C", None, None).expect("unable to parse XPath expression");

“t” now contains a Transform that will return “C” elements that have a “B” parent and an “A” grandparent in the source document.

To evaluate the transformation we need a Context with a source document as its current item.

use xrust::item::{Sequence, SequenceTrait, Item, Node, NodeType};
use xrust::trees::smite::RNode;
use xrust::parser::ParseError;
use xrust::parser::xml::parse as xmlparse;
use xrust::parser::xpath::parse;
use xrust::transform::context::{Context, ContextBuilder, StaticContext, StaticContextBuilder};

let t = parse("/child::A/child::B/child::C", None, None)
    .expect("unable to parse XPath expression");

let source = RNode::new_document();
xmlparse(source.clone(), "<A><B><C/></B><B><C/></B></A>", Some(|_: &_| Err(ParseError::MissingNameSpace)))
    .expect("unable to parse XML");
let mut static_context = StaticContextBuilder::new()
    .message(|_| Ok(()))
    .fetcher(|_| Ok(String::new()))
    .parser(|_| Err(Error::new(ErrorKind::NotImplemented, "not implemented")))
    .build();
let context = ContextBuilder::new()
    .context(vec![Item::Node(source)])
    .build();
let sequence = context.dispatch(&mut static_context, &t)
    .expect("evaluation failed");
assert_eq!(sequence.len(), 2);
assert_eq!(sequence.to_xml(), "<C/><C/>")

Functions§

expr
parse
Parse an XPath expression to produce a Transform. The optional Node or NamespaceMap may be used to resolve XML Namespaces (The Node will be searched first).