Get XPath from node : XPath « XML « Ruby






Get XPath from node


xml = %{
<aquarium>
 <fish color="blue" size="small" />
 <fish color="orange" size="large">
   <fish color="green" size="small">
    <fish color="red" size="tiny" />
   </fish>
 </fish>
 <decoration type="castle" style="gaudy">
  <algae color="green" />
 </decoration>
</aquarium>}

require 'rexml/document'
doc = REXML::Document.new xml

p red_fish = doc.children[0]


p red_fish.xpath


p REXML::XPath.first(doc, red_fish.xpath)

 








Related examples in the same category

1.Pass xml node to a method
2.Navigating a Document with XPath
3.REXML::XPath.match(doc, '//[@color="green"]')
4.Find the second green element.
5.Find the color attributes of all small fish.
6.Count how many fish are inside the large fish.
7.each loop on element
8.Get the first element and first child
9.Search element by XPath