简体   繁体   中英

Getting the siblings of a node with Nokogiri

Is there a way to find a specific value in a node and then return all its sibling values?

For example, I would like to find find the id node that contains ID 5678 and then get the email address and all images associated with ID 5678.

Nokogiri::XML.parse(File.open('info.xml'))

Here's a sample XML file.

<xmlcontainer>
  <details>
    <id>1234</id>
    <email>sdfsdf@sdasd.com</email>
    <image>images/1.jpg</image>
    <image>images/2.jpg</image>
    <image>images/3.jpg</image>
  </details>
  <details>
    <id>5678</id>   
    <email>zzzz@zzz.com</email>
    <image>images/4.jpg</image>
    <image>images/5.jpg</image>
  </details>
  <details>
    <id>9011</id>   
    <email>aaaa@aaa.com</email>
    <image>images/6.jpg</image>
    <image>images/7.jpg</image>
  </details>
</xmlcontainer>

You can use ~ , which is the css general sibling selector:

doc.search('id[text()="5678"] ~ *').map &:text
#=> ["zzzz@zzz.com", "images/4.jpg", "images/5.jpg"]

It's a little bit weird to use css with xml but it's so much easier to look at (than xpath).

require 'nokogiri'
doc = Nokogiri::XML.parse(File.open('info.xml'))
details = doc.css('details').find{|node| node.css('id').text == "5678"}
email = details.css('email').text # => "zzzz@zzz.com"
images = details.css('image').map(&:text) # => ["images/4.jpg", "images/5.jpg"]

Update: There are shorter, arguably better, ways to grab the details node you want:

details = doc.at('details:has(id[text()="5678"])')

or

details = doc.search('id[text()="5678"] ~ *')

Those are both courtesy of pguardiario.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM