Skip to main content

Module pattern

Module pattern 

Source
Expand description

A small subset of XPath 1.0 sized for fast per-node matching.

This is the engine behind libxml2’s xmlPattern family — a stripped XPath dialect that the SAX/pull-parser side of libxml2 uses for questions like “does this node match //book[@id]?” during a traversal. It’s much faster than firing the full XPath engine per node, and the grammar covers most real-world streaming selectors.

§Grammar

Pattern    := Branch ('|' Branch)*
Branch     := '/'?  Step ('/' Step | '//' Step)*
Step       := AxisStep Predicate*
AxisStep   := '@' NCName
            | '@' '*'
            | NCName
            | '*'
Predicate  := '[' PredExpr ']'
PredExpr   := Integer                     -- positional, 1-based
            | 'last' '(' ')'              -- last position
            | '@' NCName                  -- attribute exists
            | '@' NCName '=' Literal      -- attribute equals
Literal    := '"' …no " … '"' | "'" …no ' … "'"

Not supported (intentional — fall outside libxml2’s pattern subset): XPath function calls in predicates beyond last(), other axes (parent::, ancestor::, …), numeric expressions, variable references. Patterns that use them won’t compile.

§Two evaluation modes

  • Backward walk: Pattern::matches — given a node with parent pointers, walk upward through ancestors checking each step. Random access; works on any DOM tree.
  • Forward streaming: [Pattern::streaming] — TODO. An NFA-shaped state machine that tracks pattern match progress as SAX-style events flow past, without needing parent pointers.

Structs§

Pattern
A compiled libxml2-flavour pattern. Match nodes via Self::matches.