Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Summarizing a total when needing individual calcuations first

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 2/3/2006 2:31:00 PM

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.

> 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() &gt; 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>

-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent