[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: sum of products

Subject: RE: sum of products
From: Adam Van Den Hoven <Adam.Hoven@xxxxxxxxxxxx>
Date: Wed, 10 Oct 2001 07:37:07 -0700
sum of products
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) &gt; 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@xxxxxxxxx]
> Sent: Wednesday, October 10, 2001 6:00 AM
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Re:  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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.