RE: [xsl] Repeating a string

Subject: RE: [xsl] Repeating a string
From: "Andrew Welch" <awelch@xxxxxxxxxxxxxxx>
Date: Thu, 6 Jun 2002 09:00:36 +0100


Filipe Correia wrote:

>I would do something like this:
>
><xsl:template match="some/node/selection">
>	<xsl:call-template name="repeater">
>		<xsl:with-param i="20"/>
>		<xsl:with-param str="Hello"/>
>	</xsl:call-template>
></xsl:template>
>
><xsl:template name="repeater">
>	<xsl:param name="i"/>0</xsl:param> <!-- defaul value-->
>	<xsl:if test="$i > 0">
>		<xsl:call-template name="repeater">
>			<xsl:with-param i="$i - 1"/>
>			<xsl:with-param str="$str"/>
>		</xsl:call-template>
>		<xsl:value-of select="$str"/>	
>	</xsl:if>
></xsl:template>
>
>        Cheers,
>            Filipe


Its the right idea but its wrong in several places, firstly your
with-params:

 <xsl:with-param str="Hello"/>

Should be:

 <xsl:with-param name="str" select="'Hello'"/>

There is no param to take 'str' in "repeater", and the value-of is below
your call-template so it won't output anything!

>PS: I have not tested this but I think it would work.

Its normally a good idea to test your code before you post it :) 

Here's a working version:

<xsl:template match="/">
	<xsl:call-template name="repeater">
		<xsl:with-param name="theString" select="'Hello'"/>
		<xsl:with-param name="numberOfTimes" select="20"/>
	</xsl:call-template>
</xsl:template>

<xsl:template name="repeater">
	<xsl:param name="theString" select="''"/>
	<xsl:param name="numberOfTimes" select="0"/>
	<xsl:if test="$numberOfTimes > 0">
	   <xsl:value-of select="$theString"/>
	   <xsl:call-template name="repeater">
	     <xsl:with-param name="theString" select="$theString"/>
	     <xsl:with-param name="numberOfTimes" select="$numberOfTimes
- 1"/>
	   </xsl:call-template>		
	</xsl:if>
</xsl:template>


cheers
andrew

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.350 / Virus Database: 196 - Release Date: 17/04/2002
 

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


Current Thread