| Class | REXML::XPath |
| In: |
lib/rexml/xpath.rb
|
| Parent: | Object |
| EMPTY_HASH | = | {} |
Iterates over nodes that match the given path, calling the supplied block with the match.
| element: | The context element |
| path: | The xpath to search for. If not supplied or nil, defaults to ’*’ |
| namespaces: | If supplied, a Hash which defines a namespace mapping |
XPath.each( node ) { |el| ... }
XPath.each( node, '/*[@attr='v']' ) { |el| ... }
XPath.each( node, 'ancestor::x' ) { |el| ... }
# File lib/rexml/xpath.rb, line 45
45: def XPath::each element, path=nil, namespaces=nil, variables={}, &block
46: raise "The namespaces argument, if supplied, must be a hash object." unless namespaces.nil? or namespaces.kind_of?(Hash)
47: raise "The variables argument, if supplied, must be a hash object." unless variables.kind_of?(Hash)
48: parser = XPathParser.new
49: parser.namespaces = namespaces
50: parser.variables = variables
51: path = "*" unless path
52: element = [element] unless element.kind_of? Array
53: parser.parse(path, element).each( &block )
54: end
Finds and returns the first node that matches the supplied xpath.
| element: | The context element |
| path: | The xpath to search for. If not supplied or nil, returns the first node matching ’*’. |
| namespaces: | If supplied, a Hash which defines a namespace mapping. |
XPath.first( node )
XPath.first( doc, "//b"} )
XPath.first( node, "a/x:b", { "x"=>"http://doofus" } )
# File lib/rexml/xpath.rb, line 22
22: def XPath::first element, path=nil, namespaces=nil, variables={}
23: raise "The namespaces argument, if supplied, must be a hash object." unless namespaces.nil? or namespaces.kind_of?(Hash)
24: raise "The variables argument, if supplied, must be a hash object." unless variables.kind_of?(Hash)
25: parser = XPathParser.new
26: parser.namespaces = namespaces
27: parser.variables = variables
28: path = "*" unless path
29: element = [element] unless element.kind_of? Array
30: parser.parse(path, element).flatten[0]
31: end
Returns an array of nodes matching a given XPath.
# File lib/rexml/xpath.rb, line 57
57: def XPath::match element, path=nil, namespaces=nil, variables={}
58: parser = XPathParser.new
59: parser.namespaces = namespaces
60: parser.variables = variables
61: path = "*" unless path
62: element = [element] unless element.kind_of? Array
63: parser.parse(path,element)
64: end