Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to calculate running total with Xslt

Status
Not open for further replies.

AnNguyen

Programmer
Jul 18, 2002
33
US
Hello and please help.
I'm new to XSL and how can i calculate the running total and display it within the for loop.
part of my XML
<Expenses>
<expense> <id>1</id><amount>10</amount></expense>
<expense> <id>1</id><amount>20</amount></expense>
<expense> <id>1</id><amount>30</amount></expense>
.....
</expenses>
my XSL
<xsl:template match="/">
...
<xsl:for-each select="expenses/expense">
<tr><td><xsl:value-of select="id"/></td>
<td><xsl:value-of select="amount"/></td>
<!-- here is the running total of the amount ????? -->
<xsl:variable name="temp" select="sum(preceding-sibling::expenses/expense/amount) + amount"/>
<td><xsl:value-of select="$temp"/></td>
<tr>
</xsl:for-each>....

and it gives
<tr><td>1</td><td>10</td><td>10</td></tr>
<tr><td>2</td><td>20</td><td>20</td></tr>
<tr><td>2</td><td>20</td><td>30</td></tr> ...

instead of
<tr><td>1</td><td>10</td><td>10</td></tr>
<tr><td>2</td><td>20</td><td>30</td></tr>
<tr><td>2</td><td>20</td><td>60</td></tr>...

What did i do wrong? or how ca i implement something similar to the following VB code in XSL
A= 0
for (i=1 to 10)
a = a + somevalue
' display a
next i

Thank you



 
><xsl:variable name="temp" select="sum(preceding-sibling::expenses/expense/amount) + amount"/>
[tt]<xsl:variable name="temp" select="sum([red]preceding-sibling::expense[/red]/amount) + amount"/>[/tt]
 
Thank you.
it solves my problem.
by the way, would you suggest a good XSL book for me to buy.

 
>would you suggest a good XSL book for me to buy.

Normally I am very reluctant to do so. It is very much dependent on one's background, what we do and pattern of learning... Since you ask, I would make a very much unorthodox suggestion, a dated book:
[tt]M Van Otegem, Teach Yourself XSLT in 21 Days, 2002, Que/Sams.[/tt]
Covers only selected topics of xslt 1.0 at entry level but with a precision and judicial parsimony of wording that one, after being much more thoroughly mastered the subject matter, can go back and re-read it to appreciate more what the author has put into without being bored by then, (not necessarily applicable to other Teach Yourself in N Days type books, usually I don't read). The importance is you being enlighted and the subject matters solidly understood and not carried away before the time by cliché and modes like in many other resources... then the rest is just reading technical documents, articles (more than one can handle) on the net and more advanced level books (like Kay's and others you can't miss by simply browsing the net) to know more what others are doing with the technology, and yourself do ungodly number of little or sizeable applications and practices.. But by then you do so with ground well-prepard. It is worthy to be with certainly much longer than 21 days.

You could maybe buy it 2nd hand since it is already out there for that long time and, in case that you don't like it or already way above the level, to not to regret later for having spend too much for a copy of it.

But if I recommend M Kay's xslt 2.0 and xpath 2.0 (two volumes), I am on the safe-side and without a doubt excellent references as well; but it may end up on the shelf dusting for many.
 
Let me take this opportunity to push faq426-6460. The final bullet covers a worthy contender for an XSLT resource.

(Later I'll likely add Tsuji's recommendations.)
 
No need to use a <xsl:for-each ..> Assuming your document is:
Code:
<Expenses>
   <expense><id>1</id><amount>10</amount></expense>
   <expense><id>2</id><amount>20</amount></expense>
   <expense><id>2</id><amount>30</amount></expense>
</Expenses>
the following XSL stylesheet
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:output method="http"/>

   <xsl:template match="expense">
      <tr><td><xsl:value-of select="id"/></td>
      <td><xsl:value-of select="amount"/></td>
      <td><xsl:value-of select="sum(preceding-sibling::expense/amount) + amount"/></td></tr>
   </xsl:template>

</xsl:stylesheet>
outputs
Code:
   <tr><td>1</td><td>10</td><td>10</td></tr>
   <tr><td>2</td><td>20</td><td>30</td></tr>
   <tr><td>2</td><td>30</td><td>60</td></tr>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top