2013-02-08 39 views
0

我想要得到的总和。这是xslt代码。如何获得属性中的值的总和在xslt

<xsl:template match="Entry"> 
    <xsl:if test="position() &lt;= 10"> 


    <tr> 
     <td> 
     <xsl:value-of select="substring-before(@Value,'||')"/> 
     </td> 

     <td> 
     <xsl:value-of select="format-number(substring(substring-after(@Value,'||||'),1,10),'#.#')"/> 

     </td> 
    </tr> 

</xsl:if> 


    </xsl:template> 

上面的代码会将数据填充为两个颜色。没关系。现在我需要得到的总和<xsl:value-of select="format-number(substring(substring-after(@Value,'||||'),1,10),'#.#')"/> 我来自程序编程。我读了很多文章,但我仍然想知道如何得到这个总和。有谁能够帮助我?

这里是XML

<TopHoldings Currency="xxx"> 
      <Entry Type="CName||C||S||Fund Weight (%)||Benchmark weight (%)" Value="Ab||U||||1.2170000000000||" Date="8/31/2011" /> 

这里是整个XSLT

 <table style="width:50%;font-size:12px;" cellspacing="0" cellpadding="0"> 
      <tr style="width:50%; text-align:left;background-color:E6F1F9;"> 
      <th>  </th> 
      <th>  % of funds  </th> 
     </tr> 
     <xsl:apply-templates select="$items"> 
       <xsl:sort select="format-number(substring(substring-after(@Value,'||||'),1,10),'#.#')" order="descending"/> 
       <xsl:sort select="substring-before(@Value,'||')"/> 
     </xsl:apply-templates> 


     </table> 

     </body> 

    </html> 

    </xsl:template> 

    <xsl:template match="Entry"> 
    <xsl:if test="position() &lt;= 10"> 


    <tr> 
     <td> 
     <xsl:value-of select="substring-before(@Value,'||')"/> 
     </td> 

     <td> 
     <xsl:value-of select="format-number(substring(substring-after(@Value,'||||'),1,10),'#.#')"/> 

     </td> 
    </tr> 

</xsl:if> 


    </xsl:template> 



</xsl:stylesheet> 
+0

您能否向我们展示您引用此模板的模板?您是否只想总结前10个值? – JLRishe

+0

是的我想获得十个值的总和 – newday

+0

并且关于我对更大的XSLT样本...?您使用什么处理器来执行您的XSLT? – JLRishe

回答

2

当你需要总结的XSLT 1.0你有多个值的值依赖于递归[编辑:在XSLT 1.0中函数总和也可用](在XSLT 2.0中有一个XPath函数sum( ))。

以下模板通过elements-to-sum参数执行给定元素的总和,提取值以按照您指定的属性@Value求和。

<?xml version="1.0" encoding="utf-8" ?> 

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       version="1.0"> 

<xsl:output method="html" /> 

<xsl:template match="TopHoldings"> 
    <table> 
     <xsl:call-template name="sum"> 
      <xsl:with-param name="elements-to-sum" 
          select="Entry" /> 
     </xsl:call-template> 
    </table> 
</xsl:template> 

<xsl:template name="sum"> 
    <xsl:param name="elements-to-sum" /> 

    <xsl:param name="sum" select="'0'" /> 

    <!-- Store in variables the following operations to avoid performing them more than once --> 
    <xsl:variable name="current-element" select="$elements-to-sum[1]" /> 
    <xsl:variable name="current-value" select="format-number(substring(substring-after($current-element/@Value,'||||'),1,10),'#.#')" /> 

    <!-- Output the table row --> 
    <tr> 
     <td><xsl:value-of select="substring-before($current-element/@Value, '||')" /></td> 
     <td><xsl:value-of select="$current-value" /></td> 
    </tr> 

    <!-- Determine whether continue --> 
    <xsl:choose> 
     <!-- Case END: we have just one element to sum so we perform the sum and we output the desired result --> 
     <xsl:when test="count($elements-to-sum) = 1"> 
      <tr> 
       <td>Result:</td> 
       <td><xsl:value-of select="$current-value + $sum" /></td> 
      </tr> 
     </xsl:when> 
     <!-- Case RECURSION : we call this template again adding the current value to the sum and removing the first element from the parameter elements-to-sum --> 
     <xsl:otherwise> 
      <xsl:call-template name="sum"> 
       <xsl:with-param name="elements-to-sum" 
           select="$elements-to-sum[position() > 1]" /> 
       <xsl:with-param name="sum" 
           select="$sum + $current-value" /> 
      </xsl:call-template> 
     </xsl:otherwise> 
    </xsl:choose> 

</xsl:template> 

</xsl:stylesheet> 

我假设你想显示总和的结果作为在同一个表中的新行,如果情况并非如此,你要显示的结果在其他地方的解决方案将略有不同。告诉我,如果这个解决方案对你来说是可以接受的,或者你需要在元素外面显示总和。

注意:不要在属性@Value中执行这些'复杂'的字符串操作,我会考虑将该属性中的所有信息分成不同的属性。

+0

顺便说一句,我发布此代码之前知道你张贴你的。如果您无法完成工作,请告诉我,我会将其重写为您当前的代码。 –

+0

如果您可以将新代码添加为解决方案。 – newday

+0

只有两个问题: 1.你将如何处理值的总和?因为在您发布的XSLT中,我无法看到打印结果的位置。 2.您是否使用支持节点集扩展功能的XSLT处理器? –

0

只需使用XPath函数sum(),就可以在XSLT 2.0的XPath 2.0和XSLT 1.0的XPath 1.0中使用它,如http://www.w3.org/TR/xpath/#function-sum中所述。如果你想得到总和的数字是属性“Value”到一个元素“Entry”,使用sum(// Entry/@ Value)来抓取所有数据并进行总结。 xml数据

+0

你确定吗? sum不会将所有属性相加在一起。这就是递归的原因。如果它是元素节点,那么我们可以使用sum。如果我错了,请纠正我。 – newday

+0

@pottuamman:是的,我尝试和sum函数处理属性 - 但前提是函数可以从节点列表中抓取它们(甚至属性在技术上是节点)。同时我意识到你尝试求和的数据只是属性字符串的一部分,所以你用“substring”做一些解开。因为然后没有总和可以工作的节点列表,它会失败。尝试将信息分成自己的属性。你可以做一个准备XSL转换来获得适当的层次结构,并就此进行总结。 – Andreas