ruby + XPath

To document an annoying example…


require 'rexml/document'
include REXML #namespace convenience
doc = Document.new File.read("chiquita_shipments.xml")
doc.elements.each("//fruits/fruit/color") do |el|
puts el.parent if el.texts[0].to_s == "yellow"
end

Let’s say chiquita_shipments.xml looks like…

<pallet number='1'>
<fruits>
  <fruit>
    <name>banana</name>
    <color>yellow</color>
    <round>no</round>
  </fruit>
<fruits>
<pallet>

About this entry