0

I am trying to write XSL (1.0) to recursively add values from nodes. I have googled and it seems there are functions to add as long as the nodes are parent/child. My nodes are quite a few steps beyond that. For example, I have:

<Document>
  <Finance>
    <Account>
      <Type>Expense</Expense>
      <Amount>25</Amount>
    </Account>
  </Finance
</Document>
<Document>
  <Finance>
    <Account>
      <Type>Capital</Type>
      <Amount>75</Amount>
    </Account>
  </Finance
</Document>
<Document>
  <Finance>
    <Account>
      <Type>Expense</Type>
      <Amount>50</Amount>
    </Account>
  </Finance
</Document>

I need to get a variable or something that totals 150 by adding all of these amounts. Note. This is not the exact structure but I simplified it. The nodes are the same for each document. I have been looking for a way of basically doing: i=0 then looping to add up with i=i+1

It seems like this is something that should be easy.

I even thought of maybe being able to for-each and creating a table that just has two columns then using that to add up but I cannot find a way. So I am looking for Account Type and the total running amount: Expense = 75 Capital = 75

  • Can you show us your code? – zeeMonkeez Nov 20 '15 at 03:00
  • "*I have been looking for a way of basically doing: i=0 then looping to add up with i=i+1*" That's not how you do it in XSLT. This is really a question about *grouping* - start here: http://www.jenitennison.com/xslt/grouping/muenchian.html – michael.hor257k Nov 20 '15 at 08:48

1 Answers1

1

If the categories (Expense, Capital) are constant and known in advance, you could simply sum each in turn. Here's a simple example:

XML

<root>
   <Document>
      <Finance>
         <Account>
            <Type>Expense</Type>
            <Amount>25</Amount>
         </Account>
      </Finance>
   </Document>
   <Document>
      <Finance>
         <Account>
            <Type>Capital</Type>
            <Amount>100</Amount>
         </Account>
      </Finance>
   </Document>
   <Document>
      <Finance>
         <Account>
            <Type>Expense</Type>
            <Amount>50</Amount>
         </Account>
      </Finance>
   </Document>
</root>

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:key name="tx-by-type" match="Account" use="Type" />

<xsl:template match="/root">
    <table border="1">
        <tr>
            <th>Expense</th>
            <th>Capital</th>
            <th>Total</th>
        </tr>
        <tr>
            <xsl:variable name="total-expense" select="sum(key('tx-by-type', 'Expense')/Amount)" />
            <xsl:variable name="total-capital" select="sum(key('tx-by-type', 'Capital')/Amount)" />
            <td>
                <xsl:value-of select="$total-expense" />
            </td>
            <td>
                <xsl:value-of select="$total-capital" />
            </td>
            <td>
                <xsl:value-of select="$total-expense + $total-capital" />
            </td>
        </tr>
    </table>
</xsl:template>

</xsl:stylesheet>

Result

<table border="1">
   <tr>
      <th>Expense</th>
      <th>Capital</th>
      <th>Total</th>
   </tr>
   <tr>
      <td>75</td>
      <td>100</td>
      <td>175</td>
   </tr>
</table>

enter image description here

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51