xpath and Expression must evaluate to a node-set

Discussion in 'XML' started by Jack Frillman, Mar 16, 2011.

  1. 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.
    Jack Frillman, Mar 16, 2011
    #1
    1. Advertising

  2. 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."
    Joe Kesselman, Mar 16, 2011
    #2
    1. Advertising

  3. Jack Frillman wrote:

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

    --

    Martin Honnen
    http://msmvps.com/blogs/martin_honnen/
    Martin Honnen, Mar 17, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,375
  2. Jeff
    Replies:
    2
    Views:
    1,050
    Dimitre Novatchev
    Jul 5, 2003
  3. Alastair Cameron
    Replies:
    1
    Views:
    7,359
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  4. Tjerk Wolterink
    Replies:
    2
    Views:
    1,393
    Dimitre Novatchev
    Aug 24, 2006
  5. Jason luo
    Replies:
    3
    Views:
    511
    Peter Ammon
    Aug 19, 2004
Loading...

Share This Page