Class: Moxml::XPath::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/moxml/xpath/engine.rb

Overview

XPath 1.0 evaluation engine

This engine provides complete XPath 1.0 support for Moxml documents, particularly useful for the Ox adapter which has limited native XPath.

Examples:

Evaluate XPath expression

engine = Moxml::XPath::Engine.new(document)
results = engine.evaluate("//book[@id='123']/title")

With context node

engine = Moxml::XPath::Engine.new(document)
results = engine.evaluate("./author", context: book_element)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Engine

Initialize engine with a document

Parameters:



24
25
26
# File 'lib/moxml/xpath/engine.rb', line 24

def initialize(document)
  @document = document
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



19
20
21
# File 'lib/moxml/xpath/engine.rb', line 19

def document
  @document
end

Instance Method Details

#evaluate(expression, context: nil) ⇒ Moxml::NodeSet, ...

Evaluate an XPath expression

Parameters:

  • expression (String)

    XPath expression to evaluate

  • context (Moxml::Node, nil) (defaults to: nil)

    Context node (defaults to document root)

Returns:

  • (Moxml::NodeSet, String, Numeric, Boolean)

    Result depends on expression

Raises:



35
36
37
38
# File 'lib/moxml/xpath/engine.rb', line 35

def evaluate(expression, context: nil)
  raise ::NotImplementedError,
        "XPath engine implementation in progress (Phase 1.1+)"
end

#valid?(expression) ⇒ Boolean

Check if expression is valid XPath syntax

Parameters:

  • expression (String)

    XPath expression to validate

Returns:

  • (Boolean)

    true if valid, false otherwise



44
45
46
47
48
49
# File 'lib/moxml/xpath/engine.rb', line 44

def valid?(expression)
  evaluate(expression, context: document.root)
  true
rescue Moxml::XPath::SyntaxError
  false
end