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: "andrew welch" <andrew.j.welch@xxxxxxxxx>
Date: Tue, 5 Sep 2006 17:15:10 +0100
On 9/5/06, hanged.man@xxxxxxxxx <hanged.man@xxxxxxxxx> wrote:
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>
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?

It's as the recursion unravels - it essentially adds it from last item to the first. It's a great example of a mind-bending recursive programming :)

cheers
andrew

Current Thread