Wednesday, September 14, 2011

Oh XPATH

you are so cool.


require 'rubygems'
gem 'nokogiri'
require 'nokogiri'

xml = <<-EOXML
<doc>
<p>
<name>Pippo</name>
<com>
<type>phone</type>
<value>12345</value>
</com>
<com>
<type>fax</type>
<value>67890</value>
</com>
</p>
</doc>
EOXML

doc = Nokogiri::XML(xml)
ps = doc.xpath('//p')

@p = ps.first
def take field
@p.at_xpath("./com[type[text()='#{field}']]/value").text
end

puts "phone: #{take :phone}"
puts "fax: #{take :fax}"






$ ruby nokogiri-xpath.rb
phone: 12345
fax: 67890

2 comments:

  1. You see, within the earlier years, the ball track had lips where the ball would roll before selecting the diamonds when much of its velocity as been lost. At the time, gamers would predict where the ball would land from the track lips. If you’re seeking to turn out to be an online 온라인 카지노 roulette pro, you’re in the proper place. Our experts are here to tell you every thing want to|you should|you have to} know – from the essential rules of roulette to high strategy tips to win. Casino Rodos presents thirteen single-zero American Roulette tables, with betting limits starting from €5 minimum {up to|as much as} €500 ‘straight up’ on a quantity. Drake'snew $620,000 Jacob & Co watch does much more than simply tell the time — it includes a fully functional roulette wheel.

    ReplyDelete