[英]XSLT : How to assign a value to global variable
我只想做簡單的加法。 當我迭代值時,我只想進行求和並打印該值。
這是我嘗試的方式。 但這只是打印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的新手。 請幫我解決這個問題。
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"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.