# REXML and XPath

**URL:** <https://www.ruby-forum.com/t/rexml-and-xpath/53166>\
**Category:** Ruby\
**Created:** [January 27, 2006, 3:07am UTC](https://www.ruby-forum.com/t/rexml-and-xpath/53166 "2006-01-27T03:07:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 27, 2006, 3:07am UTC](https://www.ruby-forum.com/t/rexml-and-xpath/53166/1 "2006-01-27T03:07:19Z")

</div>

The documentation for REXML says is has full XPath 1.0 support.  
However, I can’t see a way to get the XPath class to return values  
other than nodes. For example, if I have an XML document that contains  
“book” elements, I should be able to do something like this.

count = XPath.evaluate(doc, count(’//book’))

There is no “evaluate” method and the “first” and “match” methods don’t  
do this.

Is this supported?

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 27, 2006, 3:19am UTC](https://www.ruby-forum.com/t/rexml-and-xpath/53166/2 "2006-01-27T03:19:22Z")

</div>

On 1/26/06, Mark V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The documentation for REXML says is has full XPath 1.0 support.  
> However, I can’t see a way to get the XPath class to return values  
> other than nodes. For example, if I have an XML document that contains  
> “book” elements, I should be able to do something like this.
> 
> count = XPath.evaluate(doc, count(‘//book’))

Minor syntax correction here,

count = XPath.evaluate(doc, ‘count(//book)’)

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 27, 2006, 10:57pm UTC](https://www.ruby-forum.com/t/rexml-and-xpath/53166/3 "2006-01-27T22:57:07Z")

</div>

On Jan 26, 2006, at 8:18 PM, Mark V. wrote:

> count = XPath.evaluate(doc, ‘count(//book)’)

As a work-around, you could try this:

count = XPath.match(doc, ‘//book’).length

REXML may just support XPath’s selection syntax and not the pseudo-  
function-things like you’re trying to use above.

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 28, 2006, 2:53pm UTC](https://www.ruby-forum.com/t/rexml-and-xpath/53166/4 "2006-01-28T14:53:07Z")

</div>

On 1/27/06, Adam K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Minor syntax correction here,
> > 
> > count = XPath.evaluate(doc, ‘count(//book)’)
> 
> As a work-around, you could try this:
> 
> count = XPath.match(doc, ‘//book’).length
> 
> REXML may just support XPath’s selection syntax and not the pseudo-  
> function-things like you’re trying to use above.

I found out that it does support what I wanted which is XPath  
expressions that result in a number, string or boolean. You need to  
use the XPath.first method for those.
