REXML and XPath

M

Mark Volkmann

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 =3D XPath.evaluate(doc, count('//book'))

There is no "evaluate" method and the "first" and "match" methods don't do =
this.

Is this supported?
 
M

Mark Volkmann

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 =3D XPath.evaluate(doc, count('//book'))

Minor syntax correction here,

count =3D XPath.evaluate(doc, 'count(//book)')
 
U

Une bévue

Mark Volkmann said:
Minor syntax correction here,

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

does we have to require something else than :
require 'rexml/document'
include REXML


because i get :
NoMethodError: undefined method `evaluate' for REXML::XPath:Class
 
A

Adam Keys

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.
 
M

Mark Volkmann

As a work-around, you could try this:

count =3D 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top