Skip to content

@rklemme /name-value-validate.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Solutions for ruby-talk 387408
#!/bin/env ruby19
require 'pp'
require 'nokogiri'
doc = Nokogiri::XML(DATA)
doc.xpath('//name[following-sibling::value]').each do |name|
value = name.xpath('following-sibling::value[1]').first
# pp value, value.size
p [name.text, value.text]
end
puts '--------------'
doc.xpath('//name[not(following-sibling::value)]').each do |name|
p name.text
end
puts '--------------'
doc.xpath('//name[following-sibling::*[1][name()!="value"]]').each do |name|
p name.text
end
puts '--------------'
doc.xpath('//name[count(following-sibling::value)!=1]').each do |name|
p name.text
end
puts '--------------'
doc.xpath('//name[following-sibling::*[1][name()!="value"]]|//name[count(following-sibling::value)!=1]').each do |name|
p name.text
end
__END__
<?xml version="1.0"?>
<note>
<name>Tove</name>
<value>Tove's value is: 10</value>
<garbage>xxxx</garbage>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
<parent>
<name>Jani</name>
<value>Jani's value is: 20</value>
<garbage>xxxx</garbage>
</parent>
<parent>
<name>Too many</name>
<value>Wrong's value is: 30</value>
<value>Mr. Wrong</value>
<garbage>xxxx</garbage>
</parent>
<parent>
<name>Missing</name>
<garbage>xxxx</garbage>
</parent>
<parent>
<name>No sibling</name>
</parent>
<value>No sibling's value is: 30</value>
</note>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.