I have this (an attempt to solve the task):
<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal">
<xsl:output method="html" indent="no"/>
<xsl:decimal-format NaN=""/>
<xsl:template match="/">
<xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row"/>
<xsl:value-of select="sum($Rows/@Distance[.!=''])" />
</xsl:template>
</xsl:stylesheet>
The result is: "NaN", so I figure that my XSLT is wrong.
Question is: How do I write the XSLT to calculate and show the sum of all values in the "Distance" column in a Data View Web Part? The column might have empty values (does not have empty values now by the way) that should be ignored calculating the sum.
Update 1:
<xsl:decimal-format grouping-separator="." decimal-separator="," NaN="" name="european"/>
<xsl:template match="/">
Test:
<xsl:copy-of
select="format-number(sum(for ($i in /dsQueryResponse/Rows/Row/@Distance) { return number(replace($i, ',','.')})), '#,##', 'european')" />
Tried this, but it fails (Failed setting processor stylesheet. expected token ')' found 'NAME'.
Update 2:
<xsl:template match="/">
xsum //Distance = <xsl:call-template name="xsum"><xsl:with-param name="currnode" select="//row/@Distance[1]"/></xsl:call-template>
<xsl:template name="xsum">
<xsl:param name="currnode"/>
<xsl:param name="res" select="0"/>
<xsl:choose>
<xsl:when test="$currnode">
<xsl:call-template name="xsum">
<xsl:with-param name="currnode" select="$currnode/following-sibling::tal[1]"/>
<xsl:with-param name="res" select="$res + translate($currnode,',','.')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="translate($res,'.',',')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
I've got the XSLT above and it does provide output but with the following values in the Distance Column:
2 3,5 -1,3 0
I get a result different from NaN but not the sum, only the first value as far as I can see. The result is: xsum //Distance = 2.
Update 3:
This works. Had some problems with the selection of the start node but that should be fixed now.
<xsl:decimal-format name="da-DK" decimal-separator="," grouping-separator="." minus-sign="-" NaN="Ikke tal"/>
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:text>Distance tilbagelagt = </xsl:text>
<xsl:call-template name="xsum">
<xsl:with-param name="currnode" select="//Row[1]"/>
<xsl:with-param name="attr-type" select="'Distance'"/>
<xsl:with-param name="res" select="0"/>
</xsl:call-template> km
<xsl:template name="xsum">
<xsl:param name="currnode"/>
<xsl:param name="attr-type"/>
<xsl:param name="res" select="0"/>
<xsl:choose>
<xsl:when test="$currnode/following-sibling::Row">
<xsl:call-template name="xsum">
<xsl:with-param name="currnode" select="$currnode/following-sibling::Row[1]"/>
<xsl:with-param name="attr-type" select="$attr-type"/>
<xsl:with-param name="res" select="$res + number(translate($currnode/@*[name() = $attr-type],',','.'))"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number($res + number(translate($currnode/@*[name() = $attr-type],',','.')), '#,##','da-DK')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>