-1

I want to sum the number column in a table,, I can't able to understand how can I do that in XSL FO? Can anyone explain me in an easy manner
Here is my XML input code:

<Studies>
<Study>
<name>name1</name>
<amount>500</amount>
<age>5</age>
</Study>
<Study>
<name>name1</name>
<amount>500</amount>
<age>15</age>
</Study>
<Study>
<name>name1</name>
<amount>500</amount>
<age>20</age>
</Study>
</studies>

I want the output as, Total:1500(ie., sum the amount) see also this related question: How can I sum up some values per page in a table in XSL-FO?

Community
  • 1
  • 1
Jeni
  • 320
  • 2
  • 12

1 Answers1

1

You don't "sum the number column in a table"; you sum the numbers that go into the column. For example:

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

<xsl:template match="/Studies">
    <table>
        <xsl:for-each select="Study">
            <tr>
                <td><xsl:value-of select="name"/></td>
                <td><xsl:value-of select="age"/></td>
                <td><xsl:value-of select="amount"/></td>
            </tr>
        </xsl:for-each>
            <tr>
                <td colspan="2">Total Amount</td>
                <td><xsl:value-of select="sum(Study/amount)"/></td>
            </tr>               
    </table>
</xsl:template>

</xsl:stylesheet>

Note:
XML is case-sensitive: </studies> does not close <Studies>.

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