From: Robert Klemme Date: 2011-04-20T16:46:44+09:00 Subject: Re: Nokogiri parsing question On Wed, Apr 20, 2011 at 9:27 AM, Kyle X. 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.  Here are the two examples I am having trouble > with: > > Case 1) > >   >     >       >         ref="i17886"/> >         ref="i17919"/> >       >     >   > > > 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']") => nil > Case 2) > >   >    1. >    0. >    0. >   > > > 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(< irb(main):044:1" irb(main):045:1" 1. irb(main):046:1" 0. irb(main):047:1" 0. 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"}) 1. 0. 0. => nil With ns prefix: irb(main):055:0> puts doc.xpath("//ns1:*[@id='#{ref}']//ns1:double-wrapper", {"ns1" => "foo"}) => nil Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/