我需要使用XPath節點的子節點,因爲我想「鑽」到一個節點的子節點。下面是我想要的代碼:紅寶石,libxml的獲得匹配的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>
先找工作找到並返回書節點。然而,第二個find(「//文件:標題」)返回所有標題節點的文件即使我只是在找到的節點搜索。
爲什麼會發生這種情況?我已經厭倦修改第二個XPath了,但是我已經厭倦了。有什麼建議?
THX,以爲我已經試過了,但我想我沒有。 – 2009-10-26 13:13:55