Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Re: Summarizing a total when needing individual calcuations first >Thread Next - Re: Summarizing a total when needing individual calcuations first Re: Summarizing a total when needing individual calcuations firstTo: NULL Date: 2/3/2006 11:31:00 AM "Martin Honnen" <mahotrash@y...> wrote in message news:eVhFeYMKGHA.360@T...... > With XSLT 1.0 you need to write a recursive template processing the > order elements and summing up what you want: > > <xsl:stylesheet > xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > version="1.0"> > > <xsl:output method="text" /> > > <xsl:template match="/"> > <xsl:call-template name="sumOrders"> > <xsl:with-param name="orders" select="root/order" /> > </xsl:call-template> > </xsl:template> > > <xsl:template name="sumOrders"> > <xsl:param name="orders" /> > <xsl:param name="currentSum" select="0" /> > <xsl:choose> > <xsl:when test="$orders"> > <xsl:call-template name="sumOrders"> > <xsl:with-param name="orders" > select="$orders[position() > 1]" /> > <xsl:with-param name="currentSum" > select="$currentSum + $orders[1]/price div $orders[1]/conversion_rate"/> > </xsl:call-template> > </xsl:when> > <xsl:otherwise> > <xsl:value-of select="$currentSum" /> > </xsl:otherwise> > </xsl:choose> > </xsl:template> > > </xsl:stylesheet> Hi Martin, That worked great! Thank You. As a follow up, I need to use the template you created to convert several variants of the "price" node. Is there a way assign a variable node instead of just using "price"? I've tried the following, but get an error: "Node test expected" which referneces the parameter "nodeToConvert" in my sample. It's the same template you provided with the addtion of the "nodeToConvert" parameter. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="text" /> <xsl:template match="/"> <xsl:call-template name="sumOrders"> <xsl:with-param name="orders" select="root/order" /> <xsl:with-param name="nodeToConvert" select="rebate" /> </xsl:call-template> </xsl:template> <xsl:template name="sumOrders"> <xsl:param name="orders" /> <xsl:param name="nodeToConvert" /> <xsl:param name="currentSum" select="0" /> <xsl:choose> <xsl:when test="$orders"> <xsl:call-template name="sumOrders"> <xsl:with-param name="orders" select="$orders[position() > 1]" /> <xsl:with-param name="nodeToConvert" select="$nodeToConvert" /> <xsl:with-param name="currentSum" select="$currentSum + $orders[1]/$nodeToConvert div $orders[1]/conversion_rate"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$currentSum" /> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> ================ sample xml ==================== <root> <order> <price>100</price> <rebate>2</rebate> <conversion_rate>1.20</conversion_rate> </order> <order> <price>85</price> <rebate>5</rebate> <conversion_rate>1.12</conversion_rate> </order> <order> <price>125</price> <rebate>7</rebate> <conversion_rate>1.12</conversion_rate> </order> <order> <price>105</price> <rebate>3</rebate> <conversion_rate>1.22</conversion_rate> </order> </root> ================== end xml ================= Regards, -- Kindler Chase | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
