Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > REXML bug - descendant-or-self selector

Reply
Thread Tools

REXML bug - descendant-or-self selector

 
 
Gavin Kistner
Guest
Posts: n/a
 
      09-04-2005
In XPath, "//" is an abbreviation for the "descendant-or-self"
selector. My understanding (and Microsoft's XPath implementation) of
this selector means that the following code should select the bar
element as well as the descendants.

[Sliver:~/Desktop] gkistner$ cat tmp.rb
xml = <<ENDXML
<foo id="foo">
<bar id="bar">
<jim id="jim" />
<jam id="jam" />
<bork><whee id="whee" /></bork>
</bar>
</foo>
ENDXML

require 'rexml/document'
include REXML
bar = XPath.first( Document.new( xml ), '//bar' )
bar.each_element( './/[@id]' ){ |element_with_id|
puts element_with_id
}


[Sliver:~/Desktop] gkistner$ ruby -v tmp.rb
ruby 1.8.2 (2004-12-25) [powerpc-darwin7.7.2]
<jim id='jim'/>
<jam id='jam'/>
<whee id='whee'/>



 
Reply With Quote
 
 
 
 
Gavin Kistner
Guest
Posts: n/a
 
      09-05-2005
On Sep 3, 2005, at 11:51 PM, Gavin Kistner wrote:
> In XPath, "//" is an abbreviation for the "descendant-or-self"
> selector. My understanding (and Microsoft's XPath implementation)
> of this selector means that the following code should select the
> bar element as well as the descendants.
>
> [Sliver:~/Desktop] gkistner$ cat tmp.rb
> xml = <<ENDXML
> <foo id="foo">
> <bar id="bar">
> <jim id="jim" />
> <jam id="jam" />
> <bork><whee id="whee" /></bork>
> </bar>
> </foo>
> ENDXML
>
> require 'rexml/document'
> include REXML
> bar = XPath.first( Document.new( xml ), '//bar' )
> bar.each_element( './/*[@id]' ){ |element_with_id|
> puts element_with_id
> }


To set the record straight:
REXML is correct. My claim above (and what I believe is Microsoft's
implementation) is incorrect.

The XPath expression:
.//*[@id]
is XPath short-hand for:
./descendant-or-self::node()/child::*[attribute::id]

The implicit child:: axis is what causes the standard short-hand
usage of // to act truly like a descendant selector. If you wanted
the behavior that I describe above, then the legal XPath expression
(which REXML supports) would be:
.//self::*[@id]

Way to go Sean! Thanks for setting me straight. (I filed a bug
report, and he kindly explained why I was wrong. I researched the
matter with other web-based XPath tools[1] and confirmed my
incorrectness.)

[1] Such as the oh-so-useful http://www.zvon.org:9001/saxon/cgi-bin/
XLab/XML/extras.html?stylesheetFile=XSLT%2FxpathAxes.xslt&v alue=%2FAAA
%2FBBB%2F%2F*%5B%40id%5D


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
REXML::Element.write is deprecated. See REXML::Formatters Phlip Ruby 0 01-15-2008 08:23 PM
Selector bug? Andersen Java 3 02-07-2006 04:35 AM
rexml error - REXML::Validation Daniel Berger Ruby 2 10-12-2004 04:19 PM
[BUG] REXML 2.7.1 External Entity Parsing Paul Duncan Ruby 0 09-05-2003 11:40 AM
soap4r 1.4.8.1 with REXML 2.7.1 - no REXML::VERSION_MAJOR Damphyr Ruby 2 07-16-2003 09:49 AM



Advertisments