xpath and Expression must evaluate to a node-set

J

Jack Frillman

Below is an xpath that works. It returns all the values of the "value
attribute" of the OutputField tag:

//Satellite/multidataset/file[contains(@name, '311multi')]/Data/
DriftAnalysis[contains(@name, '311')]/Metric[@name='Detail' and
@type='Overlap']/Element[@metrictype='Overlap']/
OutputField[@name='Cleared']/@value

But when I try to sum the returned values of the "value attribute"
using the sum() function like the example below I get the error
"Expression must evaluate to a node-set". Can anyone tell me why and
how to fix it?

sum(//Satellite/multidataset/file[contains(@name, '311multi')]/Data/
DriftAnalysis[contains(@name, '311')]/Metric[@name='Detail' and
@type='Overlap']/Element[@metrictype='Overlap']/
OutputField[@name='Cleared']/@value)

And here is a snipit of the xml file that I'm zeoring in on.
 
J

Joe Kesselman

Is this in an XSLT stylesheet? Assuming it is...

XSLT 1.0 draws a distinction between "result trees" and "node sets".
Depending on how you're computing the results of that path and what
you're doing with it (you didn't give us context), you may need to put
the value through a conversion routine... which was not standardized at
that time. The semi-standardized solution, if your processor supports
the EXSLT extension library (most do, at this point) is to use the
exslt:node-set() function. There's a decent article on EXSLT at
http://www.ibm.com/developerworks/library/x-exslt.html


(This distinction between trees and sets went away in XSLT 2.0. In a 2.0
processor nodes are all either in input-document trees or Temporary
Trees, and the processing is the same for either kind of tree. So the
other solution would be to find a processor which supports XSLT
2.0/XPath 2.0 and change your stylesheet's version declaration to
specify that it wants to use these newer features.)

--
Joe Kesselman,
http://www.love-song-productions.com/people/keshlam/index.html

{} ASCII Ribbon Campaign | "may'ron DaroQbe'chugh vaj bIrIQbej" --
/\ Stamp out HTML mail! | "Put down the squeezebox & nobody gets hurt."
 
M

Martin Honnen

Jack said:
But when I try to sum the returned values of the "value attribute"
using the sum() function like the example below I get the error
"Expression must evaluate to a node-set". Can anyone tell me why and
how to fix it?

sum(//Satellite/multidataset/file[contains(@name, '311multi')]/Data/
DriftAnalysis[contains(@name, '311')]/Metric[@name='Detail' and
@type='Overlap']/Element[@metrictype='Overlap']/
OutputField[@name='Cleared']/@value)

How do you evaluate that expression? There are XPath APIs that only
support evaluating expressions that returns a node-set, but not
expressions that evaluate to a primitive value like a number, string or
boolean value. Depending on the XPath API you use you might simply need
to use a different method, for instance with .NET's XPathNavigator you
can use
http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathnavigator.evaluate.aspx
with any XPath expression while
http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathnavigator.select.aspx
only allows expression evaluating to a node-sets.
 

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,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top