Nokogiri parsing question

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. Here are the two examples I am having trouble
with:

Case 1)
<IfcWallStandardCase id="i17855">
  <Representation>
    <IfcProductDefinitionShape id="i17925">
      <Representations id="i17928" exp:cType="list">
        <IfcShapeRepresentation exp:pos="0" xsi:nil="true"
ref="i17886"/>
        <IfcShapeRepresentation exp:pos="1" xsi:nil="true"
ref="i17919"/>
      </Representations>
    </IfcProductDefinitionShape>
  </Representation>
</IfcWallStandardCase>

I am trying to get the reference for exp:pos="1", and I had this working
with using REXML with the following -

XPath.match( $doc, "//IfcWallStandardCase//*[@pos='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" => $http})
and
$doc_noko.xpath("//uosNS:IfcWallStandardCase//uosNS:IfcShapeRepresentation",
{"uosNS" => $http})

But cannot figure out how to get it to read only pos=1 using either
method and continuously get error or nil.

Case 2)
<IfcDirection id="i1574">
  <DirectionRatios id="i1577" exp:cType="list">
    <exp:double-wrapper pos="0">1.</exp:double-wrapper>
    <exp:double-wrapper pos="1">0.</exp:double-wrapper>
    <exp:double-wrapper pos="2">0.</exp:double-wrapper>
  </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 = "i1574"
$doc_noko.xpath("//uosNS:*[@id='#{ref}']//uosNS:exp:double-wrapper",
{"uosNS" => $http}).map {|element| element.text}

I am guessing that it would be easier to use .css here rather than
.xpath. So I have tried using it but cannot seem to get it correct.
Trying -

ref = "i1574"
$doc_noko.css("uosNS|#{ref} uosNS|exp:double-wrapper", {"uosNS" =>
$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.

Any ideas?

···

--
Posted via http://www.ruby-forum.com/.

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. Here are the two examples I am having trouble
with:

Case 1)
<IfcWallStandardCase id="i17855">
<Representation>
<IfcProductDefinitionShape id="i17925">
<Representations id="i17928" exp:cType="list">
<IfcShapeRepresentation exp:pos="0" xsi:nil="true"
ref="i17886"/>
<IfcShapeRepresentation exp:pos="1" xsi:nil="true"
ref="i17919"/>
</Representations>
</IfcProductDefinitionShape>
</Representation>
</IfcWallStandardCase>

I am trying to get the reference for exp:pos="1", and I had this working
with using REXML with the following -

XPath.match( $doc, "//IfcWallStandardCase//*[@pos='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" => $http})
and
$doc_noko.xpath("//uosNS:IfcWallStandardCase//uosNS:IfcShapeRepresentation",
{"uosNS" => $http})

But cannot figure out how to get it to read only pos=1 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='1']")
<IfcShapeRepresentation pos="1" nil="true" ref="i17919"/>
=> nil

Case 2)
<IfcDirection id="i1574">
<DirectionRatios id="i1577" exp:cType="list">
<exp:double-wrapper pos="0">1.</exp:double-wrapper>
<exp:double-wrapper pos="1">0.</exp:double-wrapper>
<exp:double-wrapper pos="2">0.</exp:double-wrapper>
</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 = "i1574"
$doc_noko.xpath("//uosNS:*[@id='#{ref}']//uosNS:exp:double-wrapper",
{"uosNS" => $http}).map {|element| element.text}

I am guessing that it would be easier to use .css here rather than
.xpath. So I have tried using it but cannot seem to get it correct.
Trying -

