# REXML XPath bug?

**URL:** <https://www.ruby-forum.com/t/rexml-xpath-bug/137634>\
**Category:** Ruby\
**Created:** [May 14, 2008, 10:00pm UTC](https://www.ruby-forum.com/t/rexml-xpath-bug/137634 "2008-05-14T22:00:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andywatts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@andywatts](https://www.ruby-forum.com/u/andywatts)\
**Post date:** [May 14, 2008, 10:00pm UTC](https://www.ruby-forum.com/t/rexml-xpath-bug/137634/1 "2008-05-14T22:00:45Z")

</div>

I’m having trouble with a particular XPATH in REXML and would greatly  
appreciate any help.

Given this simple document…

xxx

yyy

The xpath “//div[@class=‘summary’]/following-sibling::div/span” is  
failing to return ‘yyy’.

Here’s a console dump that also shows a simplier xpath working.

> > REXML::XPath.match(doc, “//div[@class=‘summary’]/following-sibling::div/span”).to\_s  
> > =\> “”
> 
> > REXML::XPath.match(doc, “//div[@class=‘summary’]/following-sibling::div”).to\_s  
> > =\> “
> > 
> > \nyyy\n
> > ”
> 
> > puts doc.to\_s

xxx

yyy

The xpath works fine in firefox’s xpath checker, so it seems like an  
issue with rexml.  
Any help greatly appreciated.

- Andy

---

<div class="post-metadata">

**Author:** ![andywatts](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@andywatts](https://www.ruby-forum.com/u/andywatts)\
**Post date:** [May 17, 2008, 11:41am UTC](https://www.ruby-forum.com/t/rexml-xpath-bug/137634/2 "2008-05-17T11:41:05Z")

</div>

On Wed, 14 May 2008 15:00:47 -0500, Andy W. wrote:

> I’m having trouble with a particular XPATH in REXML and would greatly  
> appreciate any help.  
> (rest of it quoted below)

Ok, I tested this with JEdits XPATH tool and indeed, the xpath “//div  
[@class=‘summary’]/following-sibling::div” returns the span-element with  
string-value yyy, xml fragment yyy.

Is it a bug? I must admit I would need to read more references etc  
before  
being able to say definately - or is it just different interpretation of  
the following-sibling -axis. Dont have the time to see the rfc atm.

But I played around a bit:  
irb(main):046:0\> puts root.elements[“//div[@class=‘summary’]/following-  
sibling::div/span”]  
nil

- So, it doesnt work like that.

irb(main):047:0\> puts root.elements[“//div[@class=‘summary’]/following-  
sibling::div”]

yyy
\* hmm....

irb(main):049:0\> puts root.elements[“//div[@class=‘summary’]/  
following::div/span”]  
yyy  
\*\* Would this work in your case?

ALSO - you could include the rexml namespace by adding the line  
include REXML  
after the rexml requires. This way you wont have to use the long ::-  
syntax, but can refer to document or elements directly, and use xpaths  
simply as above inside [“”].

I also played around with this [http://www.zvon.org:9001/saxon/cgi-bin/](http://www.zvon.org:9001/saxon/cgi-bin/)  
XLab/XML/xpatut\_15.html and following seems to accomplish what you need.

Finally, just plain-jane /div/span would select the span but maybe there  
can be many and you only want the one after the summary div.

Out of time, gtg

Casimir P.
