title: Need to calculate value in each loop and sum together in XSLT 1.0
body: <p>Say I have a simple input XML document like:</p>
  
  <pre><code>&lt;Amounts&gt;
      &lt;item&gt;
          &lt;Base&gt;4750&lt;/Base&gt;
          &lt;Tax1&gt;2800&lt;/Tax1&gt;
          &lt;Tax2&gt;50&lt;/Tax2&gt;
      &lt;/item&gt;
      &lt;item&gt;
          &lt;Base&gt;4750&lt;/Base&gt;
          &lt;Tax1&gt;2800&lt;/Tax1&gt;
          &lt;Tax2&gt;50&lt;/Tax2&gt;
      &lt;/item&gt;
  &lt;/Amounts&gt;
  </code></pre>
  
  <p>And an XSLT as:</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  &lt;xsl:output  version="1.0" method="xml" omit-xml-declaration="yes" indent="yes"/&gt;
      &lt;xsl:template match="/"&gt;
        &lt;xsl:element name="MyTax"&gt;
          &lt;xsl:variable name="bf" select="sum(//item/Base)"/&gt;
          &lt;xsl:variable name="discount" select="$bf * 0.08"/&gt;
          &lt;xsl:variable name="bf1" select="$bf - $discount"/&gt;
          &lt;xsl:variable name="yq1" select="sum(//item/Tax1)"/&gt;
          &lt;xsl:variable name="yr1" select="sum(//item/Tax2)"/&gt;
          &lt;xsl:variable name="stax"&gt;
              &lt;xsl:value-of select="round(format-number((($bf1 + $yq1 + $yr1) * 0.0495),'#.##'))"/&gt;
          &lt;/xsl:variable&gt;
          &lt;xsl:element name="Stax"&gt;&lt;xsl:value-of select="$stax"/&gt;&lt;/xsl:element&gt;
        &lt;/xsl:element&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>We get an output XML as:</p>
  
  <pre><code>&lt;MyTax&gt;
      &lt;Stax&gt;715&lt;/Stax&gt;
  &lt;/MyTax&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>So the output I am looking for would be:</p>
  
  <pre><code>&lt;MyTax&gt;
           &lt;item&gt;
      &lt;Stax&gt;357&lt;/Stax&gt;
           &lt;/item&gt;
           &lt;item&gt;
              &lt;Stax&gt;357&lt;/Stax&gt;
           &lt;/item&gt;
           &lt;TotalStax&gt;714&lt;/TotalStax&gt;
  &lt;/MyTax&gt;
  </code></pre>
  
  <p>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.</p>