On Wed, Apr 20, 2011 at 9:27 AM, Kyle X. <haebooty / yahoo.com> wrote:
> Hello, I have beenhaving trouble trying to transform some REXML to
> Nokogiri, and from my reading online I cannot find the proper way to
> write it using Nokogiri. =A0Here are the two examples I am having trouble
> with:
>
> Case 1)
> <IfcWallStandardCase id=3D"i17855">
> =A0<Representation>
> =A0 =A0<IfcProductDefinitionShape id=3D"i17925">
> =A0 =A0 =A0<Representations id=3D"i17928" exp:cType=3D"list">
> =A0 =A0 =A0 =A0<IfcShapeRepresentation exp:pos=3D"0" xsi:nil=3D"true"
> ref=3D"i17886"/>
> =A0 =A0 =A0 =A0<IfcShapeRepresentation exp:pos=3D"1" xsi:nil=3D"true"
> ref=3D"i17919"/>
> =A0 =A0 =A0</Representations>
> =A0 =A0</IfcProductDefinitionShape>
> =A0</Representation>
> </IfcWallStandardCase>
>
> I am trying to get the reference for exp:pos=3D"1", and I had this workin=
g
> with using REXML with the following -
>
> XPath.match( $doc, "//IfcWallStandardCase//*[@pos=3D'1']" )
>
> With nokogiri I can get it to read both pos 0 and 1, using .css and
> .xpath-
>
> $doc_noko.css("uosNS|IfcWallStandardCase uosNS|IfcShapeRepresentation",
> {"uosNS" =3D> $http})
> and
> $doc_noko.xpath("//uosNS:IfcWallStandardCase//uosNS:IfcShapeRepresentatio=
n",
> {"uosNS" =3D> $http})
>
> But cannot figure out how to get it to read only pos=3D1 using either
> method and continuously get error or nil.

The XPath seems to work in Nokogiri as well:

irb(main):017:0> puts(doc.xpath "//IfcWallStandardCase//*[@pos=3D'1']")
<IfcShapeRepresentation pos=3D"1" nil=3D"true" ref=3D"i17919"/>
=3D> nil

> Case 2)
> <IfcDirection id=3D"i1574">
> =A0<DirectionRatios id=3D"i1577" exp:cType=3D"list">
> =A0 =A0<exp:double-wrapper pos=3D"0">1.</exp:double-wrapper>
> =A0 =A0<exp:double-wrapper pos=3D"1">0.</exp:double-wrapper>
> =A0 =A0<exp:double-wrapper pos=3D"2">0.</exp:double-wrapper>
> =A0</DirectionRatios>
> </IfcDirection>
>
> The issue I am having here is that I am reading this with Nokogiri using
> .xpath and the colon in exp:double is giving me trouble since the xpath
> is written -
>
> ref =3D "i1574"
> $doc_noko.xpath("//uosNS:*[@id=3D'#{ref}']//uosNS:exp:double-wrapper",
> {"uosNS" =3D> $http}).map {|element| element.text}
>
> I am guessing that it would be easier to use .css here rather than
> .xpath. =A0So I have tried using it but cannot seem to get it correct.
> Trying -
>
> ref =3D "i1574"
> $doc_noko.css("uosNS|#{ref} uosNS|exp:double-wrapper", {"uosNS" =3D>
> $http}).map {|element| element.text}
>
> I read in a previous post that you call the ref using #{} for css,
> but this returns nil for me.

When I define a namespace mapping and remove the namespace prefix from
the wildcard it works as expected:

irb(main):042:0> doc=3DNokogiri.XML(<<XML)
irb(main):043:1" <IfcDirection xmlns:exp=3D"foo" id=3D"i1574">
irb(main):044:1"  <DirectionRatios id=3D"i1577" exp:cType=3D"list">
irb(main):045:1"    <exp:double-wrapper pos=3D"0">1.</exp:double-wrapper>
irb(main):046:1"    <exp:double-wrapper pos=3D"1">0.</exp:double-wrapper>
irb(main):047:1"    <exp:double-wrapper pos=3D"2">0.</exp:double-wrapper>
irb(main):048:1"  </DirectionRatios>
irb(main):049:1" </IfcDirection>
irb(main):050:1" XML

Note: "exp" is mapped to "foo".

irb(main):052:0> ref
=3D> "i1574"

irb(main):054:0> puts
doc.xpath("//*[@id=3D'#{ref}']//ns1:double-wrapper", {"ns1" =3D> "foo"})
<exp:double-wrapper pos=3D"0">1.</exp:double-wrapper>
<exp:double-wrapper pos=3D"1">0.</exp:double-wrapper>
<exp:double-wrapper pos=3D"2">0.</exp:double-wrapper>
=3D> nil

With ns prefix:

irb(main):055:0> puts
doc.xpath("//ns1:*[@id=3D'#{ref}']//ns1:double-wrapper", {"ns1" =3D>
"foo"})
=3D> nil

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/