From: Todd Benson Date: 2007-07-13T01:21:41+09:00 Subject: Re: XML file processing using REXML (beginner's question) On 7/12/07, Ronald Fischer wrote: > Hello! > > I try to understand REXML using the tutorial in > > http://www.germane-software.com/software/rexml/docs/tutorial.html > > However, I have problems applying the path expressions to my > application. > Consider for example the following program, and in particular the last > line: > > #!/usr/bin/ruby > require "rexml/document" > require 'pp' > source=< > > > > > > > > > > ENDXML > doc=REXML::Document.new source > doc.elements.each("ns:toplevel/ns:request") { |element| puts > element.attributes["requestID"] } > root=doc.root > pp root.elements["ns:toplevel/ns:request[@requestID='2']"] > > The program outputs > > 1 > 2 > nil > > that is, it finds the two elements "request" and correctly outputs their > attribute > requestID, but when I try to locate request number two with the path > expression > > ns:toplevel/ns:request[@requestID='2'] > > it is not found. What am I doing wrong here? Your root variable represents your ns:toplevel so, either of these would return an object instead of nil ... pp root.elements["ns:request[@requestID='2']"] pp doc.elements["ns:toplevel/ns:request[@requestID='2']"] Todd