title: xslt show top 5 of sums
body: <p>long time reader, 1st time poster here. I'm usually able to get quite a lot of info from other posts on the site but i can't find a solution for this particular problem.
  Using xslt, I'm currently able to show a sub total of each client invoice then the total of those invoices by adding another variable of <code>$grandtotal</code> to my below xslt template and adding the <code>$sum</code> to it in each iteration of the loop.</p>
  
  <p>What I now need to do is to find the top 5 highest totaling invoices.</p>
  
  <p>This is a shortened version of my XML:</p>
  
  <pre><code>&lt;bits&gt;
      &lt;client type="Commercial"&gt;
      &lt;clientid&gt;1&lt;/clientid&gt;
              &lt;inv&gt;
              &lt;invno&gt;1&lt;/invno&gt;
                  &lt;product&gt;
                      &lt;productid&gt;321&lt;/productid&gt;
                      &lt;productprice&gt;99.00&lt;/productprice&gt;
                      &lt;totalqty&gt;2&lt;/totalqty&gt;
                  &lt;/product&gt;
                  &lt;product&gt;
                      &lt;productid&gt;333&lt;/productid&gt;
                      &lt;productprice&gt;299.00&lt;/productprice&gt;
                      &lt;totalqty&gt;1&lt;/totalqty&gt;
              &lt;/product&gt;
          &lt;/inv&gt;
              &lt;inv&gt;
              &lt;invno&gt;2&lt;/invno&gt;
                  &lt;product&gt;
                      &lt;productid&gt;321&lt;/productid&gt;
                      &lt;productprice&gt;99.00&lt;/productprice&gt;
                      &lt;totalqty&gt;2&lt;/totalqty&gt;
                  &lt;/product&gt;
                  &lt;product&gt;
                      &lt;productid&gt;333&lt;/productid&gt;
                      &lt;productprice&gt;299.00&lt;/productprice&gt;
                      &lt;totalqty&gt;2&lt;/totalqty&gt;
              &lt;/product&gt;
          &lt;/inv&gt;
      &lt;/client&gt;
      &lt;client type="Government"&gt;
          &lt;clientid&gt;2&lt;/clientid&gt;
          &lt;inv&gt;
              &lt;invno&gt;3&lt;/invno&gt;
              &lt;product&gt;
                  &lt;productid&gt;399&lt;/productid&gt;
                  &lt;productprice&gt;1469.00&lt;/productprice&gt;
                  &lt;totalqty&gt;1&lt;/totalqty&gt;
                  &lt;/product&gt;
              &lt;product&gt;
                  &lt;productid&gt;354&lt;/productid&gt;
                  &lt;productprice&gt;15.00&lt;/productprice&gt;
                  &lt;totalqty&gt;1&lt;/totalqty&gt;
              &lt;/product&gt;
                  &lt;product&gt;
                  &lt;productid&gt;311&lt;/productid&gt;
                  &lt;productprice&gt;58.00&lt;/productprice&gt;
                  &lt;totalqty&gt;1&lt;/totalqty&gt;
              &lt;/product&gt;
                  &lt;product&gt;
                      &lt;productid&gt;341&lt;/productid&gt;
                      &lt;productprice&gt;199.00&lt;/productprice&gt;
                      &lt;totalqty&gt;1&lt;/totalqty&gt;
                  &lt;/product&gt;
          &lt;/inv&gt;
      &lt;/client&gt;
  &lt;/bits&gt;
  </code></pre>
  
  <p>I have used the following code to sum the invoice total for each client:</p>
  
  <pre><code>&lt;xsl:for-each select="//client"&gt;
      &lt;xsl:call-template name="sum"&gt;
          &lt;xsl:with-param name="nodes" select="inv/product"/&gt;
      &lt;/xsl:call-template&gt;
  &lt;/xsl:for-each&gt;
  
  &lt;xsl:template name="sum"&gt;
  &lt;xsl:param name="nodes" /&gt;
  &lt;xsl:param name="sum" select="0" /&gt;
  &lt;xsl:variable name="current" select="$nodes[1]" /&gt;
  &lt;xsl:if test="$current"&gt;
    &lt;xsl:call-template name="sum"&gt;
      &lt;xsl:with-param name="nodes" select="$nodes[position() &amp;gt; 1]" /&gt;
      &lt;xsl:with-param name="sum" select="$sum + $current/totalqty * $current/productprice" /&gt;
    &lt;/xsl:call-template&gt;
  &lt;/xsl:if&gt;
  &lt;xsl:if test="not($current)"&gt;
    &lt;xsl:value-of select="$sum" /&gt;
  &lt;/xsl:if&gt;
  </code></pre>
  
  <p></p>
  
  <p>What I'd like to do is reuse this code to also show the 5 highest summing invoice and their corresponding <code>&lt;clientid&gt;</code> and <code>type</code> eg:</p>
  
  <p>Top 5:</p>
  
  <ol>
  <li>Clientid: 2, Invoice no: 3, Invoice total: $1741, Type: Government</li>
  <li>Clientid: 1, Invoice no: 2, Invoice total: $796, Type: Commercial</li>
  <li>Clientid: 1, Invoice no: 1, Invoice total: $497, Type: Commercial</li>
  </ol>
  
  <p>In the past i have used a for loop <code>&lt;xsl:for-each select=...&gt;</code>
  <code>&lt;xsl:sort select="Total" data-type="number" order="descending"/&gt;</code> 
  ...<code>&lt;xsl:if test="position()&amp;lt;6"&gt;</code>
  to show top 5 but this is looking at a stored value.
  I will need another solution. </p>
  
  <p>I need some tips at this point as I'm still very new to markup!</p>