This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Summing a Calculation
- To: <xsl-list at lists dot mulberrytech dot com>
- Subject: Re: [xsl] Summing a Calculation
- From: "Frank Chen" <frank63 at ms5 dot hinet dot net>
- Date: Fri, 22 Jun 2001 08:51:06 +0800
- References: <6514DE680737F449885673E7895ED250164427@zeus.nimble.com>
- Reply-To: xsl-list at lists dot mulberrytech dot com
Hello, Mark:
I think the following stylesheet can get what you want, not so sure whether
it is the most efficient:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
>
<xsl:output method="text" encoding="iso-8859-1"/>
<xsl:template match="order">
<xsl:for-each select="item">
<xsl:value-of select="id"/><xsl:text> </xsl:text>
<xsl:value-of select="price"/><xsl:text> </xsl:text>
<xsl:value-of select="quantity"/><xsl:text> </xsl:text>
<xsl:value-of select="price * quantity"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
TOTAL:<xsl:text/>
<xsl:call-template name="total">
<xsl:with-param name="num" select="count(item)"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="total">
<xsl:param name="num"/>
<xsl:choose>
<xsl:when test="$num > 1">
<xsl:variable name="accum">
<xsl:call-template name="total">
<xsl:with-param name="num" select="$num - 1"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$accum + item[$num]/price *
item[$num]/quantity"/>
</xsl:when>
<xsl:when test="$num = 1">
<xsl:value-of select="item[$num]/price * item[$num]/quantity"/>
</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
It outputs:
1 1.34 4 5.36
2 5.77 3 17.31
5 12.99 1 12.99
TOTAL:35.66
----- Original Message -----
From: "Mark Swardstrom" <mark@nimble.com>
To: <xsl-list@lists.mulberrytech.com>
Sent: Friday, June 22, 2001 7:19 AM
Subject: RE: [xsl] Summing a Calculation
> I need to multiply each line by the quantity to get the amount per item.
>
> Visually - it would look like this
>
> ID Price Qty Amount
> 1 1.34 4 5.36
> 2 5.77 3 17.31
> 5 12.99 1 12.99
> TOTAL 35.66
>
>
> That TOTAL number (35.66) is the one I'm hoping to generate in a single
> line.
>
> Here's my current solution, but would like to reduce:
>
> XML
> <order>
> <item>
> <id>1</id>
> <price>1.34</price>
> <quantity>4</quantity>
> </item>
> <item>
> <id>2</id>
> <price>5.77</price>
> <quantity>3</quantity>
> </item>
> <item>
> <id>5</id>
> <price>12.99</price>
> <quantity>1</quantity>
> </item>
> </order>
>
>
>
> XSL
> <xsl:variable name="total">
> <xsl:call-template name="calcTotal">
> <xsl:with-param name="sum" select="'0'"/>
> <xsl:with-param name="node" select="item[1]"/>
> </xsl:call-template>
> </xsl:variable>
> <xsl:value-of select="format-number($total, '$#,##0.00')"/>
>
>
> <xsl:template name="calcTotal">
> <xsl:param name="sum"/>
> <xsl:param name="node"/>
>
> <xsl:choose>
> <xsl:when test="$node/following-sibling::item">
> <xsl:call-template name="calcTotal">
> <xsl:with-param name="sum" select="$sum + ($node/price *
> $node/quantity)"/>
> <xsl:with-param name="node"
> select="$node/following-sibling::item[1]"/>
> </xsl:call-template>
> </xsl:when>
> <xsl:otherwise>
> <xsl:value-of select="$sum + ($node/price * $node/quantity)"/>
> </xsl:otherwise>
> </xsl:choose>
> </xsl:template>
>
>
> -----Original Message-----
> From: bryan.s.schnabel@exgate.tek.com
> [mailto:bryan.s.schnabel@exgate.tek.com]
> Sent: Thursday, June 21, 2001 3:54 PM
> To: xsl-list@lists.mulberrytech.com
> Subject: RE: [xsl] Summing a Calculation
>
>
> This works:
>
> <xsl:template match="order">
> <b>
> <xsl:text>Total Price: </xsl:text>
> <xsl:value-of select="sum(item/price)"/>
> </b>
> </xsl:template>
>
> -----Original Message-----
> From: Mark Swardstrom [mailto:mark@nimble.com]
> Sent: Thursday, June 21, 2001 11:55 AM
> To: xsl-list@lists.mulberrytech.com
> Subject: [xsl] Summing a Calculation
>
>
> I'm trying to get the sum of a purchase order, which seems simple, but I
> can't find an easy solution.
>
> Consider the following XML:
>
> <order>
> <item>
> <id>1</id>
> <price>1.34</price>
> <quantity>4</quantity>
> </item>
> <item>
> <id>2</id>
> <price>5.77</price>
> <quantity>3</quantity>
> </item>
> <item>
> <id>5</id>
> <price>12.99</price>
> <quantity>1</quantity>
> </item>
> </order>
>
> I was trying to do something along these lines:
>
> <xsl:template match="order">
> <xsl:apply-templates select="item"/>
> <b>Total price: <xsl:value-of select="sum(item/price *
> item/quantity)"/></b>
> </xsl:template>
>
> To get a total amount for the entire order.
>
> But that gives the following error.
>
> Can not convert #NUMBER to a NodeList!
>
> Before I go down the (seemingly too complex) recursive loop approach, I
was
> wondering if anyone else had another idea.
>
> Thanks.
>
> - Mark
>
>
> XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
>
> XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
>
> XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list