Module: XML::XXPathMethods

Included in:
REXML::Child, XML::XXPath::Accessors::Attribute
Defined in:
lib/xml/xxpath_methods.rb

Overview

set of convenience wrappers around XML::XPath’s instance methods, for people who frequently use XML::XPath directly. This module is included into the REXML node classes and adds methods to them that enable you to write a call like

path.first(xml_element)

as the more pleasant-looking variant

xml_element.first_xpath(path)

with the added bonus that path may not only be an XML::XXPath instance, but also just a String containing the XPath expression.

Please note that the names of all the added methods are suffixed with “_xpath” to avoid name clashes with REXML methods. Please note also that this was changed recently, so older versions of xml-mapping (version < 0.9) used method names without _xpath appended and thus would be incompatible with this one here.

As a special convenience, if you’re using an older version of REXML that doesn’t have the new methods yet, methods without _xpath in their names will also (additionally) be added to the REXML classes. This will enable code that relied on the old names to keep on working as long as REXML isn’t updated, at which point that code will fail and must be changed to used the methods suffixed with _xpath.

Instance Method Summary collapse

Instance Method Details

#all_xpath(path, options = {}) ⇒ Object

see XML::XXPath#all



47
48
49
# File 'lib/xml/xxpath_methods.rb', line 47

def all_xpath(path,options={})
  to_xxpath(path).all self, options
end

#create_new_xpath(path) ⇒ Object

see XML::XXPath#create_new



52
53
54
# File 'lib/xml/xxpath_methods.rb', line 52

def create_new_xpath(path)
  to_xxpath(path).create_new self
end

#each_xpath(path, options = {}, &block) ⇒ Object

see XML::XXPath#each



37
38
39
# File 'lib/xml/xxpath_methods.rb', line 37

def each_xpath(path,options={},&block)
  to_xxpath(path).each self, options, &block
end

#first_xpath(path, options = {}) ⇒ Object

see XML::XXPath#first



42
43
44
# File 'lib/xml/xxpath_methods.rb', line 42

def first_xpath(path,options={})
  to_xxpath(path).first self, options
end

#to_xxpath(path) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/xml/xxpath_methods.rb', line 75

def to_xxpath(path)
  if String===path
    XXPath.new path
  else
    path
  end
end