XSLT for summing a column in a Data View Web Part

noesgard
  • XSLT for summing a column in a Data View Web Part noesgard

    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>
    

  • This should work:

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

    M.

Tags
sharepoint-designer data-view-web-part xslt
Related questions and answers
  • ://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal"&gt; &lt;xsl:output method="html" indent...:call-template name="dvt_1.header"&gt; &lt;xsl:with-param name="Rows" select="$Rows" /&gt; &lt;/xsl:call-template>; &lt;table border="0" width="100%"&gt; &lt...;xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:dsp="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http

  • confirm (or deny) that the following looks legitimate (or suggest a better way to do it)? &lt;xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint...="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"&gt; &lt;xsl:template match="FieldRef[@FieldType='DataLinkUrl']" mode="Note_body"&gt; &lt;xsl:param name="thisNode" select="."/&gt; &lt;xsl

  • I can create a CQWP for the Announcement content type, but when I export the .webpart and add &lt;property name="CommonViewFields" type="string"&gt;Expires,Date;Body,text</property> In my ItemStyles.xsl I have added this to show all the returned fields &lt;xsl:template name="ShowXML" match="Row[@Style='ShowXML']" mode="itemstyle"&gt; &lt;xsl:for-each select="@*"&gt; &lt;br /&gt; Name: &lt;xsl:value-of select="name()" /&gt; &lt;br /&gt;Value:&lt;xsl:value-of select="." disable-output-escaping="yes"/&gt; &lt;/xsl:for-each> &lt;/xsl:template>

  • ()); } UPDATE: Based on feedback from Wictor, I tried the following: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;webParts> &lt;webPart xmlns="http://schemas.microsoft.com/WebPart/v3"&gt; &lt...\"?&gt;" + "&lt;webParts>&lt;webPart xmlns=\"http://schemas.microsoft.com/WebPart/v3\"&gt;" + "&lt;metaData>" + "&lt;type name... correctly for the Content Editor Web Part (based on sample code), however when I replace the Content Editor Web Part definition with one for my own web part I get the following error

  • Nullable="TRUE" and Type="UserMulti". This works, in the query now returns the correct number of records, however the value in my 2 multi user fields is empty. &lt;View> &lt;Webs Scope="Recursive...; &lt;FieldRef Name="ID" /&gt; &lt;!-- Field which do not return values --&gt; &lt;FieldRef Name="Responsible" Nullable="TRUE" Type="UserMulti" /&gt; &lt;FieldRef Name="Accountable" Nullable="TRUE" Type="UserMulti" /&gt; &lt;!-- // Field which do not return values --&gt; &lt;ProjectProperty Name="Title" /&gt; &lt;/ViewFields> &lt;/View> &lt;Query>

  • and not the id) &lt;Query> &lt;Where> &lt;Eq> &lt;FieldRef Name='Relaterat_x0020_till_x0020_avtal' /&gt; &lt;Value Type='Lookup'&gt;Test avtal</Value>...' /&gt; &lt;Value Type='Lookup'&gt;1&lt;/Value> &lt;/Eq> &lt;/Where> &lt;/Query> The problem is that this SPQuery returns results only for the first 13 lookup...); Update: I've built a C# console app which creates all lists / lookup columns / content types and add the lookup column to corresponding content type. Result: same thing, same magic number 13. Anyone

  • = "Microsoft"; //The XML string containing the query request information //for the Web service string qXMLString = "&lt;QueryPacket xmlns='urn:Microsoft.Search.Query'&gt;" + "&lt;Query>&lt;SupportedFormats>&lt;Format revision='1'&gt;" + "urn:Microsoft.Search.Response.Document:Document</Format>" + "&lt;/SupportedFormats>&lt;Context>...I am using Microsoft Search Server 2008 (based on SharePoint Server 2007) + C# + .Net 3.5 + VSTS 2008 + ASP.Net to develop a web application which invokes Search Server 2008 Web Services when

  • " xmlns="http://schemas.microsoft.com/sharepoint/"&gt; &lt;ElementManifests> &lt;ElementManifest Location="elements.xml"/&gt; &lt;/ElementManifests> &lt;/Feature> Element.xml &lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;Elements xmlns="http://schemas.microsoft.com/sharepoint/"&gt; &lt;Module Name="WebPartPopulation" Url="_catalogs/wp" RootWebOnly="TRUE"&gt...) { SPList list = web.Lists["Web Part Gallery"]; // go through the items in reverse //for (int i = list.ItemCount – 1; i &gt;= 0; i–) for(int i=list.ItemCount-1; i &gt;=0

  • in-browser and they are visible in SPDesigner, but when I "View All Site Content" they are not there. Why is this the case? The Feature's Elements File: &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;Elements Id="9e85eb79-6d8d-4ff3-b0d4-64d55c3bb577" xmlns="http://schemas.microsoft.com/sharepoint/"&gt; &lt;Module Name="Pages" Url="Pages"&gt; &lt;File Path="Example.aspx" Url...="~masterurl/default.master"%&gt; &lt;%-- deliberately left empty --&gt; (yup, it's empty!) Addendum When I "visit the pages in-browser" I mean navigate to their URLs manually: http://myserver:PORT/subsite

Data information