varuog
varuog

Reputation: 3081

finite looping in xslt stylesheet

<data id="5">
</data>



<xsl:template match="data">
 <div class="holder">
  <!-- Print this as many time as id attribute have -->
  <b>Print this 5 times</b>
  <!-- block end -->
 </div>
</xsl:template>

I want to print some html code fragment 5 times using xslt. how to do it?

Upvotes: 0

Views: 71

Answers (1)

Matthew Green
Matthew Green

Reputation: 10401

You can build a recursive template that runs until it matches the id in your element.

Here is what that could look like:

<xsl:template name="printLines">
    <xsl:param name="num" />
    <xsl:param name="id" />
    <xsl:if test="not($num = $id)">
        <b>Print this line</b>
        <xsl:call-template name="printLines">
            <xsl:with-param name="num" select="$num + 1" />
            <xsl:with-param name="id" select="$id" />
        </xsl:call-template>
    </xsl:if>
</xsl:template>

And you can call it in your current template like this:

<xsl:template match="data">
  <div class="holder">
    <xsl:call-template name="printLines">
        <xsl:with-param name="num" select="number('0')" />
        <xsl:with-param name="id" select="@id" />
    </xsl:call-template>
  </div>
</xsl:template>

Upvotes: 1

Related Questions