This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
RE: sum of products
- To: "'xsl-list at lists dot mulberrytech dot com'" <xsl-list at lists dot mulberrytech dot com>
- Subject: RE: [xsl] sum of products
- From: Adam Van Den Hoven <Adam dot Hoven at bluezone dot net>
- Date: Wed, 10 Oct 2001 07:37:07 -0700
- Reply-To: xsl-list at lists dot mulberrytech dot com
Actually that is not entirely true, since I've done just that without a
named template to do the work. But it is recursive. Given your data and the
following:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="dataid" select="'2'"/>
<xsl:output method="text" indent="no" />
<xsl:template match="/items">
<xsl:apply-templates select="(item/data[@id=$dataid])[1]"/>
</xsl:template>
<xsl:template match="data">
<xsl:param name="pSum" select="0"/>
<xsl:variable name="lSum" select="$pSum + (number(a) *
number(b))"/>
<xsl:variable name="remaining"
select="(following-sibling::data |
parent::item/following-sibling::item/data[@id=$dataid])"/>
<xsl:choose>
<xsl:when test="count($remaining) > 0">
<xsl:apply-templates select="$remaining[1]">
<xsl:with-param name="pSum"
select="$lSum"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="done">
<xsl:with-param name="pSum"
select="$lSum"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="done">
<xsl:param name="pSum" select="0"/>
<xsl:text>The Sum of the Products of data items with @id =
</xsl:text>
<xsl:value-of select="$dataid"/>
<xsl:text> is:</xsl:text>
<xsl:value-of select="$pSum"/>
</xsl:template>
</xsl:stylesheet>
I got the following output:
The Sum of the Products of data items with @id = 2 is:310
There may be a better way that to use the variable remaining but I built
this pretty quickly. By the way, the select for $remaining is written so
that you could have multiple datas with the same id in one item.
Adam
> -----Original Message-----
> From: David Carlisle [mailto:davidc@nag.co.uk]
> Sent: Wednesday, October 10, 2001 6:00 AM
> To: xsl-list@lists.mulberrytech.com
> Subject: Re: [xsl] sum of products
>
>
>
> > Is there another method that does not use node-set?
>
> yes write a recursive named template that accumulates the sum,
> there's examples in this list archives, if not the faq.
> basically just add a*b to the value passed in as the
> parameter then call
> yourself on a following node, passing on this new value.
>
> David
>
> _____________________________________________________________________
> This message has been checked for all known viruses by Star Internet
> delivered through the MessageLabs Virus Scanning Service. For further
> information visit http://www.star.net.uk/stats.asp or
> alternatively call
> Star Internet for details on the Virus Scanning Service.
>
> XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list