Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 10th, 2007, 02:27 PM
Registered User
 
Join Date: Oct 2007
Location: Pune, Maharashtra, India.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 08:04 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.