Jump to content

I can't get running total to calculate


SimonLee
 Share

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>

Link to comment
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>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...