Need to calculate value in each loop and sum together in XSLT 1.0

Surge picture Surge · Oct 30, 2012 · Viewed 12.4k times · Source

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>

And an XSLT as:

<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>

We get an output XML as:

<MyTax>
    <Stax>715</Stax>
</MyTax>

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>

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.

Answer

Dimitre Novatchev picture Dimitre Novatchev · Oct 31, 2012

This XSLT 1.0 transformation:

<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>

when applied on the provided XML document:

<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

Do note that the xxx:node-set() extension function is necessary (this can be avoided using recursion).