4

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>
5
  • <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'.....
    – noesgard
    Jan 18, 2010 at 12:05
  • 1
    @noesgard: You can edit your question to add extra detail if you wish. Code doesn't show up well in comments unfortunately. :-)
    – Alex Angas
    Jan 19, 2010 at 22:51
  • Thx for the comment will do that
    – noesgard
    Jan 20, 2010 at 6:44
  • @noesgard: Thanks for coming back and updating with the answer! To clarify my previous comment... It's great to keep updating your question with things you've tried. However answers such as your latest edit should still be added as answer - or else there's no way you can mark it as the answer. :-) Does that make more sense?
    – Alex Angas
    Jan 20, 2010 at 23:09
  • It does, but I don't want to accept my own solution. I got some good inspiration here and want to acknowlegde that here.
    – noesgard
    Jan 21, 2010 at 7:02

2 Answers 2

1

Check out this Number with decimals, shows as 'NaN' in DataFormWebPart

1
  • A variation of this link solved the problem (see my Updates in the question) Therefore this will be the accepted answer.
    – noesgard
    Jan 21, 2010 at 7:04
3

This should work:

<xsl:value-of select="sum($Rows[string-length(@Distance) &gt; 0]/@Distance)" />

M.

5
  • Still gets NaN on that one :o/
    – noesgard
    Jan 18, 2010 at 7:17
  • Think I might have a clue to the cause: The locale is danish and therefore e,g, 3.5 is written: 3,5. SharePoint can handle this and calculates the sum correctly, but 3,5 is not recognized as a number in the xslt... Any ideas how to fix that? (maybe a string replace in the calculation and then again in the representation of the result if possible?)
    – noesgard
    Jan 18, 2010 at 7:25
  • Does not handle negative numbers either...
    – noesgard
    Jan 18, 2010 at 9:06
  • 1
    Ah, locale joys. You can try the translate function to replace the comma with a period: translate(@Distance, ',', '.'). Is the decimal format line you show above something you added? If not, then removing it might do the trick. Jan 18, 2010 at 15:12
  • And yes the decimal format was something I added in an attempt to solve the problem
    – noesgard
    Jan 19, 2010 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.