Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!talisker.lacave.net!lacave.net!not-for-mail From: "Kyle X." Newsgroups: comp.lang.ruby Subject: Re: Can you search in REXML by attributes? Date: Fri, 1 Apr 2011 11:27:10 -0500 Organization: Service de news de lacave.net Lines: 100 Message-ID: <07d88b73a8b6b59812b5fed98c782aca@ruby-forum.com> References: <6680e1dd986ba2ce87d806950a81ee57@ruby-forum.com> NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: talisker.lacave.net 1301675248 38959 65.111.164.187 (1 Apr 2011 16:27:28 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Fri, 1 Apr 2011 16:27:28 +0000 (UTC) In-Reply-To: X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 380755 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: <07d88b73a8b6b59812b5fed98c782aca@ruby-forum.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:2114 Robert K. wrote in post #990336: > On Fri, Apr 1, 2011 at 2:53 AM, Kyle X. wrote: > > It is not entirely clear what you want. Do you want to look for all > "ref" instances and find elements they are referring to? Or do you > want to do some kind of graph traversal where you start with a > particular element and follow every ref attribute? Hi and thank you for your help. I am sorry if what I wrote was unclear. What my goal is is to start at a given location (in this case- ) and eventually grab the three IfcLengthMeasure text values, that are associated with this , and put them into an array. > If the latter you can for example do a BFS. > > 10:11:30 Temp$ ./rx.rb > --- VISIT: > > > > > > --- VISIT: > > > 117.4 > 119.7 > 0. > > > 10:11:43 Temp$ cat -n rx.rb > 1 #!/bin/env ruby19 > 2 > 3 require 'rexml/document' > 4 > 5 doc = REXML::Document.new(DATA.read) > 6 > 7 # BFS > 8 queue = %w{i1671} > 9 > 10 until queue.empty? > 11 id = queue.shift > 12 > 13 REXML::XPath.each(doc, "//*[@id='#{id}']") do |e| > 14 puts "--- VISIT:", e > 15 > 16 REXML::XPath.each(e, './/*[@ref]') do |child| > 17 next_id = child.attribute('ref') and queue.push(next_id) > 18 end > 19 end > 20 end > 21 > 22 __END__ > 23 > 24 > 25 > 26 > 27 > 28 > 29 > 30 > 31 > 32 > 33 > 34 > 35 > 36 > 37 > 38 117.4 > 39 119.7 > 40 0. > 41 > 42 > 43 > 10:11:47 Temp$ I will give this a try. > > Kind regards > > robert Dear 7stud. Using the XPath command: doc = Document.new xml target = XPath.match(doc, "//*[@id = 'i1671']") p target It produces the following output as you said it would. --output:-- [ ... ] But I cannot figure out how to do anything with this from here to get to the next point, and eventually be able to grab the three values. -- Posted via http://www.ruby-forum.com/.