From: gavin@...
Date: 2005-02-10T02:25:06+09:00
Subject: REXML XPath not iterating in Source Order
I'm using REXML with XPath queries to parse a (horrific, ugly, nast)
XHTML document. Unfortunately, the nodes traversed in many cases are
not in the same order as they appear in the document, twiddling
paragraphs (and worse).
For example, following is a piece of Ruby code, the snippet of the
XHTML it's operating on, and the debug output. (I've trimmed the debug
output slightly to be clear.) Note that the lines are not in the
correct order:
RUBY:
puts "Looking inside #{tds[1].to_s.inspect}"
tds[1].elements.each( ".//*[@class='pCellBody']" ){ |line|
puts "Found line #{line.to_s.inspect}"
}
XHTML:
Specifies whether or not the event will bubble up the object model
hierarchy.
0 = Bubbling enabled. This is the default setting.
1 = Bubbling disabled.
For more information about event bubbling, see Working with mouse events and event
bubbling.
|
OUTPUT:
Looking inside " \nSpecifies whether ... | "
Found line "\nSpecifies whether ... \n
"
Found line "\nFor more information ...
"
Found line "\n0 = Bubbling enabled. ...
\n
"
Found line "\n1 = Bubbling disabled. \n
"
Unfortunately, I can't reproduce this problem in a simple test case.
For example, the following code behaves as expected:
require 'rexml/document'
doc = REXML::Document.new( <<-ENDXML
Line 1
Line 2
Line 3
Line 4
ENDXML
)
doc.elements.each( ".//*[@class='foo' or @class='bar']" ) do |el|
puts el.to_s
end
doc.elements.each( "//b" ) do |el|
puts el.to_s
end
div = REXML::XPath.first( doc, "//*[@class='subsection']" )
div.elements.each( './*[@class="foo" or @class="bar"]' ) do |el|
puts el.to_s
end
OUTPUT:
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 3
4
Line 5
6
Line 5
Line 6
Line 7
Has anyone experienced this not-in-order behavior before? Any ideas how
to prevent it?