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 firstTo: NULL Date: 2/3/2006 7:48:00 AM Martin Honnen wrote: > Kindler Chase wrote: > > >> *Desired Result* >> I need to take the result of every (price div conversion_rate) and >> then sum the total of their results.. >> >> This obviously throws an error: >> <xsl:value-of >> select ="sum(root/order/price div root/order/conversion_rate)" /> > > That is not the obvious, the problem is that XPath 1.0 only selects > existing nodes it can't construct new ones so the expression you call > sum on does not yield any node set but rather a number. > XPath 2.0 is more expressive, it can construct new sequences > (sequences are part of the new data model used instead of node set) > so in XPath 2.0 you could simply do > > <xsl:value-of > select="sum(for $order in root/order return $order/price div > $order/conversion_rate)" /> > > which yields > > 356.89890710382514 > > however so your math below is somehow flawed. :D Guess I pressed 2 instead of 3 :D Sorry 'bout that. Thank you very much Martin! I'll take a go at this in a couple of hours and let you know how it goes. >> In the above example, I need a result such as: >> 100 / 1.20 = 83.33 >> 85 / 1.12 = 75.89 >> 125 / 1.12 = 111.61 >> 105 / 1.22 = 86.1 >> ------------------------ >> Total: 256.93 <--------- desired result > > 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> -- kindler chase http://www.ncubed.com Home of SuperInvoice's Fortress of BeanCounters news://news.ncubed.com/support n3 Support Group | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
