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)









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)


<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>

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?

On Wed, Apr 20, 2011 at 9:27 AM, Kyle X. [email protected] wrote:

<IfcShapeRepresentation exp:pos=“0” xsi:nil=“true”

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’]”)

=> nil

.xpath and the colon in exp:double is giving me trouble since the xpath
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"
irb(main):044:1"
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"
irb(main):049:1"
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

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.” [email protected] 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
xsi:schemaLocation=“urn:oid:1.0.10303.28.2.1.1 ex.xsd” version=“2.0”>












</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?

On Wed, Apr 20, 2011 at 5:54 PM, Kyle X. [email protected] wrote:

xsi:schemaLocation=“urn:oid:1.0.10303.28.2.1.1 ex.xsd” version=“2.0”>
<IfcShapeRepresentation exp:pos=“1” xsi:nil=“true”
$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?

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
xsi:schemaLocation=“urn:oid:1.0.10303.28.2.1.1 ex.xsd”
version=“2.0”>












</doc:iso_10303_28>

Cheers

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs