Re: [xsl] apply template within call template

Subject: Re: [xsl] apply template within call template
From: Joerg Pietschmann <joerg.pietschmann@xxxxxx>
Date: Fri, 11 Jan 2002 10:06:39 +0100
"cutlass" <cutlass@xxxxxxxxxxx> wrote:
[rearranged]
> sometimes people confuse recursion with circular references, the above is a
> circular reference.

Jim, i thought you knew better! The code snippet
  <xsl:template name="test">
    <xsl:call-template name="test"/>
  </xsl:template>
is perfectly legal, and it's recursion. Of course, it would
run forever provided infinite ressources. In order to make it
useful, a method for termination should be provided:
for example a counter:
  <xsl:template name="test">
    <xsl:param name="counter"/>
    <xsl:if test="$counter > 0">
      <xsl:call-template name="test">
        <xsl:with-param name="counter" select="$counter - 1"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
or processing a node set:
  <xsl:template name="test">
    <xsl:param name="nodes"/>
    <xsl:if test="count($nodes) > 1">
      <xsl:call-template name="test">
        <xsl:with-param name="nodes" select="$nodes[position() > 1]"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

> for example x=x+1 could be thought of as recursive, but in actuality it goes
> on 'forever'.... which means that the function never bottoms out...
> recursion always bottoms out ( c'mon u GEB readers.... ).
Wrong analogy. BTW i've never met anyone who thought of
x=x+1 as recursive (it's a perfectly valid non-recursive
expression in most programming languages and something
quite different in mathematics).

As for circular references, i think in XSLT they can be produced
only with global variables
  <xsl:variable name="foo">
    <xsl:copy-of select="$bar"/>
  </xsl:variable>
  <xsl:variable name="bar">
    <xsl:copy-of select="$foo"/>
  </xsl:variable>
It is of course a fatal error to do this.

HTH
J.Pietschmann

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread