sgubert Posted October 29, 2010 Posted October 29, 2010 Hi, Unfortunately I have other problem that seems more complex.Using the same example: <CATEGORIES> <CATEGORY ID="CAT1"> <ITEM VALUE="1" QUANTITY="2"/> <ITEM VALUE="1" QUANTITY="4"/> <ITEM VALUE="1" QUANTITY="1"/> <ITEM VALUE="1" QUANTITY="3"/> </CATEGORY> <CATEGORY ID="CAT2"> <ITEM VALUE="2" QUANTITY="2"/> <ITEM VALUE="2" QUANTITY="3"/> <ITEM VALUE="2" QUANTITY="5"/> <ITEM VALUE="2" QUANTITY="1"/> </CATEGORY></CATEGORIES> I need calculate the sum of VALUE * QUANTITY for each category and after sum this total of each category.CAT1=10CAT2=22Total Categories: 32Is there a way to do this?Thanks :)Samuel
Martin Honnen Posted October 29, 2010 Posted October 29, 2010 XSLT/XPath 2.0 can do that easily: <xsl:template match="CATEGORY"> <xsl:value-of select="sum(for $item in ITEM return $item/@VALUE * $item/@QUANTITY)"/></xsl:template> With XSLT 1.0 you need to write a named template taking the ITEM elements as parameter and the current total initialized to 0, then compute the product for the first item and call the template again with the remaining ITEM elements and the current total.
sgubert Posted November 4, 2010 Author Posted November 4, 2010 XSLT/XPath 2.0 can do that easily:<xsl:template match="CATEGORY"> <xsl:value-of select="sum(for $item in ITEM return $item/@VALUE * $item/@QUANTITY)"/></xsl:template> With XSLT 1.0 you need to write a named template taking the ITEM elements as parameter and the current total initialized to 0, then compute the product for the first item and call the template again with the remaining ITEM elements and the current total. Can you show me how resolve this problem using XSLT 1.0??I am a newbie with XSL.. :)Thanks
Martin Honnen Posted November 4, 2010 Posted November 4, 2010 Here is an example stylesheet <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html" indent="yes"/> <xsl:template name="item-sum"> <xsl:param name="items"/> <xsl:param name="total" select="0"/> <xsl:choose> <xsl:when test="not($items)"> <xsl:value-of select="$total"/> </xsl:when> <xsl:otherwise> <xsl:call-template name="item-sum"> <xsl:with-param name="items" select="$items[position() > 1]"/> <xsl:with-param name="total" select="$items[1]/@VALUE * $items[1]/@QUANTITY + $total"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="CATEGORIES"> <table> <thead> <tr> <th>category id</th> <th>sum</th> </tr> </thead> <tbody> <xsl:apply-templates/> </tbody> <tbody> <tr> <td>total</td> <td> <xsl:call-template name="item-sum"> <xsl:with-param name="items" select="CATEGORY/ITEM"/> </xsl:call-template> </td> </tr> </tbody> </table> </xsl:template> <xsl:template match="CATEGORY"> <tr> <td> <xsl:value-of select="@ID"/> </td> <td> <xsl:call-template name="item-sum"> <xsl:with-param name="items" select="ITEM"/> </xsl:call-template> </td> </tr> </xsl:template> </xsl:stylesheet> When applied to your sample input, the result is <table> <thead> <tr> <th>category id</th> <th>sum</th> </tr> </thead> <tbody> <tr> <td>CAT1</td> <td>10</td> </tr> <tr> <td>CAT2</td> <td>22</td> </tr> </tbody> <tbody> <tr> <td>total</td> <td>32</td> </tr> </tbody></table>
Recommended Posts
Archived
This topic is now archived and is closed to further replies.