#!/usr/bin/ruby

# example script to show a bug in REXML's xpath parsing when parentheses are used in selection criteria, 
# at least on descendant::node()
#
# Author: wiebe@halfgaar.net

require 'rexml/document'

doc = "
<html>
  <head>
    <title>test</title>
  </head>
  <body>
    <p>
      A <a rel=\"sub\" href=\"/\">link</a>.
    </p>
  </body>
</html>
"

xmldoc = REXML::Document.new(doc)

puts "The Following xpath expressions works fine..."

# Works fine without parentheses
xmldoc.elements.each("descendant::node()[local-name()='link' or local-name()='a' and @rel='sub']") do |element|
  puts "writing element: "
  puts element.to_s
  puts ""
end

puts "The following xpath expression is going to fail..."

# This gives "/usr/lib/ruby/1.9.1/rexml/xpath_parser.rb:437:in `expr': undefined method `inject' for true:TrueClass (NoMethodError)"
xmldoc.elements.each("descendant::node()[(local-name()='link' or local-name()='a') and @rel='sub']") do |element|
  puts "writing element: "
  puts element.to_s
  puts ""
end
