From: Robert Klemme Date: 2005-07-05T18:05:45+09:00 Subject: Re: Finding all paths in an XML document Ghislain Mary wrote: > Hi all, > > I'd like to know how you would search for all the paths of an XML > document using REXML. For example, let say that the document is the > following: > > > a simple sample > > some text > > > > an other sample > > > > > What I'd like to have is an Array like this: > [['content'], > ['node'], > ['node', 'content'], > ['node', 'foo'], > ['node', 'foo', 'content']] > > Any ideas ? > > Thanks, > > Ghislain This might help: require "rexml/document" doc = File.open( "doc.xml" ){|io| REXML::Document.new io} doc.elements.each( "//*" ) do |el| p el.xpath.split(%r{/}) end Alternative: require "rexml/document" doc = File.open( "doc.xml" ){|io| REXML::Document.new io} doc.elements.each "//*" do |el| path = [] par = el while par path.unshift par.name par = par.parent end path.shift p path end Kind regards robert