Ruby, libxml receiving child nodes matching xpath

I need to get the child nodes of a node using XPath, since I want to "expand" into a node. Here is 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 is the 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 search jobs find and return the Books node. However, the second find ('// Document: Title') returns all Title nodes in the document, although I am only looking in the found nodes.

Why is this going to happen? I'm tired of modifying the second XPath, but I'm not tired of anything. Any tips?

+3
source share
2 answers

xml_document . , , "" node.

, XPath , " ": // ( " " ) , :

book_title_xpath = 'Document:Title'

"". node, :

book_title_xpath = './/Document:Title'

. " node node ".

+3

book_title_xpath = 'Title'

0
source

Source: https://habr.com/ru/post/1721030/


All Articles