Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 10th, 2007, 02:27 PM
Registered User
 
Join Date: Oct 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to im_himanshu2004
Default FOR LOOP IN XSLT

Hi All,
Here I'm giving most generic technique to implement for loop in XSLT.

  <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 &lt;= $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>


    <xsl:template name="XSLTLOOPPROCESSOR">
    <xsl:param name="Label"/>

    <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'">

        Write your code here...

      </xsl:when>
    </xsl:choose>
  </xsl:template>


How to call or use - General Syntax:
    <xsl:call-template name="XSLTLOOP">

      <xsl:with-param name="Label" select="'Label1'"/>

      <xsl:with-param name="from" select="1"/>

      <xsl:with-param name="to" select="11"/>

      <xsl:with-param name="step" select="1"/>

      <xsl:with-param name="Param1" select="125"/>

      <xsl:with-param name="Param2" select="'ABCD'"/>

      <xsl:with-param name="Param3" />

      <xsl:with-param name="Param4" />

      <xsl:with-param name="Param5" />

      <xsl:with-param name="Param6"/>

      <xsl:with-param name="Param7" />

      <xsl:with-param name="Param8" />

      <xsl:with-param name="Param9" />

      <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'">

        Write your code here...

      </xsl:when>
      <xsl:when test="$Label = 'MyNewLabel'">

        Write your code here...

      </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 :)

Himanshu Singh





Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested For-Each Loop In XSLT dghosh XSLT 10 July 2nd, 2008 04:34 AM
XSLT Loop? kwilliams XSLT 1 June 16th, 2008 06:04 PM
xslt when loop problem check XSLT 1 April 18th, 2008 08:47 AM
javascript 'for' loop in xslt mummra1 XSLT 22 February 6th, 2007 04:26 PM
loop in xslt vazarshahy XSLT 6 June 7th, 2006 04:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.