Forum: Ruby REXML and XPath

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mark V. (Guest)
on 2006-01-27 04:07
(Received via mailing list)
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?
Mark V. (Guest)
on 2006-01-27 04:19
(Received via mailing list)
On 1/26/06, Mark V. <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)')
Adam K. (Guest)
on 2006-01-27 23:57
(Received via mailing list)
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.
Mark V. (Guest)
on 2006-01-28 15:53
(Received via mailing list)
On 1/27/06, Adam K. <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.
This topic is locked and can not be replied to.