Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Summarizing a total when needing individual calcuations first

From: "Kindler Chase" <support@------_--_------.--->
To: 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() &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>



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






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