public
Created

Solutions for ruby-talk 387408

  • Download Gist
name-value-validate.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#!/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>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.