From: 7stud -- Date: 2009-02-14T02:46:27+09:00 Subject: Re: Xpath Alberto Alberto wrote: > > I would like to know if it is possible, using REXML, get full path from > root, of document XML, to leaf node, through a XPath query? > > > example with l'XML: > > > > > > > > ... > > > > > Xpath: //terzo[@id='3'] > > Output: > //primo/secondo/terzo[id='3'] > Is that really considered XML? require "rexml/document" include REXML xml = < ... TO_END_OF_XML doc = Document.new xml target = "//terzo[@id='3']" paths = [] XPath.each(doc, target) do |element| parents = [] while true parent = element.parent break if parent.name == "" parents << parent.name element = parent end path = parents.reverse.join('/') paths << "//#{path}#{target[1..-1]}" end puts *paths --output:-- //primo/secondo/terzo[@id='3'] //primo/terzo[@id='3'] -- Posted via http://www.ruby-forum.com/.