Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Sum over computed value ? [Thread Next] Re: Sum over computed value ?To: NULL Date: 1/11/2006 2:42:00 PM Thomas Zangl wrote: > <invoice> > <billitems> > > <item> > <PName>Kiwi</PName> > <PPrice>0.900</PPrice> > <PId>1</PId> > <BCQuant>15</BCQuant> > > </item> > > <item> > <PName>Apfel</PName> > <PPrice>0.500</PPrice> > <PId>3</PId> > <BCQuant>10</BCQuant> > > </item> > > </billitems> > </invoice> > > Now I want to have the sum of > /invoice/billitems/item/BCQuant*/invoice/billitems/item/PPrice XPath 2.0 with a for..in expression has the expressive power to do that easily and with one expression e.g. <xsl:value-of select="sum(for $item in invoice/billitems/item return $item/PPrice * $item/BCQuant)" /> If you want to do it with XSLT 1.0 and XPath 1.0 then you need to write a recursive template where you pass in the item elements, compute the BCQuant * PPPrice for the first item and pass that value and the remaining items to the template until all items have been processed: <xsl:template name="computeTotal"> <xsl:param name="items" /> <xsl:param name="currentTotal" select="0" /> <xsl:choose> <xsl:when test="$items"> <xsl:call-template name="computeTotal"> <xsl:with-param name="items" select="$items[position() > 1]" /> <xsl:with-param name="currentTotal" select="$items[1]/PPrice * $items[1]/BCQuant + $currentTotal" /> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$currentTotal" /> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="/"> <html> <head> <title>sum</title> </head> <body> <p>Sum is: <xsl:call-template name="computeTotal"> <xsl:with-param name="items" select="invoice/billitems/item" /> </xsl:call-template> </p> </body> </html> </xsl:template> -- Martin Honnen http://JavaScript.FAQTs.com/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
