Page 1 of 1

FOR LOOP IN XSLT

Posted: Wed Oct 10, 2007 10:34 pm
by im_himanshu2004
Hi All,
Here I'm giving most generic technique to implement for loop in XSLT.
<!-- START: XSLTLOOP Template. Please do not touch this template. Just copy as it is.-->
<xsl:template name="XSLTLOOP">
<xsl:param name="Label"/>
<xsl:param name="EnterCount" select="1"/>
<xsl:param name="from" select="1"/>
<xsl:param name="to" select="1"/>
<xsl:param name="step" select="1"/>
<xsl:param name="Param1" select="0"/>
<xsl:param name="Param2" select="0"/>
<xsl:param name="Param3" select="0"/>
<xsl:param name="Param4" select="0"/>
<xsl:param name="Param5" select="0"/>
<xsl:param name="Param6" select="0"/>
<xsl:param name="Param7" select="0"/>
<xsl:param name="Param8" select="0"/>
<xsl:param name="Param9" select="0"/>
<xsl:param name="Param10" select="0"/>
<xsl:if test="$from <= $to">
<xsl:call-template name="XSLTLOOPPROCESSOR">
<xsl:with-param name="Label" select="$Label"/>
<xsl:with-param name="EnterCount" select="$EnterCount"/>
<xsl:with-param name="Param1" select="$Param1"/>
<xsl:with-param name="Param2" select="$Param2"/>
<xsl:with-param name="Param3" select="$Param3"/>
<xsl:with-param name="Param4" select="$Param4"/>
<xsl:with-param name="Param5" select="$Param5"/>
<xsl:with-param name="Param6" select="$Param6"/>
<xsl:with-param name="Param7" select="$Param7"/>
<xsl:with-param name="Param8" select="$Param8"/>
<xsl:with-param name="Param9" select="$Param9"/>
<xsl:with-param name="Param10" select="$Param10"/>
</xsl:call-template>
<xsl:call-template name="XSLTLOOP">
<xsl:with-param name="Label" select="$Label"/>
<xsl:with-param name="EnterCount" select="$EnterCount + 1"/>
<xsl:with-param name="from" select="$from + $step"/>
<xsl:with-param name="to" select="$to"/>
<xsl:with-param name="step" select="$step"/>
<xsl:with-param name="Param1" select="$Param1"/>
<xsl:with-param name="Param2" select="$Param1"/>
<xsl:with-param name="Param3" select="$Param1"/>
<xsl:with-param name="Param4" select="$Param1"/>
<xsl:with-param name="Param5" select="$Param1"/>
<xsl:with-param name="Param6" select="$Param1"/>
<xsl:with-param name="Param7" select="$Param1"/>
<xsl:with-param name="Param8" select="$Param1"/>
<xsl:with-param name="Param9" select="$Param1"/>
<xsl:with-param name="Param10" select="$Param1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<!-- END: XSLTLOOP Template. Please do not touch this template.-->
<!-- START: XSLTLOOPPROCESSOR Template. Please do not touch this template. Just copy as it is.-->
<xsl:template name="XSLTLOOPPROCESSOR">
<xsl:param name="Label"/>
<!--EnterCount : Special variable that tells entry count in this template. -->
<xsl:param name="EnterCount"/>
<xsl:param name="Param1" select="0"/>
<xsl:param name="Param2" select="0"/>
<xsl:param name="Param3" select="0"/>
<xsl:param name="Param4" select="0"/>
<xsl:param name="Param5" select="0"/>
<xsl:param name="Param6" select="0"/>
<xsl:param name="Param7" select="0"/>
<xsl:param name="Param8" select="0"/>
<xsl:param name="Param9" select="0"/>
<xsl:param name="Param10" select="0"/>
<xsl:choose>
<xsl:when test="$Label = 'Label1'">
<!--START: Label1 block-->
Write your code here...
<!--END : Label1 block-->
</xsl:when>
</xsl:choose>
</xsl:template>
<!-- END: XSLTLOOPPROCESSOR Template. Please do not touch this template. Just copy as it is.-->

How to call or use - General Syntax:
<xsl:call-template name="XSLTLOOP">
<!--REQUIRED. Label1 can be any valid string that will signify your data block in XSLTLOOPPROCESSOR template.-->
<xsl:with-param name="Label" select="'Label1'"/>
<!--OPTIONAL. Default = 1-->
<xsl:with-param name="from" select="1"/>
<!--OPTIONAL. Default = 1-->
<xsl:with-param name="to" select="11"/>
<!--OPTIONAL. Default = 1-->
<xsl:with-param name="step" select="1"/>
<!--OPTIONAL. Default = 0(Integer). Shown here how to pass integer as argument.-->
<xsl:with-param name="Param1" select="125"/>
<!--OPTIONAL. Default = Same as Param1. Shown here how to pass StringValue as argument.-->
<xsl:with-param name="Param2" select="'ABCD'"/>
<!--OPTIONAL-->
<xsl:with-param name="Param3" />
<!--OPTIONAL-->
<xsl:with-param name="Param4" />
<!--OPTIONAL-->
<xsl:with-param name="Param5" />
<!--OPTIONAL-->
<xsl:with-param name="Param6"/>
<!--OPTIONAL-->
<xsl:with-param name="Param7" />
<!--OPTIONAL-->
<xsl:with-param name="Param8" />
<!--OPTIONAL-->
<xsl:with-param name="Param9" />
<!--OPTIONAL-->
<xsl:with-param name="Param10" />
</xsl:call-template>

Example:
1. COPY FIRST TWO TEMPLATES AS IT IS.
2.
for (int i = 1; i <= 10; i++)
{
//Write your code here.
}
Two implement above loop...
<xsl:call-template name="XSLTLOOP">
<xsl:with-param name="Label" select="'MyNewLabel'"/>
<xsl:with-param name="to" select="10"/>
</xsl:call-template>
3.Go to XSLTLOOPPROCESSOR template. Add you data block.

<xsl:template name="XSLTLOOPPROCESSOR">
...
...
<xsl:when test="$Label = 'Label1'"> <!-- ALREADY PRESENT -->
<!--START: Label1 block-->
Write your code here...
<!--END : Label1 block-->
</xsl:when>
<xsl:when test="$Label = 'MyNewLabel'"> <!-- YOUR NEW BLOCK -->
<!--START: MyNewLabel block-->
Write your code here...
<!--END : MyNewLabel block-->
</xsl:when>
</xsl:choose>
</xsl:template>

This appraoch also helps in keeping all loop related code under single template thus increases manageability of code.

Thanks,

Himanshu Singh, Pune, India.

Happy Programming :D