Class: Canon::Xml::XPathEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/xml/xpath_engine.rb

Overview

XPath evaluation engine for C14N subset selection.

Supports a focused subset of XPath 1.0 sufficient for W3C C14N subset canonicalization:

  • Absolute paths: /root/child, /root/child

  • Descendant-or-self: //element, //ns:element

  • Predicates: [1] (position), [@attr], [@attr=‘value’]

  • Wildcards: *

  • Union: expr1 | expr2

Not supported (not needed for C14N subset):

  • Axes other than child and descendant-or-self

  • Functions (last(), position(), etc.)

  • Variables

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ XPathEngine

Returns a new instance of XPathEngine.



31
32
33
# File 'lib/canon/xml/xpath_engine.rb', line 31

def initialize(root)
  @root = root
end

Class Method Details

.evaluate(root, xpath) ⇒ Array<Node>

Evaluate an XPath expression against a data model tree.

Parameters:

  • root (Nodes::RootNode)

    Root of the data model tree

  • xpath (String)

    XPath expression

Returns:

  • (Array<Node>)

    Matched nodes in document order



27
28
29
# File 'lib/canon/xml/xpath_engine.rb', line 27

def self.evaluate(root, xpath)
  new(root).evaluate(xpath)
end

Instance Method Details

#evaluate(xpath) ⇒ Array<Node>

Evaluate an XPath expression and return matched nodes.

Parameters:

  • xpath (String)

    XPath expression

Returns:

  • (Array<Node>)

    Matched nodes in document order



39
40
41
42
43
44
45
46
# File 'lib/canon/xml/xpath_engine.rb', line 39

def evaluate(xpath)
  # Handle union operator (|)
  if xpath.include?("|")
    xpath.split("|").flat_map { |expr| evaluate(expr.strip) }.uniq
  else
    evaluate_path(xpath.strip)
  end
end