2015-03-08 142 views
0

只是我想做简单的加法。当我迭代值时,只是我想要求和并打印该值。XSLT:如何为全局变量赋值

这是我试过的方法。但它只是打印0.

这是我的xml。

 <t:Employee> 
     <t:Earnings_Deductions> 
      <t:Amount t:PriorValue="">4000</t:Amount> 
     </t:Earnings_Deductions> 
     </t:Employee> 
     <t:Employee> 
     <t:Earnings_Deductions> 
      <t:Amount t:PriorValue="">4000</t:Amount> 
     </t:Earnings_Deductions> 
     </t:Employee> 
     <t:Employee> 
     <t:Earnings_Deductions> 
      <t:Amount t:PriorValue="">4000</t:Amount> 
     </t:Earnings_Deductions> 
     </t:Employee> 

我想添加所有金额并显示它像这样。

<item><p>700000</p></item> 

<xsl:variable name="Total_Amount" select="0" /> 
     <item> 

     <xsl:for-each select="//*[local-name() = 'Employee']"> 
     <xsl:for-each select="//*[local-name() = 'Earning_Deductions']"/> 
     <xsl:variable name="amount"><xsl:value-of select="//*[local-name() = 'Amount']"/></xsl:variable> 
     <xsl:value-of select="$Total_Amount"><xsl:value-of select="$Total_Amount + $amount"/></xsl:value-of> 
     </xsl:for-each> 
     <p><xsl:value-of select="$Total_Amount"></xsl:value-of></p> 
     </item> 

我是新来的xslt。请帮我解决这个问题。

+1

这是XSLT不是如何工作 - 变量不能被更新。显示[完整的输入XML示例,完整的XSLT样式表和您期望的输出](http://stackoverflow.com/help/mcve) - 我确信还有另一种方法。 – 2015-03-08 22:34:44

回答

2

XSLT变量是不可变的。一旦设定好,就不能改变。

使用sum()功能,你就可以生产总量没有循环结构或需要改变的变量值:

<xsl:variable name="Total_Amount" 
    select="sum(//*[local-name()='Employee']/*[local-name()='Earning_Deductions']/*[local-name()='Amount'])"/> 
<xsl:value-of select="$Total_Amount"/> 
+0

我想添加所有金额。我应该在哪里提及它? – 2015-03-09 02:05:25

+0

对不起,我添加了'Earning_Deductions'而不是'Amount'。我已经更新了XPath表达式。 – 2015-03-09 02:11:52