RE: [xsl] Calling a template recursively

Subject: RE: [xsl] Calling a template recursively
From: "Andrew Welch" <ajwelch@xxxxxxxxxxxxxxx>
Date: Mon, 5 Jul 2004 07:20:32 +0100
> Hello List,
>           I am using following template to display a
> <br> tag after every 25th character of the string. But
> that does not seem to work.
> 
> <xsl:template name="normaliseString">
> 		<xsl:param name="releaselevel"/>
> 		<xsl:variable name="temp" 
> select="substring($releaselevel,1,25)"/>
> 		<xsl:value-of select="$temp"/>
> 		<xsl:text>&lt;br&gt; </xsl:text>
> 		<xsl:if test="string-length($releaselevel) >25">
> 			
> 			<xsl:variable name="temp2" 
> select="substring($temp,26,string-length($releaselevel))"/>
> 			<xsl:value-of select="$temp2"/>
> 			<xsl:call-template name="normaliseString">
> 				<xsl:with-param  name="releaselevel"
> select="@temp2"/>
> 				</xsl:call-template>
> 		</xsl:if>
> 		
> 	</xsl:template>
> 
> any suggestion.


Hi,

You forgot to say why your template 'didn't seem to work' so I've had to
guess ;-)  I think you just want the standard tail-recursive string
processing template.  This involves ensuring the recursive part of the
template is the last operation in the template, allowing the compiler to
flatten the recursion into a set of if-elses (which is more memory
efficient).  

<xsl:template name="normaliseString">
  <xsl:param name="releaselevel"/>
  <xsl:choose>
    <xsl:when test="string-length($releaselevel) &lt; 25">
      <xsl:value-of select="$releaselevel"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="substring($releaselevel, 1, 25)"/>
      <br/>
      <xsl:call-template name="normaliseString">
        <xsl:with-param name="releaselevel"
select="substring($releaselevel, 25)"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

cheers
andrew

Current Thread