RE: [xsl] Two versions of sum over node list by recursion--why and how does second one work?

Subject: RE: [xsl] Two versions of sum over node list by recursion--why and how does second one work?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 5 Sep 2006 17:28:05 +0100
Perhaps the algorithm is clearer when rewritten in XSLT 2.0:

<xsl:stylesheet 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
   version="1.0">

<xsl:function name="f:total-sales-value" as="xs:decimal">
   <xsl:param name="list" as="xs:decimal*"/>
   
   <xsl:choose>
      <xsl:when test="exists($list)">
         <xsl:sequence select="$list[1]/(sales*price) + 
 
f:total-sales-value($list[position()!=1])"/>
      </xsl:when>
      <xsl:otherwise>
         <xsl:sequence select="0"/>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<xsl:template match="/">   
Total sales value is: <xsl:value-of 
       select="format-number(f:total-sales-value(//book)), '$#.00')"/>   
</xsl:template>
</xsl:stylesheet> 

But of course in 2.0 you don't need recursion, you can write

sum(//book/(sales*price)) 

Michael Kay
http://www.saxonica.com/


> -----Original Message-----
> From: hanged.man@xxxxxxxxx [mailto:hanged.man@xxxxxxxxx] 
> Sent: 05 September 2006 16:53
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Two versions of sum over node list by 
> recursion--why and how does second one work?
> 
> Here are two examples of summing a node list by recursion: 
> the first sends the result of the addition back to itself by 
> a parameter: the second does not. The first was posted by G. 
> Ken Holman--the second is from Michael Kay s _XSLT 
> Programmer's Reference_. 
> 
> I know that Mr. Kay's code works...I just can't figure out _why_. 
> 
> FIRST EXAMPLE:
> 
> <xsl:template match="/">
>   <xsl:variable name="result-text">
>     <xsl:text/>0<xsl:apply-templates select="/*/RECEIPT[1]"
> mode="equation"/>
>   </xsl:variable>
>   <xsl:variable name="result" select="number($result-text)"/>
> 
>   <xsl:value-of select="$result"/>
> </xsl:template>
> 
> <xsl:template match="RECEIPT" mode="equation">
>   <xsl:param name="result" select="0"/>
>   <xsl:variable name="this" select="$result + ( qtyRcpt * line/pr )"/>
>   <xsl:choose>
>     <xsl:when test="following-sibling::RECEIPT">
>       <xsl:apply-templates select="following-sibling::RECEIPT[1]"
>                            mode="equation">
>         <xsl:with-param name="result" select="$this"/>
>       </xsl:apply-templates>
>     </xsl:when>
>     <xsl:otherwise>
>       <xsl:value-of select="$this"/>
>     </xsl:otherwise>
>   </xsl:choose>
> </xsl:template>
> 
> </xsl:stylesheet>
> 
> SECOND EXAMPLE:
> 
> <xsl:stylesheet 
>    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>    version="1.0">
> 
> <xsl:template name="total-sales-value">
>    <xsl:param name="list"/>
>    <xsl:choose>
>       <xsl:when test="$list">
>          <xsl:variable name="first" select="$list[1]"/>
>          <xsl:variable name="total-of-rest">
>             <xsl:call-template name="total-sales-value">
>                <xsl:with-param name="list" 
> select="$list[position()!=1]"/>
>             </xsl:call-template>
>          </xsl:variable>
>          <xsl:value-of select="$first/sales * $first/price + 
> $total-of-rest"/>
>       </xsl:when>
>       <xsl:otherwise>0</xsl:otherwise>
>    </xsl:choose>
> </xsl:template>
> 
> 
> <xsl:template match="/">   
>    <xsl:variable name="total">
>         <xsl:call-template name="total-sales-value">
>             <xsl:with-param name="list" select="//book"/>
>         </xsl:call-template>
>    </xsl:variable>
> Total sales value is: <xsl:value-of select="format-number($total,
> '$#.00')"/>   
> </xsl:template>
> </xsl:stylesheet>
> 
> QUESTION:
> 
> In Mr. Kay's example, why does the variable $total-of-rest 
> indeed contain the total and not just the result of the 
> addition in the last recursion?  
> 
> 
> 
> --------------------------------------------------------------------
> mail2web - Check your email from the web at
> http://mail2web.com/ .

Current Thread