From: Robert Klemme Date: 2011-09-05T23:30:29+09:00 Subject: Re: Ruby and XML Gavin Kistner wrote in post #1020210: > On Sep 5, 2011, at 1:28 AM, 7stud -- wrote: >> puts "No tag that is a sibling" >> end >> >> puts "-" * 20 >> end > > For comparison, here's one way to write code with the similar > functionality using Nokogiri, but ensuring that the element is a > sibling of the element. Unlike the above code, the following > allows any number of elements between the and the : > require 'nokogiri' > > doc = Nokogiri::XML(IO.read("my.xml")) > doc.search('name').each do |name| > puts "name: #{name.text}" > if value=name.at_xpath('following-sibling::value') > puts value.text > else > puts "No tag that is a sibling" > end > puts "-" * 20 > end You can even use an XPath expression to find names which do not have proper values: //name[not(following-sibling::value)] //name[following-sibling::*[1][name()!="value"]] //name[count(following-sibling::value)!=1] //name[following-sibling::*[1][name()!="value"]]|//name[count(following-sibling::value)!=1] attached and also here: https://gist.github.com/1195114 Kind regards robert Attachments: http://www.ruby-forum.com/attachment/6578/nv.rb -- Posted via http://www.ruby-forum.com/.