To loop in XSLT you need to call a template recursively.
In past I have done this using two approaches:
Option-1: While Loop
Logic is similar to a while loop. So the counter is decremented till it is greater than 0.
Option-2: For Loop
Here counter is incremented from a initial count till it reaches the desired loop count.
In past I have done this using two approaches:
Option-1: While Loop
Logic is similar to a while loop. So the counter is decremented till it is greater than 0.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="no" />
<xsl:template match="/">
<LoopInput>
<xsl:call-template name="CreateLoop">
<xsl:with-param name="count" select="${numLoop}" />
</xsl:call-template>
</LoopInput>
</xsl:template>
<xsl:template name="CreateLoop">
<xsl:param name="count" />
<xsl:if test="$count > 0">
<xsl:call-template name="CreateLoop">
<xsl:with-param name="count" select="$count - 1" />
</xsl:call-template>
<Line>
<xsl:text disable-output-escaping="no">Line Data: </xsl:text>
<xsl:value-of select="$count" disable-output-escaping="no" />
</Line>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Option-2: For Loop
Here counter is incremented from a initial count till it reaches the desired loop count.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="no" />
<xsl:template match="/">
<LoopInput>
<xsl:call-template name="CreateLoop">
<xsl:with-param name="MaxLoop" select="${numLoop}" />
<xsl:with-param name="iLoop" select="1" />
</xsl:call-template>
</LoopInput>
</xsl:template>
<xsl:template name="CreateLoop">
<xsl:param name="MaxLoop" />
<xsl:param name="iLoop" />
<Line>
<xsl:text disable-output-escaping="no">Line Data: </xsl:text>
<xsl:value-of select="$iLoop" disable-output-escaping="no" />
</Line>
<xsl:if test="$iLoop < $MaxLoop">
<xsl:call-template name="CreateLoop">
<xsl:with-param name="MaxLoop" select="$MaxLoop" />
<xsl:with-param name="iLoop" select="$iLoop + 1" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
No comments:
Post a Comment