Jump to content
Sign in to follow this  
SimonLee

I can't get running total to calculate

Recommended Posts

Do anyone know what is wrong and what I can do to fix this? Thanks. <?xml version="1.0" encoding="UTF-8" ?> <?xml-stylesheet type="text/xsl" href="cars.xsl"?> <cars> <car id="001"> <name>Lexus</name> <qty>5</qty> <price>12000</price> </car> <car id="002"> <name>Honda</name> <qty>14</qty> <price>22299</price> </car> <car id="003"> <name>BMW</name> <qty>23</qty> <price>3299</price> </car> </cars> <?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" version="4.0" /> <!-- start of root template --> <xsl:template match="/"> <html> <head> <title>Car Sales</title> </head> <body> <table border="1" cellpadding="5" cellspacing="5"> <tr> <!-- row 1 --> <th colspan="2">Cars List</th> </tr> <tr> <!-- row 2 --> <th>Cars:</th> <td><xsl:value-of select="count(//car)"/></td> </tr> <tr> <!-- row 3 --> <th>Quantity:</th> <td><xsl:value-of select="sum(//qty)"/></td> </tr> <tr> <!-- row 4 --> <th>Total Sale:</th> <td> <xsl:call-template name="calctotalsales"/> </td> </tr> </table> </body> </html> </xsl:template> <!-- end of root template --> <!-- start of calctotalsales template --> <xsl:template name="calctotalsales"> <xsl:param name="list"/> <xsl:param name="total" select="0"/> <!-- calculate the total car cost --> <xsl:choose> <xsl:when test="$list"> <!-- calculate the first car sales --> <xsl:variable name="firstsales" select="$list[1]/cars/car/qty * $list[1]/cars/car/price" /> <!-- call the totalcost template --> <xsl:call-template name="calctotalsales"> <xsl:with-param name="list" select="$list[position() > 1]" /> <xsl:with-param name="total" select="$firstsales + $total" /> </xsl:call-template> </xsl:when> <!-- list of records finished, time to display the total cost --> <xsl:otherwise> <xsl:value-of select="format-number($total, '$#,#00.00')" /> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- end of calctotalsales template --> </xsl:stylesheet>

Share this post


Link to post
Share on other sites

The problem is in your <xsl:param name="list" /> where you are not selecting/poining to anything and you are checking it into choose which is always returning otherwise. Here is the solution:<?xml version="1.0" ?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" version="4.0" /> <!-- start of root template --> <xsl:template match="/"> <html> <head> <title>Car Sales</title> </head> <body> <table border="1" cellpadding="5" cellspacing="5"> <tr> <!-- row 1 --> <th colspan="2">Cars List</th> </tr> <tr> <!-- row 2 --> <th>Cars:</th> <td><xsl:value-of select="count(//car)"/></td> </tr> <tr> <!-- row 3 --> <th>Quantity:</th> <td><xsl:value-of select="sum(//qty)"/></td> </tr> <tr> <!-- row 4 --> <th>Total Sale:</th> <td> <xsl:value-of select="format-number(sum(for $EachCar in cars/car return $EachCar/qty * $EachCar/price), '$#,#00.00')"/> <!-- <xsl:call-template name="calctotalsales"/>--> </td> </tr> </table> </body> </html> </xsl:template></xsl:stylesheet>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...