[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: [xml-dev] calculating list of values using variables
- From: Paul Brown <Paul.Brown@spirit-soft.com>
- To: xml-dev@lists.xml.org
- Date: Thu, 20 Sep 2001 10:05:26 +0100
here's my feeble effort :)
<xsl:transform xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
version='1.0'>
<xsl:template match='/'>
<xsl:variable name='result'>
<xsl:call-template name='sum'>
<xsl:with-param name='amounts'
select='/detailss/details/detail/amount' />
<xsl:with-param name='quants'
select='/detailss/details/detail/quantity' />
<xsl:with-param name='tally' select='0' />
</xsl:call-template>
</xsl:variable>
<result><xsl:value-of select='$result' /></result>
</xsl:template>
<xsl:template name='sum'>
<xsl:param name='amounts'/>
<xsl:param name='quants'/>
<xsl:param name='running'/>
<xsl:choose>
<xsl:when test='count($amounts)!=0'>
<xsl:call-template name='sum'>
<xsl:with-param name='amounts' select='$amounts[position()>1]'
/>
<xsl:with-param name='quants' select='$quants[position()>1]' />
<xsl:with-param name='tally'
select='$amounts[1]*$quants[1]+$tally' />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select='$tally'/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:transform>
and i hope your document looks like this ...
<?xml version='1.0'?>
<detailss>
<details>
<detail>
<amount>1</amount>
<quantity>5</quantity>
</detail>
</details>
<details>
<detail>
<amount>2</amount>
<quantity>1</quantity>
</detail>
<detail>
<amount>3</amount>
<quantity>2</quantity>
</detail>
</details>
</detailss>
-----Original Message-----
From: Ian Rogers [mailto:ian.rogers@net800.co.uk]
Sent: Wednesday, September 19, 2001 12:02 PM
To: 'Xml-Dev
Subject: [xml-dev] calculating list of values using variables
can anyone help.
I sent a post out before about trying to use variables, and have been
trying
some code but it comes up with the error "Keyword xsl:stylesheet may not
contain PCDATA nodes."
I have a set of details which contain an amount and a quantity and i
want to
calculate the total amount for each set of details. I can calculate that
and
place it in a variable, but i now want to calculate the total for all
the
sets of details, giving an overall amount for all the sets of details
<xsl:variable name="Items" select="//details"/>
<xsl:variable name="TotalValue">
<xsl:call-template name="Total">
<xsl:with-param name="Items" select="$Items"/>
<xsl:with-param name="RunningTotal" select="0"/>
</xsl:call-template>
</xsl:variable>
Total = <xsl:value-of
select="format-number($TotalValue, '####0.0#')"/>
<xsl:template name="Total">
<xsl:param name="Items"/>
<xsl:param name="RunningTotal"/>
<xsl:choose>
<xsl:when test="not($Items)">
<xsl:copy-of select="$RunningTotal"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="CurrentTotal"
select="$RunningTotal + ($Items[1]/amount *
$Items[1]/quantity)"/>
<xsl:call-template name="Total">
<xsl:with-param name="Items" select="$Items[position()>1]"/>
<xsl:with-param name="RunningTotal" select="$CurrentTotal"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
"then i am calling the rest of my code to generate the table by using a
template and matching on the root node. I don't know if this could be
causing a problem?
<xsl:template match="/">
i would be very grateful if anyone could see what i am doing wrong or if
there is another way to calculate the total amount
thanks
Ian
-----------------------------------------------------------------
The xml-dev list is sponsored by XML.org <http://www.xml.org>, an
initiative of OASIS <http://www.oasis-open.org>
The list archives are at http://lists.xml.org/archives/xml-dev/
To subscribe or unsubscribe from this elist use the subscription
manager: <http://lists.xml.org/ob/adm.pl>