Method: REXML::Element#xpath

Defined in:
lib/rexml/element.rb

#xpathObject

:call-seq:

xpath -> string_xpath

Returns the string xpath to the element relative to the most distant parent:

d = REXML::Document.new('<a><b><c/></b></a>')
a = d.root # => <a> ... </>
b = a[0]   # => <b> ... </>
c = b[0]   # => <c/>
d.xpath    # => ""
a.xpath    # => "/a"
b.xpath    # => "/a/b"
c.xpath    # => "/a/b/c"

If there is no parent, returns the expanded name of the element:

e = REXML::Element.new('foo')
e.xpath    # => "foo"


1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
# File 'lib/rexml/element.rb', line 1184

def xpath
  path_elements = []
  cur = self
  path_elements << __to_xpath_helper( self )
  while cur.parent
    cur = cur.parent
    path_elements << __to_xpath_helper( cur )
  end
  path_elements.reverse.join( "/" )
end