Class: CocoaXML::NodeSet

Inherits:
Array
  • Object
show all
Defined in:
lib/cocoa-xml/nodeset.rb

Instance Method Summary collapse

Instance Method Details

#[](attr) ⇒ Array<String, nil>

Get the value of the attribute for each node in set

Parameters:

  • attr (String)

    attribute to search for on each node

Returns:

  • (Array<String, nil>)

    value of attribute for each node



53
54
55
# File 'lib/cocoa-xml/nodeset.rb', line 53

def [](attr)
  collect { |node| node[attr] }.flatten
end

#css(selector) ⇒ NodeSet<NSXMLNode, String>

TODO:

This will bomb if the node has String elements in it

Perform selector on each element in set

Parameters:

  • selector (String)

    css selector to use on each node

Returns:

  • (NodeSet<NSXMLNode, String>)

    new set resulting from performing selector on each node in set



24
25
26
# File 'lib/cocoa-xml/nodeset.rb', line 24

def css(selector)
  xpath ::Nokogiri::CSS::xpath_for(selector, :prefix => ".//").join
end

#textString Also known as: inner_text

Collect all the texts of nodes in set and join together

Returns:

  • (String)

    single string containing text of each element



6
7
8
# File 'lib/cocoa-xml/nodeset.rb', line 6

def text
  collect { |node| (node.respond_to?(:to_str) && node.to_str) || node.text }.flatten.join
end

#to_sString

Collect all the #to_s representations of elements in array

Returns:

  • (String)

    single string containing #to_s of each element



14
15
16
# File 'lib/cocoa-xml/nodeset.rb', line 14

def to_s
  collect { |node| node.to_s }.join
end

#xpath(query) ⇒ NodeSet<NSXMLNode>

TODO:

This will bomb if the node has String elements in it

Perform XPath selection on each node in set

Parameters:

  • path (String)

    xpath to follow on each node

Returns:

  • (NodeSet<NSXMLNode>)

    results of path on each node



44
45
46
47
# File 'lib/cocoa-xml/nodeset.rb', line 44

def xpath(query)
  query.sub! %r{^//}, ''    # Roots searches to start from nodes
  NodeSet.new(collect { |node| node.xpath(query) }.flatten)
end

#xquery(query) ⇒ NodeSet<NSXMLNode, String>

TODO:

This will bomb if the node has String elements in it

Perform XQuery on each node in set

Parameters:

  • query (String)

    xquery to perform on each node in set

Returns:

  • (NodeSet<NSXMLNode, String>)

    results of performing query



34
35
36
37
# File 'lib/cocoa-xml/nodeset.rb', line 34

def xquery(query)
  query.sub! %r{^//}, ''    # Root searches to start from nodes
  NodeSet.new(collect { |node| node.xquery(query) }.flatten)
end