Take the 2-minute tour ×

I have a template with a parameter. How can I print something n times?

n is the value of the parameter. I must use XSLT 1.0...

There's a cleaner way insted of this:

<xsl:for-each select="//*[position() &lt;= $count]">&#x9;</xsl:for-each>
share|improve this question
<xsl:for-each... ? Show what you have tried so far. –  Jim Garrison Feb 6 '13 at 9:55
possible duplicate of How I can repeat an action X times with XSLT –  phihag Feb 6 '13 at 10:00

1 Answer 1

up vote 2 down vote accepted

Your approach will work if your source XML has $count elements, but I wouldn't say that's a very good way to go about it. This is a lot more verbose, but I'd suggest defining a template like this:

 <xsl:template name="RepeatValue">
    <xsl:param name="times" />
    <xsl:param name="value" />

    <xsl:if test="$times > 0">
        <xsl:value-of select="$value" />
        <xsl:call-template name="RepeatValue">
           <xsl:with-param name="times" select="$times - 1" />
           <xsl:with-param name="value" select="$value" />

Then you would call this template using:

<xsl:call-template name="RepeatValue">
   <xsl:with-param name="times" select="$count" />
   <xsl:with-param name="value" select="'&#x9;'" />
share|improve this answer

Your Answer


