From: Robert Klemme Date: 2011-04-21T16:19:44+09:00 Subject: Re: Nokogiri parsing question On Wed, Apr 20, 2011 at 5:54 PM, Kyle X. 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: > 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"> >   xmlns="http://www.iai-tech.org/ifcXML/IFC2x3/FINAL" > xsi:schemaLocation="http://www.iai-tech.org/ifcXML/IFC2x3/FINAL > ifc2x3.xsd"> >     >       >         >           >             ref="i17886"/> >             ref="i17919"/> >           >         >       >     >   > > > 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? 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__ Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/