From: "Jesús Gabriel y Galán" Date: 2011-04-11T16:37:41+09:00 Subject: Re: Can you search in REXML by attributes? On Mon, Apr 11, 2011 at 1:27 AM, Kyle X. wrote: > Kyle X. wrote in post #992033: >> "Jesús Gabriel y Galán" wrote in post >> #990433: >>> >>> I usually use Nokogiri to handle XML documents, and find css selectors >>> easier than XPath. I'd do it like this: >>> >>> doc = Nokogiri::XML(<>> >>> >>>   >>>     >>>   >>> >>> >>> >>>   >>>     >>>   >>> >>> >>> >>>   >>>    117.4 >>>    119.7 >>>    0. >>>   >>> >>> >>> END >>> ) >>> >>> reference = doc.css("#i1671 Location >>> IfcCartesianPoint").attribute("ref").value >>> doc.css("##{reference} Coordinates IfcLengthMeasure").map {|element| >>> element.text} >>> >>> This returns: => ["117.4", "119.7", "0."] > > Please disregard the previous post.  I figured out how to load the > files, but it does not appear to be reading correctly. > > Using- > fname = File.open("C:/Users/Kyle/Desktop/CSUF/Research Winter > 11/IFXCML/automation trials/one.xml") > $doc = Nokogiri::XML(fname) > reference = $doc.css(" IfcCartesianPoint Coordinates > IfcLengthMeasure").first > > This produce and output of nil when it should be "117.4" correct?  What > is going wrong here? > This works for me: $ cat noko.rb && ruby -rubygems noko.rb require 'nokogiri' doc = Nokogiri::XML(File.read("one.xml")) reference = doc.css("IfcCartesianPoint Coordinates IfcLengthMeasure").first puts reference.text 117.4 But if I leave a space before IfcCartesianPoint in the call to the css method I get a parser error (`on_error': unexpected ' ' after '' (Nokogiri::CSS::SyntaxError)). This is my file one.xml: 117.4 119.7 0. This works too: doc.css("IfcCartesianPoint Coordinates IfcLengthMeasure").each {|el| puts el.text} Produces: 117.4 119.7 0. Maybe it's the way you are passing the file to Nokogiri::XML? By the way, in your way you are not closing the file handler. If you want to pass Nokogiri the file instead of reading it yourself you can do: doc = nil File.open("one.xml") {|f| doc = Nokogiri::XML(f)} or doc = File.open("one.xml") {|f| Nokogiri::XML(f)} This way, the file is properly closed. Jesus.