9 Replies Latest reply on Jul 28, 2014 4:45 AM by Sen2008

    for-loop in XSLT-Important

    Sen2008

      All,

       

      version:11.1.1.4

       

      I have a requirement in the for-loop which has to iterate for a fixed loop. For instance: I want the for-loop to iterate only for 5 times, how should I define the for-loop?  Any idea, please share your thoughts.

       

      <xsl:for-each select="5">

       

      </xsl:for-each>

       

      I tried the above code, didn't work!.

       

      Rgds

      sen

        • 1. Re: for-loop in XSLT-Important
          Tarak

          If it is only for 5 times then build the logic this way

           

          1) create for each

             <xsl:for-each select="Xpath">

          2) Create a variable after for each and assign position function to it

             <xsl:variable name="Count" select = "position()">

          3) Test variable count not equal to 5

              <xsl:if test ="$Count !=5">

           

          Logic:-

           

          <xsl:for-each select="Xpath">

          <xsl:variable name="Count" select = "position()"/>

          <xsl:if test ="$Count !=5">

          <!-- Mapping logic--->

          </xsl:if>

          </xsl:for-each>

           

           

          Regards,

          TJ

          1 person found this helpful
          • 2. Re: for-loop in XSLT-Important
            Sen2008

            Hi TJ,

             

            Quite interesting I'll try this logic and update the forum.

             

            Thanks a lot

            sen

            • 3. Re: for-loop in XSLT-Important
              Sen2008

              TJ,

               

              xpath in the for-loop ( <xsl:for-each select="Xpath">) should be a valid one? i.e I give the valid xpath, but the xpath may or may not be the collection

              • 4. Re: for-loop in XSLT-Important
                Tarak

                That should not be a problem even though your xpath iterate for one time then the loop rotates only one time and  Count set to value "1" so still condition holds good.

                 

                Regards,

                TJ

                • 5. Re: for-loop in XSLT-Important
                  Sen2008

                  TJ,

                   

                  My requirement is, the xpath will always be only once, but irrespective of that, I need the for-loop to iterate for 5 times.  Hence, I have to specify the fixed size.

                   

                  rgds

                  sen

                  • 6. Re: for-loop in XSLT-Important
                    Sen2008

                    Hi TJ,

                     

                    Is it doable (i.e without specifying the xpath, can i give the fixed length)?  Pls confirm.

                     

                    rgds

                    sen

                    • 7. Re: for-loop in XSLT-Important
                      Tarak

                      Can you post the requirement here..

                      For each iterate based on xpath, If xpath returns one node then loop rotates one time only.... So in this case what ever logic i provide it not going to work.

                      • 8. Re: for-loop in XSLT-Important
                        abhay kumar

                        You can use templates in xslt like one below :-  Assign max - 5 and this will loop for 5 times.

                        <xsl:template name="loop">
                          <!--recursive loop until done-->
                          <xsl:param name="i"/>
                          <xsl:param name="max"/>
                          <xsl:if test="$i &lt;= $max">
                            <!-- Repeated content Here -->
                            <!-- use value-of i to get loop index -->
                            <xsl:value-of select="$i"/>.<br/>
                              <xsl:call-template name="loop">
                                <xsl:with-param name="i" select="$i + 1"/>
                                <xsl:with-param name="max" select="$max"/>
                              </xsl:call-template>
                          </xsl:if>
                        </xsl:template>

                        <xsl:call-template name="loop">
                          <xsl:with-param name="i" select="number(1)"/>
                          <xsl:with-param name="max" select="number(5)"/>
                        </xsl:call-template>

                         

                        Or you can also do while loop like one below:-

                        <!-- recursive named template -->
                        <xsl:template name="while">

                        <xsl:variable name="VALUE">
                        <!-- your evaluation -->
                        </xsl:variable>

                        <!-- evaluate and recurse -->
                        <xsl:if test="$VALUE=0">
                          
                        <xsl:call-template name="while"/>
                        </xsl:if>

                        </xsl:template>

                        • 9. Re: for-loop in XSLT-Important
                          Sen2008

                          I'll try this out and update the forum. 

                           

                          Thanks everyone.

                          sen