2003-06-09 21:31:01 -04:00
|
|
|
require 'rexml/functions'
|
|
|
|
require 'rexml/xpath_parser'
|
|
|
|
|
|
|
|
module REXML
|
2008-10-01 09:46:53 -04:00
|
|
|
# Wrapper class. Use this class to access the XPath functions.
|
|
|
|
class XPath
|
|
|
|
include Functions
|
|
|
|
EMPTY_HASH = {}
|
2003-06-09 21:31:01 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
# 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.
|
2008-12-02 19:31:58 -05:00
|
|
|
# variables::
|
|
|
|
# If supplied, a Hash which maps $variables in the query
|
|
|
|
# to values. This can be used to avoid XPath injection attacks
|
|
|
|
# or to automatically handle escaping string values.
|
2008-10-01 09:46:53 -04:00
|
|
|
#
|
|
|
|
# XPath.first( node )
|
|
|
|
# XPath.first( doc, "//b"} )
|
|
|
|
# XPath.first( node, "a/x:b", { "x"=>"http://doofus" } )
|
2008-12-02 19:31:58 -05:00
|
|
|
# XPath.first( node, '/book/publisher/text()=$publisher', {}, {"publisher"=>"O'Reilly"})
|
2006-09-07 22:03:44 -04:00
|
|
|
def XPath::first element, path=nil, namespaces=nil, variables={}
|
|
|
|
raise "The namespaces argument, if supplied, must be a hash object." unless namespaces.nil? or namespaces.kind_of?(Hash)
|
|
|
|
raise "The variables argument, if supplied, must be a hash object." unless variables.kind_of?(Hash)
|
2008-10-01 09:46:53 -04:00
|
|
|
parser = XPathParser.new
|
|
|
|
parser.namespaces = namespaces
|
|
|
|
parser.variables = variables
|
|
|
|
path = "*" unless path
|
|
|
|
element = [element] unless element.kind_of? Array
|
|
|
|
parser.parse(path, element).flatten[0]
|
|
|
|
end
|
2003-06-09 21:31:01 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
# 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
|
2008-12-02 19:31:58 -05:00
|
|
|
# variables::
|
|
|
|
# If supplied, a Hash which maps $variables in the query
|
|
|
|
# to values. This can be used to avoid XPath injection attacks
|
|
|
|
# or to automatically handle escaping string values.
|
2008-10-01 09:46:53 -04:00
|
|
|
#
|
|
|
|
# XPath.each( node ) { |el| ... }
|
|
|
|
# XPath.each( node, '/*[@attr='v']' ) { |el| ... }
|
|
|
|
# XPath.each( node, 'ancestor::x' ) { |el| ... }
|
2008-12-02 19:31:58 -05:00
|
|
|
# XPath.each( node, '/book/publisher/text()=$publisher', {}, {"publisher"=>"O'Reilly"}) \
|
|
|
|
# {|el| ... }
|
2008-10-01 09:46:53 -04:00
|
|
|
def XPath::each element, path=nil, namespaces=nil, variables={}, &block
|
2006-09-07 22:03:44 -04:00
|
|
|
raise "The namespaces argument, if supplied, must be a hash object." unless namespaces.nil? or namespaces.kind_of?(Hash)
|
|
|
|
raise "The variables argument, if supplied, must be a hash object." unless variables.kind_of?(Hash)
|
2008-10-01 09:46:53 -04:00
|
|
|
parser = XPathParser.new
|
|
|
|
parser.namespaces = namespaces
|
|
|
|
parser.variables = variables
|
|
|
|
path = "*" unless path
|
|
|
|
element = [element] unless element.kind_of? Array
|
|
|
|
parser.parse(path, element).each( &block )
|
|
|
|
end
|
2003-06-09 21:31:01 -04:00
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
# Returns an array of nodes matching a given XPath.
|
2008-10-01 09:46:53 -04:00
|
|
|
def XPath::match element, path=nil, namespaces=nil, variables={}
|
|
|
|
parser = XPathParser.new
|
|
|
|
parser.namespaces = namespaces
|
|
|
|
parser.variables = variables
|
|
|
|
path = "*" unless path
|
|
|
|
element = [element] unless element.kind_of? Array
|
|
|
|
parser.parse(path,element)
|
|
|
|
end
|
|
|
|
end
|
2003-06-09 21:31:01 -04:00
|
|
|
end
|