xsl-list
[Top] [All Lists]

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

2006-09-05 09:15:26
On 9/5/06, hanged(_dot_)man(_at_)lycos(_dot_)com 
<hanged(_dot_)man(_at_)lycos(_dot_)com> 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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--