I need to get the child nodes of a node using XPath as I want to "drill down" into a node. Here's the code I'm trying:
xml_ns = 'Document:http://www.google.com/books/'
xml_document = XML::Document.file('./test_pages/test.xml')
book_xpath = '//Document:View/Document:Books'
book_title_xpath = '//Document:Title'
xml_document.find(book_xpath, xml_ns).each() { | item |
puts item
item.find(book_title_xpath, xml_ns).each() { |item2|
puts '========================'
puts 'name: ' + item2.content().strip()
}
}
And here's an XML fragment
<Document xmlns="http://www.google.com/books/">
<View>
<Books>
<Title>pragmatic programming</Title>
</Books>
<Comics>
<Title>x-men</Title>
</Comics>
</View>
</Document>
The first find works find and returns the Books node. However the second find('//Document:Title') return all the Title nodes in the document even though I am only searching in the found nodes.
Why would this happen? I've tired modifying the second XPath, but nothing I've tired works. Any advice?