ref = "i1574"
$doc_noko.css("uosNS|#{ref} uosNS|exp:double-wrapper", {"uosNS" =>
$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=Nokogiri.XML(<<XML)
irb(main):043:1" <IfcDirection xmlns:exp="foo" id="i1574">
irb(main):044:1" <DirectionRatios id="i1577" exp:cType="list">
irb(main):045:1" <exp:double-wrapper pos="0">1.</exp:double-wrapper>
irb(main):046:1" <exp:double-wrapper pos="1">0.</exp:double-wrapper>
irb(main):047:1" <exp:double-wrapper pos="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
=> "i1574"

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

With ns prefix:

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

Kind regards

robert

···

On Wed, Apr 20, 2011 at 9:27 AM, Kyle X. <haebooty@yahoo.com> wrote:

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

Thank you for the reply Robert; however, I cannot get it to work still.
I left out a lit bit of the xml when I wrote the first post. Both cases
have already have a namespace defined for them.

Case 1) These two lines would be added to both cases. The actual file
reads:
<doc:iso_10303_28 xmlns:exp="urn:oid:1.0.10303.28.2.1.1"
xmlns:doc="urn:oid:1.0.10303.28.2.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:oid:1.0.10303.28.2.1.1 ex.xsd" version="2.0">
  <uos id="uos_1" description="" configuration="i-ifc2x3" edo=""
xmlns="http://www.iai-tech.org/ifcXML/IFC2x3/FINAL"
xsi:schemaLocation="http://www.iai-tech.org/ifcXML/IFC2x3/FINAL
ifc2x3.xsd">
    <IfcWallStandardCase id="i17855">
      <Representation>
        <IfcProductDefinitionShape id="i17925">
          <Representations id="i17928" exp:cType="list">
            <IfcShapeRepresentation exp:pos="0" xsi:nil="true"
ref="i17886"/>
            <IfcShapeRepresentation exp:pos="1" xsi:nil="true"
ref="i17919"/>
          </Representations>
        </IfcProductDefinitionShape>
      </Representation>
    </IfcWallStandardCase>
  </uos>
</doc:iso_10303_28>

The exact code I have been trying to run is:

$http = "http://www.iai-tech.org/ifcXML/IFC2x3/FINAL"
ref = "i17855"
$doc_noko = Nokogiri::XML(File.read(filename))

x = $doc_noko.xpath("//uosNS:*[@id='#{ref}']//uosNS:*[@pos='1']",
{"uosNS" => $http})
#=> nil

It reads for "//uosNS:*[@id='#{ref}']" but the "//uosNS:*[@pos='1']"
is where it is not working as intended returning a nil value. With
these two new lines in mind any ideas for the two cases?

···

--
Posted via http://www.ruby-forum.com/.

The problem is that the attribute pos has a different namespace (exp), I
have tried a couple of ways but could not make it work

···

El 20/04/2011 17:56, "Kyle X." <haebooty@yahoo.com> escribió:

Thank you for the reply Robert; however, I cannot get it to work still.
I left out a lit bit of the xml when I wrote the first post. Both cases
have already have a namespace defined for them.

Case 1) These two lines would be added to both cases. The actual file
reads:
<doc:iso_10303_28 xmlns:exp="urn:oid:1.0.10303.28.2.1.1"
xmlns:doc="urn:oid:1.0.10303.28.2.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation="urn:oid:1.0.10303.28.2.1.1 ex.xsd" version="2.0">
<uos id="uos_1" description="" configuration="i-ifc2x3" edo=""
xmlns="http://www.iai-tech.org/ifcXML/IFC2x3/FINAL&quot;
xsi:schemaLocation="http://www.iai-tech.org/ifcXML/IFC2x3/FINAL
ifc2x3.xsd">
<IfcWallStandardCase id="i17855">
<Representation>
<IfcProductDefinitionShape id="i17925">
<Representations id="i17928" exp:cType="list">
<IfcShapeRepresentation exp:pos="0" xsi:nil="true"
ref="i17886"/>
<IfcShapeRepresentation exp:pos="1" xsi:nil="true"
ref="i17919"/>
</Representations>
</IfcProductDefinitionShape>
</Representation>
</IfcWallStandardCase>
</uos>
</doc:iso_10303_28>

The exact code I have been trying to run is:

$http = "http://www.iai-tech.org/ifcXML/IFC2x3/FINAL&quot;
ref = "i17855"
$doc_noko = Nokogiri::XML(File.read(filename))

x = $doc_noko.xpath("//uosNS:*[@id='#{ref}']//uosNS:*[@pos='1']",
{"uosNS" => $http})
#=> nil

