Module pattern

Module pattern 

Source
Expand description

§Support for XPath patterns.

This module provides both a parser to compile a Pattern, and an interpreter to determine if an item matches a compiled pattern.

Patterns are defined in XSLT 3.0 5.5.2.

A string can be compiled as Pattern by using the try_from associated function.

use xrust::pattern::Pattern;

let p: Pattern<N> = Pattern::try_from("child::foobar")
        .expect("unable to compile pattern");

An Item can then be tested to see if it matches the Pattern. To do that, it is necessary to have a transformation Context.

let p = Pattern::try_from("/").expect("unable to compile pattern");
let n = Item::Node(RNode::new_document());

// Create a static context
let mut static_context = StaticContextBuilder::new()
    .message(|_| Ok(()))
    .fetcher(|_| Err(Error::new(ErrorKind::NotImplemented, "not implemented")))
    .parser(|_| Err(Error::new(ErrorKind::NotImplemented, "not implemented")))
   .build();

// This pattern matches the root node
assert_eq!(p.matches(&Context::new(), &mut static_context, &n), true)
let p = Pattern::try_from("child::foobar").expect("unable to compile pattern");
let n = Item::Node(RNode::new_document());
// Create a static context
// This pattern will not match because "n" is not an element named "foobar"
assert_eq!(p.matches(&Context::new(), &mut static_context, &n), false)

Structs§

Step

Enums§

Branch
Pattern
An XPath pattern. A pattern most frequently appears as the value of a match attribute. A pattern is either a predicate pattern or a selection pattern.

Type Aliases§

Path