需要在每个循环中计算值并在XSLT 1.0中加总 [英] Need to calculate value in each loop and sum together in XSLT 1.0
问题描述
说我有一个简单的输入XML文档,例如:
Say I have a simple input XML document like:
<Amounts>
<item>
<Base>4750</Base>
<Tax1>2800</Tax1>
<Tax2>50</Tax2>
</item>
<item>
<Base>4750</Base>
<Tax1>2800</Tax1>
<Tax2>50</Tax2>
</item>
</Amounts>
XSLT为:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output version="1.0" method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:element name="MyTax">
<xsl:variable name="bf" select="sum(//item/Base)"/>
<xsl:variable name="discount" select="$bf * 0.08"/>
<xsl:variable name="bf1" select="$bf - $discount"/>
<xsl:variable name="yq1" select="sum(//item/Tax1)"/>
<xsl:variable name="yr1" select="sum(//item/Tax2)"/>
<xsl:variable name="stax">
<xsl:value-of select="round(format-number((($bf1 + $yq1 + $yr1) * 0.0495),'#.##'))"/>
</xsl:variable>
<xsl:element name="Stax"><xsl:value-of select="$stax"/></xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我们得到的输出XML为:
We get an output XML as:
<MyTax>
<Stax>715</Stax>
</MyTax>
但是,现在我想分别为每个项目计算stax,然后将这些项目加起来-这是因为在上面的示例中,由于单个stax的总和实际上是714-我要查找的值,而不是上面的715,所以我最终产生了舍入错误。
However, now i'd like to compute stax separately for each item and then sum those items up - this is because in the above example I end up with rounding errors as the sum of the individual stax is actually 714 - the value I am looking for, rather than 715 above.
因此我正在寻找的输出将是:
So the output I am looking for would be:
<MyTax>
<item>
<Stax>357</Stax>
</item>
<item>
<Stax>357</Stax>
</item>
<TotalStax>714</TotalStax>
</MyTax>
尽管我可以设置一个for-each,但如何为每次通过将stax计算为变量,然后总结各个stax?我限于XSLT 1.0,看到了一些使用键和节点集的答案,但无法理解如何应用它们。
Though I can set up a for-each, how do I compute stax into a variable for each pass, and then sum up the individual stax? I am limited XSLT 1.0, saw some answers using keys and nodesets but am not able to understand how to apply them.
推荐答案
此XSLT 1.0转换:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<xsl:variable name="vrtfTaxSeq">
<xsl:apply-templates/>
</xsl:variable>
<xsl:value-of select="sum(ext:node-set($vrtfTaxSeq)/*)"/>
</xsl:template>
<xsl:template match="item">
<ttax>
<xsl:value-of select="round(0.0495*(Base - 0.08*Base + Tax1 + Tax2))"/>
</ttax>
</xsl:template>
</xsl:stylesheet>
在应用于提供的XML文档时:
<Amounts>
<item>
<Base>4750</Base>
<Tax1>2800</Tax1>
<Tax2>50</Tax2>
</item>
<item>
<Base>4750</Base>
<Tax1>2800</Tax1>
<Tax2>50</Tax2>
</item>
</Amounts>
计算每个项的总和
并产生想要的结果:
calculates the sums for each item
and produces the wanted result:
714
请注意,必须使用 xxx:node-set()
扩展功能(可以使用递归来避免这种情况)。
Do note that the xxx:node-set()
extension function is necessary (this can be avoided using recursion).
这篇关于需要在每个循环中计算值并在XSLT 1.0中加总的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!