ノードに「ドリルダウン」したいので、XPathを使用してノードの子ノードを取得する必要があります。これが私が試しているコードです:
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()
}
}
そしてこれがXMLフラグメントです
<Document xmlns="http://www.google.com/books/">
<View>
<Books>
<Title>pragmatic programming</Title>
</Books>
<Comics>
<Title>x-men</Title>
</Comics>
</View>
</Document>
最初の検索は、Booksノードを検索して返します。ただし、2番目のfind('// Document:Title')は、見つかったノードのみを検索している場合でも、ドキュメント内のすべてのTitleノードを返します。
なぜこれが起こるのでしょうか?2番目のXPathを変更するのに疲れましたが、疲れたものは何も機能しません。何かアドバイス?