It reads for "//uosNS:*[@id='#{ref}']" but the "//uosNS:*[@pos='1']"
is where it is not working as intended returning a nil value. With
these two new lines in mind any ideas for the two cases?

--
Posted via http://www.ruby-forum.com/\.

As I said: get rid of namespaces for wildcards. Instead, of course
you have to add the NS for the attribute. How you name your
namespaces in XPath expression doesn't really matter as long as the
_mapping_ is identical. XML namespaces are just a convenient
replacement for the mapped URI. So you need to make sure in the XPath
expression you use the same URI.

#!/bin/env ruby19

require 'nokogiri'

doc = Nokogiri.XML(DATA)

ref = "i17855"
puts doc.xpath("//*[@id='#{ref}']//*[@uosNS:pos='1']",
  {"uosNS" => "urn:oid:1.0.10303.28.2.1.1"})

__END__
<doc:iso_10303_28 xmlns:exp="urn:oid:1.0.10303.28.2.1.1"
  xmlns:doc="urn:oid:1.0.10303.28.2.1.3"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation="urn:oid:1.0.10303.28.2.1.1 ex.xsd"
  version="2.0">
<uos id="uos_1" description="" configuration="i-ifc2x3" edo=""
   xmlns="http://www.iai-tech.org/ifcXML/IFC2x3/FINAL&quot;
   xsi:schemaLocation="http://www.iai-tech.org/ifcXML/IFC2x3/FINALifc2x3.xsd&quot;&gt;
   <IfcWallStandardCase id="i17855">
     <Representation>
       <IfcProductDefinitionShape id="i17925">
         <Representations id="i17928" exp:cType="list">
           <IfcShapeRepresentation exp:pos="0" xsi:nil="true" ref="i17886"/>
           <IfcShapeRepresentation exp:pos="1" xsi:nil="true" ref="i17919"/>
         </Representations>
       </IfcProductDefinitionShape>
     </Representation>
   </IfcWallStandardCase>
</uos>
</doc:iso_10303_28>

Cheers

robert

···

On Wed, Apr 20, 2011 at 5:54 PM, Kyle X. <haebooty@yahoo.com> wrote:

Thank you for the reply Robert; however, I cannot get it to work still.
I left out a lit bit of the xml when I wrote the first post. Both cases
have already have a namespace defined for them.

Case 1) These two lines would be added to both cases. The actual file
reads:
<doc:iso_10303_28 xmlns:exp="urn:oid:1.0.10303.28.2.1.1"
xmlns:doc="urn:oid:1.0.10303.28.2.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation="urn:oid:1.0.10303.28.2.1.1 ex.xsd" version="2.0">
<uos id="uos_1" description="" configuration="i-ifc2x3" edo=""
xmlns="http://www.iai-tech.org/ifcXML/IFC2x3/FINAL&quot;
xsi:schemaLocation="http://www.iai-tech.org/ifcXML/IFC2x3/FINAL
ifc2x3.xsd">
<IfcWallStandardCase id="i17855">
<Representation>
<IfcProductDefinitionShape id="i17925">
<Representations id="i17928" exp:cType="list">
<IfcShapeRepresentation exp:pos="0" xsi:nil="true"
ref="i17886"/>
<IfcShapeRepresentation exp:pos="1" xsi:nil="true"
ref="i17919"/>
</Representations>
</IfcProductDefinitionShape>
</Representation>
</IfcWallStandardCase>
</uos>
</doc:iso_10303_28>

The exact code I have been trying to run is:

$http = "http://www.iai-tech.org/ifcXML/IFC2x3/FINAL&quot;
ref = "i17855"
$doc_noko = Nokogiri::XML(File.read(filename))

x = $doc_noko.xpath("//uosNS:*[@id='#{ref}']//uosNS:*[@pos='1']",
{"uosNS" => $http})
#=> nil

It reads for "//uosNS:*[@id='#{ref}']" but the "//uosNS:*[@pos='1']"
is where it is not working as intended returning a nil value. With
these two new lines in mind any ideas for the two cases?

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