I am trying to output a table which lists a customer's payments whilst keeping a running total. A section of my XML is as follows:

<Accounts>
<Payment>
<CustomerID>1</CustomerID>
<Date>2009-03-16</Date>
<Amount>22</Amount>
</Payment>
<Payment>
<CustomerID>4</CustomerID>
<Date>2009-02-12</Date>
<Amount>35</Amount>
</Payment>
<Payment>
<CustomerID>2</CustomerID>
<Date>2009-05-17</Date>
<Amount>41</Amount>
</Payment>

I have included the following code to sort the output into date order:

<xsl:apply-templates select="Payment">
<xsl:sort select="Date" />
</xsl:apply-templates>

In attempting to output the table I have written the following code:

<td>
<xsl:apply-templates select="Date" /> 
</td>
<td>
<xsl:apply-templates select="Amount" />
</td>
<td>
<xsl:variable name="running-total" select="sum(preceding-sibling::Payment/Amount) + Amount)" />
<xsl:value-of select="$running-total" />
</td>

However, when accumulating the running total the calculations are done in the order of the xml output. How would I change this so that the calculations are done (and displayed) by the earliest date first??

Many Thanks

split tag date
in Day Mounth Year

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" indent="yes"/>
    <xsl:template match="/">
        <html>
            <xsl:apply-templates select="Accounts"/>
        </html>
    </xsl:template>
    <xsl:template match="Accounts">
        <table border="black solid 5px">
            <tr>
                <td>CustomerID</td>
                <td>Date</td>
                <td>Amount</td>
                <td>Day</td>
                <td>Month</td>
                <td>Year</td>
            </tr>
            <xsl:apply-templates select="Payment">
                <xsl:sort select="substring-after(substring-after(./Date,'-'),'-')"/>
                <xsl:sort select="substring-before(substring-after(Date,'-'),'-')"/>
                <xsl:sort select="substring-before(./Date,'-')"/>
            </xsl:apply-templates>
            <tr>
                <td colspan="2">summe</td>
                <td align="center">
                    <xsl:value-of select="sum(//Amount)"/>
                </td>
            </tr>
        </table>
    </xsl:template>
    <xsl:template match="Payment">
        <tr>
            <td align="center">
                <xsl:value-of select="CustomerID"/>
            </td>
            <td>
                <xsl:value-of select="Date"/>
            </td>
            <td align="center">
                <xsl:value-of select="Amount"/>
            </td>
            <td align="center">
                <xsl:value-of select="substring-after(substring-after(Date,'-'),'-')"/>
            </td>
            <td align="center">
                <xsl:value-of select="substring-before(substring-after(Date,'-'),'-')"/>
            </td>
            <td align="center">
                <xsl:value-of select="substring-before(Date,'-')"/>
            </td>
        </tr>
    </xsl:template>
</xsl:stylesheet>

Testdata

<?xml version="1.0"?>
<Accounts>
    <Payment>
        <CustomerID>1</CustomerID>
        <Date>2009-03-16</Date>
        <Amount>22</Amount>
    </Payment>
    <Payment>
        <CustomerID>10</CustomerID>
        <Date>2009-03-17</Date>
        <Amount>22</Amount>
    </Payment>
    <Payment>
        <CustomerID>4</CustomerID>
        <Date>2009-02-12</Date>
        <Amount>35</Amount>
    </Payment>
    <Payment>
        <CustomerID>2</CustomerID>
        <Date>2009-05-17</Date>
        <Amount>41</Amount>
    </Payment>
</Accounts>

result

<html>
   <table border="black solid 5px">
      <tr>
         <td>CustomerID</td>
         <td>Date</td>
         <td>Amount</td>
         <td>Day</td>

         <td>Month</td>
         <td>Year</td>
      </tr>
      <tr>
         <td align="center">4</td>
         <td>2009-02-12</td>
         <td align="center">35</td>

         <td align="center">12</td>
         <td align="center">02</td>
         <td align="center">2009</td>
      </tr>
      <tr>
         <td align="center">1</td>
         <td>2009-03-16</td>

         <td align="center">22</td>
         <td align="center">16</td>
         <td align="center">03</td>
         <td align="center">2009</td>
      </tr>
      <tr>
         <td align="center">10</td>

         <td>2009-03-17</td>
         <td align="center">22</td>
         <td align="center">17</td>
         <td align="center">03</td>
         <td align="center">2009</td>
      </tr>

      <tr>
         <td align="center">2</td>
         <td>2009-05-17</td>
         <td align="center">41</td>
         <td align="center">17</td>
         <td align="center">05</td>

         <td align="center">2009</td>
      </tr>
      <tr>
         <td colspan="2">summe</td>
         <td align="center">120</td>
      </tr>
   </table>

</html>

Helmut Hagemann Germany

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.