简体   繁体   中英

Using fn:sum in XSLT with node-set containing null-values

I'm trying to sum up a set of values in an XML using XSLT and XPath function fn:sum. This works fine as long as the values are non-null, however this is not the case. To illustrate my problem I've made an example:

<?xml version="1.0"?>

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fn="http://www.w3.org/2005/xpath-functions">

  <xsl:template match="/root">
    <root>
      <!-- Works fine for non-null values -->
      <sum><xsl:value-of select="fn:sum(values/value)" /></sum>
    </root>
  </xsl:template>
</xsl:stylesheet>

and the XML:

<?xml version="1.0"?>
<root>
  <values>
    <value>1</value>
    <value>2</value>
    <value>3</value>
    <value>4</value>
    <!-- Nullvalue -->
    <value />
  </values>
</root>

The example works fine as long as there's no null-values. I've tried various variants of the select, such as <xsl:value-of select="fn:sum(values[value != '']/value)" /> (as you might notice, not much of an XSLT export ;) ) How can I filter out the null-values?

Explicitly test that the nodes have content:

<sum><xsl:value-of select="fn:sum(values/value[text()])" /></sum>

I think that what you mentioned:

<xsl:value-of select="fn:sum(values[value != '']/value)" /> 

does not work, because the node is empty - it does not contain a text node at all, whereas value != '' tests for an empty string - that is, a text node having data of length 0.

To sum just the elements that contain numbers:

<sum>
    <xsl:value-of select="fn:sum(values/value[number(.)=number(.)])" />
</sum>

The result of number() will be NaN for empty elements, or elements whose string value is not a number.

MSDN reference - http://msdn.microsoft.com/en-us/library/ms256211.aspx

http://www.stylusstudio.com/xsllist/199909/post60110.html

XML:

<?xml version="1.0"?>
<doc>
    <num>1</num>
    <num>7</num>
    <notnum>hello world</notnum>
    <num>4</num>
</doc>

XSLT:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0">
    <xsl:template match="/">
        <xsl:value-of select="sum(/doc/num)"/>
    </xsl:template>
</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM