[libxml-devel] doc.find and Namespace confusion

Keith Fahlgren keith at oreilly.com
Sun Jan 14 15:05:51 EST 2007


Hey,


I'm curious what I'm doing wrong in the IRB session below. I'd expect that:
   n = doc.find("*").to_a.first
   # not literal ==, just equivalent
   doc.find(n.path).to_a.first == n

Is that not supposed to be true?


Thanks,
Keith


# for the record:
/opt/local/lib/ruby/gems/1.8/gems/libxml-ruby-0.3.8.2/lib keith2$ ruby 
../tests/libxml_test.rb
VERSION: 0.3.8.2

# irb session:
$ irb --prompt xmp libxml.irb
require 'rubygems'
# => true
require 'xml/libxml'
# => true
doc = XML::Parser.string(<<EOFEED
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" 
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" 
xmlns:dc="http://purl.org/dc/elements/1.1/">
   <title>Books</title>
   <link rel="first" href="https://dummy.com/atom/oreilly/books" />
   <link rel="last" href="https://dummy.com/atom/oreilly/books?page=51" />
   <link rel="next" href="https://dummy.com/atom/oreilly/books?page=2" />
   <subtitle>Stores O'Reilly Book Content</subtitle>
   <entry>
     <title>Dummy Book</title>

     <link rel="edit" href="https://dummy.com/atom/oreilly/books/172049" />
     <link rel="edit-media" 
href="https://dummy.com/source/oreilly/books/172049" />
     <author />
     <id>tag:oreilly.com,2007:172049</id>
     <updated>2007-01-13T00:25:07Z</updated>
     <published>2007-01-13T00:25:07Z</published>
     <content type="application/docbook+xml" 
src="https://dummy.com/source/oreilly/books/172049" />

     <summary type="text" />
   </entry>
</feed>
EOFEED
).parse; nil
# => nil
doc.class
# => XML::Document
n1 = doc.find("*").to_a.first
# => <title>Books</title>
n1.path
# => "/feed/title"
n2 = doc.find(n1.path).to_a.first  # huh?
# => nil
n2 # bah!
# => nil
namespace = "http://www.w3.org/2005/Atom"
# => "http://www.w3.org/2005/Atom"
doc.find("*", namespace).each {|node|
   puts "PATH " + node.path; puts "CONTENT " + node.content[0..10]
   doc.find(node.path, namespace).each {|alt_node|
     # this'll never be run
     puts "REAL_PATH " + alt_node.path; puts "REAL CONTENT " + 
alt_node.content[0..10]
   }
}
PATH /feed/title
CONTENT Books
PATH /feed/link[1]
CONTENT
PATH /feed/link[2]
CONTENT
PATH /feed/link[3]
CONTENT
PATH /feed/subtitle
CONTENT Stores O'Re
PATH /feed/entry
CONTENT
     Dummy
# => <title>Books</title><link rel="first" 
href="https://dummy.com/atom/oreilly/books"/><link rel="last" 
href="https://dummy.com/atom/oreilly/books?page=51"/><link rel="next" 
href="https://dummy.com/atom/oreilly/books?page=2"/><subtitle>Stores 
O'Reilly Book Content</subtitle><entry>
     <title>Dummy Book</title>

     <link rel="edit" href="https://dummy.com/atom/oreilly/books/172049"/>
     <link rel="edit-media" 
href="https://dummy.com/source/oreilly/books/172049"/>
     <author/>
     <id>tag:oreilly.com,2007:172049</id>
     <updated>2007-01-13T00:25:07Z</updated>
     <published>2007-01-13T00:25:07Z</published>
     <content type="application/docbook+xml" 
src="https://dummy.com/source/oreilly/books/172049"/>

     <summary type="text"/>
   </entry>



or here: http://kfahlgren.com/code/libxml.irb






More information about the libxml-devel mailing list