Module: Oga::XML::Querying

Included in:
Document, Element
Defined in:
lib/oga/xml/querying.rb

Overview

The Querying module provides methods that make it easy to run XPath/CSS queries on XML documents/elements.

Instance Method Summary collapse

Instance Method Details

#at_css(*args) ⇒ Object

Evaluates the given CSS expression and returns the first node in the set.

See Also:

  • Oga::XML::Querying.[[#css]

49
50
51
52
53
# File 'lib/oga/xml/querying.rb', line 49

def at_css(*args)
  result = css(*args)

  return result.is_a?(XML::NodeSet) ? result.first : result
end

#at_xpath(*args) ⇒ Object

Evaluates the given XPath expression and returns the first node in the set.

See Also:

  • Oga::XML::Querying.[[#xpath]

25
26
27
28
29
# File 'lib/oga/xml/querying.rb', line 25

def at_xpath(*args)
  result = xpath(*args)

  return result.is_a?(XML::NodeSet) ? result.first : result
end

#css(expression) ⇒ Object

Evaluates the given CSS expression.

Parameters:

  • expression (String)

    The CSS expression to run.

See Also:

  • Oga::XML::Querying.[Oga[Oga::XPath[Oga::XPath::Evaluator[Oga::XPath::Evaluator#initialize]

37
38
39
40
41
# File 'lib/oga/xml/querying.rb', line 37

def css(expression)
  ast = CSS::Parser.new(expression).parse

  return XPath::Evaluator.new(self).evaluate_ast(ast)
end

#xpath(expression, variables = {}) ⇒ Object

Evaluates the given XPath expression.

Parameters:

  • expression (String)

    The XPath expression to run.

  • variables (Hash) (defaults to: {})

    Variables to bind.

See Also:

  • Oga::XML::Querying.[Oga[Oga::XPath[Oga::XPath::Evaluator[Oga::XPath::Evaluator#initialize]

15
16
17
# File 'lib/oga/xml/querying.rb', line 15

def xpath(expression, variables = {})
  return XPath::Evaluator.new(self, variables).evaluate(expression)
end