XSLT用于在数据视图Web部分中求和一列
-
16-10-2019 - |
题
我有这个(试图解决任务):
<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>
结果是:“ nan”,所以我认为我的XSLT是错误的。
问题是:如何编写XSLT来计算和显示数据视图Web部分中“距离”列中所有值的总和?该列可能具有空值(顺便说一句,现在没有空值),应忽略计算总和。
更新1:
<xsl:decimal-format grouping-separator="." decimal-separator="," NaN="" name="european"/>
<xsl:template match="/">
测试:
<xsl:copy-of
select="format-number(sum(for ($i in /dsQueryResponse/Rows/Row/@Distance) { return number(replace($i, ',','.')})), '#,##', 'european')" />
尝试了此操作,但失败(设置处理器样式表。预期令牌')'找到“名称”。
更新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>
我上面有XSLT,它确实提供了输出,但是在距离列中具有以下值:
2 3,5 -1,3 0
我得到的结果与NAN不同,但没有总和,只有我所看到的第一个值。结果是:xsum //距离= 2。
更新3:
这有效。选择起点节点有一些问题,但现在应该修复。
<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>
解决方案
其他提示
这应该有效:
<xsl:value-of select="sum($Rows[string-length(@Distance) > 0]/@Distance)" />
M.