title: XSLT sum of nodes by parameter
body: <p>I have a problem with an XSLT: I like to compute the sum of nodes by a parameter. </p>
  
  <p>The XML source looks like this:</p>
  
  <pre><code>&lt;Documents&gt;
    &lt;Document&gt;
      &lt;Deleted&gt;0&lt;/Deleted&gt;
      &lt;DocumentType&gt;2&lt;/DocumentType&gt;
      &lt;Currency&gt;EUR&lt;/Currency&gt;
      &lt;CurrencyRate&gt;4.368400&lt;/CurrencyRate&gt;
      &lt;GrossValue&gt;1000.00&lt;/GrossValue&gt;
      &lt;DeliveryDate&gt;2016-08-01&lt;/DeliveryDate&gt;
      &lt;FormOfPayment&gt;2&lt;/FormOfPayment&gt;
      &lt;DueDate&gt;2016-09-28&lt;/DueDate&gt;
    &lt;/Document&gt;
    &lt;Document&gt;
      &lt;Deleted&gt;0&lt;/Deleted&gt;
      &lt;DocumentType&gt;2&lt;/DocumentType&gt;
      &lt;Currency&gt;EUR&lt;/Currency&gt;
      &lt;CurrencyRate&gt;4.368400&lt;/CurrencyRate&gt;
      &lt;GrossValue&gt;2000.00&lt;/GrossValue&gt;
      &lt;DeliveryDate&gt;2016-08-05&lt;/DeliveryDate&gt;
      &lt;FormOfPayment&gt;5&lt;/FormOfPayment&gt;
      &lt;DueDate&gt;2016-09-05&lt;/DueDate&gt;
    &lt;/Document&gt;
    &lt;Document&gt;
      &lt;Deleted&gt;0&lt;/Deleted&gt;
      &lt;DocumentType&gt;2&lt;/DocumentType&gt;
      &lt;Currency&gt;EUR&lt;/Currency&gt;
      &lt;CurrencyRate&gt;4.368400&lt;/CurrencyRate&gt;
      &lt;GrossValue&gt;3000.00&lt;/GrossValue&gt;
      &lt;DeliveryDate&gt;2016-08-30&lt;/DeliveryDate&gt;
      &lt;FormOfPayment&gt;2&lt;/FormOfPayment&gt;
      &lt;DueDate&gt;2016-10-29&lt;/DueDate&gt;
    &lt;/Document&gt;
    &lt;Document&gt;
      &lt;Deleted&gt;0&lt;/Deleted&gt;
      &lt;DocumentType&gt;2&lt;/DocumentType&gt;
      &lt;Currency&gt;EUR&lt;/Currency&gt;
      &lt;CurrencyRate&gt;4.368400&lt;/CurrencyRate&gt;
      &lt;GrossValue&gt;2500.00&lt;/GrossValue&gt;
      &lt;DeliveryDate&gt;2016-08-26&lt;/DeliveryDate&gt;
      &lt;FormOfPayment&gt;5&lt;/FormOfPayment&gt;
      &lt;DueDate&gt;2016-09-10&lt;/DueDate&gt;
    &lt;/Document&gt;
  &lt;/Documents&gt;
  </code></pre>
  
  <p>The result that I need is a sum of amount GrossValue by the parameter <code>FormOfPayment</code>. </p>
  
  <ul>
  <li>So if <code>FormOfPayment</code> is 5, the variable <code>GrossValueCard</code> should be 4500 </li>
  <li>And when <code>FormOfPayment</code> is 2, the variable <code>GrossValueCash</code> should be 4000. </li>
  </ul>
  
  <p>I tried do this on that way:</p>
  
  <pre><code>&lt;xsl:template name="Suma"&gt;
      &lt;xsl:param name="index" select="1"/&gt;
      &lt;xsl:param name="nodes"/&gt;
      &lt;xsl:param name="totalAmount" select="0"/&gt;
      &lt;xsl:variable name="currentAmount" select="translate($nodes[$index],',','.')"/&gt;
      &lt;xsl:choose&gt;
          &lt;xsl:when test="count($nodes)=0"&gt;
              &lt;xsl:value-of select="0"/&gt;
          &lt;/xsl:when&gt;
          &lt;xsl:when test="$index=count($nodes)"&gt;
              &lt;xsl:value-of select="$totalAmount + $currentAmount"/&gt;
          &lt;/xsl:when&gt;
          &lt;xsl:otherwise&gt;
              &lt;xsl:call-template name="Suma"&gt;
                  &lt;xsl:with-param name="index" select="$index+1"/&gt;
                  &lt;xsl:with-param name="totalAmount" select="$totalAmount + $currentAmount"/&gt;
                  &lt;xsl:with-param name="nodes" select="$nodes"/&gt;
              &lt;/xsl:call-template&gt;
          &lt;/xsl:otherwise&gt;
      &lt;/xsl:choose&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>and it works, but the sum is from all <code>GrossValue</code> nodes. Can you give me some advice where I should set a parameter to select the sum by <code>FormOfPayment</code>? </p>
  
  <p>I tried to pick this in condition "when" or "if", but it didn't work.</p>