From: "Jesús Gabriel y Galán" Date: 2011-04-13T05:37:00+09:00 Subject: Re: Can you search in REXML by attributes? On Tue, Apr 12, 2011 at 9:39 PM, Kyle X. wrote: >> 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?)? Yes. > 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 version="1.0"?>?  In turn making it necessary for every inquirary to > include {"uosNS" => "http://www.iai-tech.org/ifcXML/IFC2x3/FINAL"}? Correct. The root node of your XML is the doc tag, which declares namespaces, but the uos tag has its own namespace too. nokogiri will register the ones present in the root node, as the article says. The children nodes of uos inherit the namespace declared in the uos tag, so this is what you have to use to search. I have not checked the behaviour about the automatic registering of namespaces, but reading the article this is how I understand it. Jesus.