On Mon, Apr 11, 2011 at 1:27 AM, Kyle X. <haebooty / yahoo.com> wrote:
> Kyle X. wrote in post #992033:
>> "Jes=FAs Gabriel y Gal=E1n" <jgabrielygalan / gmail.com> 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 =3D Nokogiri::XML(<<END
>>> <root xmlns:exp=3D"http://foo" xmlns:xsi=3D"http://bar">
>>> <IfcWallStandardCase id=3D"i1677">
>>> =A0<ObjectPlacement>
>>> =A0 =A0<IfcLocalPlacement xsi:nil=3D"true" ref=3D"i1671"/>
>>> =A0</ObjectPlacement>
>>> </IfcWallStandardCase>
>>>
>>> <IfcAxis2Placement3D id=3D"i1671">
>>> =A0<Location>
>>> =A0 =A0<IfcCartesianPoint xsi:nil=3D"true" ref=3D"i1667"/>
>>> =A0</Location>
>>> </IfcAxis2Placement3D>
>>>
>>> <IfcCartesianPoint id=3D"i1667">
>>> =A0<Coordinates id=3D"i1670" exp:cType=3D"list">
>>> =A0 =A0<IfcLengthMeasure exp:pos=3D"0">117.4</IfcLengthMeasure>
>>> =A0 =A0<IfcLengthMeasure exp:pos=3D"1">119.7</IfcLengthMeasure>
>>> =A0 =A0<IfcLengthMeasure exp:pos=3D"2">0.</IfcLengthMeasure>
>>> =A0</Coordinates>
>>> </IfcCartesianPoint>
>>> </root>
>>> END
>>> )
>>>
>>> reference =3D doc.css("#i1671 Location
>>> IfcCartesianPoint").attribute("ref").value
>>> doc.css("##{reference} Coordinates IfcLengthMeasure").map {|element|
>>> element.text}
>>>
>>> This returns: =3D> ["117.4", "119.7", "0."]
>
> Please disregard the previous post. =A0I figured out how to load the
> files, but it does not appear to be reading correctly.
>
> Using-
> fname =3D File.open("C:/Users/Kyle/Desktop/CSUF/Research Winter
> 11/IFXCML/automation trials/one.xml")
> $doc =3D Nokogiri::XML(fname)
> reference =3D $doc.css(" IfcCartesianPoint Coordinates
> IfcLengthMeasure").first
>
> This produce and output of nil when it should be "117.4" correct? =A0What
> is going wrong here?
>

This works for me:

$ cat noko.rb && ruby -rubygems noko.rb
require 'nokogiri'

doc =3D Nokogiri::XML(File.read("one.xml"))
reference =3D doc.css("IfcCartesianPoint Coordinates IfcLengthMeasure").fir=
st
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:

<root xmlns:exp=3D"http://foo" xmlns:xsi=3D"http://bar">
  <IfcWallStandardCase id=3D"i1677">
       <ObjectPlacement>
         <IfcLocalPlacement xsi:nil=3D"true" ref=3D"i1671"/>
      </ObjectPlacement>
  </IfcWallStandardCase>

  <IfcAxis2Placement3D id=3D"i1671">
     <Location>
       <IfcCartesianPoint xsi:nil=3D"true" ref=3D"i1667"/>
    </Location>
  </IfcAxis2Placement3D>

  <IfcCartesianPoint id=3D"i1667">
     <Coordinates id=3D"i1670" exp:cType=3D"list">
         <IfcLengthMeasure exp:pos=3D"0">117.4</IfcLengthMeasure>
         <IfcLengthMeasure exp:pos=3D"1">119.7</IfcLengthMeasure>
         <IfcLengthMeasure exp:pos=3D"2">0.</IfcLengthMeasure>
      </Coordinates>
  </IfcCartesianPoint>
</root>

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 =3D nil
File.open("one.xml") {|f| doc =3D Nokogiri::XML(f)}

or

doc =3D File.open("one.xml") {|f| Nokogiri::XML(f)}

This way, the file is properly closed.

Jesus.