Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!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: Tue, 12 Apr 2011 14:39:21 -0500 Organization: Service de news de lacave.net Lines: 69 Message-ID: <432aa169fd6ad8a814926c8ea0c7e61b@ruby-forum.com> References: <6680e1dd986ba2ce87d806950a81ee57@ruby-forum.com> <07d88b73a8b6b59812b5fed98c782aca@ruby-forum.com> <560f26c9f7549339ef393be5559307fd@ruby-forum.com> <3eb231103a780e91a17cc5b95fd82ddf@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 1302637184 61330 65.111.164.187 (12 Apr 2011 19:39:44 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Tue, 12 Apr 2011 19:39:44 +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: 381375 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: <432aa169fd6ad8a814926c8ea0c7e61b@ruby-forum.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:2706 > The difference is that you have namespaces in your file. Check this URL: > > http://tenderlovemaking.com/2009/04/23/namespaces-in-xml/ > > In order to make this work, you can do something like this: > > require 'nokogiri' > > doc = Nokogiri::XML(File.read("one.xml")) > doc.collect_namespaces.each {|key,value| puts "#{key} => #{value}"} > doc.css("uosNS|IfcCartesianPoint uosNS|Coordinates > uosNS|IfcLengthMeasure", {"uosNS" => > "http://www.iai-tech.org/ifcXML/IFC2x3/FINAL"}).each {|el| puts > el.text} > > (I added a line that shows all namespaces in the document). All nodes > under the uos node inherit the namespace referenced by the url you see > in the code, so in order to search for nodes within the uos node, you > need to specify the namespace. Thank you for the response. After reading the link you provided to make any xml file read using Nokogiri if it has a namespace you must include that with each time you are trying to grab information from it correct (the name space is the url in xmlns="..." correct?)? Like you did here: > doc.css("uosNS|IfcCartesianPoint uosNS|Coordinates > uosNS|IfcLengthMeasure", {"uosNS" => > "http://www.iai-tech.org/ifcXML/IFC2x3/FINAL"}).each {|el| puts > el.text} In the link it says that, "Even though using namespaces is essential when searching an XML document, Nokogiri tries to help out. If there are namespaces declared on the root node of a document, Nokogiri will automatically register those for you. You will still have to use the prefix when searching the document, but the URL registration is done for you." Making both - 1 doc.xpath('//xmlns:tire', 2 'xmlns' => 'http://alicesautosupply.example.com/' 3 ) 4 doc.xpath('//xmlns:tire') Equal. I tried this using the .xml I posted and it does not work. Is this because the xmlns is not in the first line immediately following ? In turn making it necessary for every inquirary to include {"uosNS" => "http://www.iai-tech.org/ifcXML/IFC2x3/FINAL"}? >I will have to read the whole file but it may make a crucial >difference whether it does so in one go or in chunks. Large files >might not even be readable with the File.read approach. If you pass >the file as a single string there is no choice but if you pass the >File instance nokogiri can decide what to do. This is more efficient. > Note also that because of buffering small files will have just one >(or a few) IO operations anyway. I will try both and see how each performs against each other and if both work properly. Thank you both for your replies. Your help with this has been invaluable. Sincerely, Kyle -- Posted via http://www.ruby-forum.com/.