Template Design Implementation A Stylesheet That Emulates a for Loop

page 72 Our for template uses four parameters: the index variable, the increment, the comparison operator, and the test value. To emulate this C++ statement: for int i=1; i=10; i++ Youd use this markup: xsl:call-template name=for-loop xsl:with-param name=i select=1 xsl:with-param name=increment select=1 xsl:with-param name=operator select=lt;= xsl:with-param name=testValue select=10 xsl:call-template To demonstrate our stylesheet, our first version simply prints out the value of our index variable each time through the loop: Transforming... Iteration 1: i=1 Iteration 2: i=2 Iteration 3: i=3 Iteration 4: i=4 Iteration 5: i=5 Iteration 6: i=6 Iteration 7: i=7 Iteration 8: i=8 Iteration 9: i=9 Iteration 10: i=10 transform took 260 milliseconds XSLProcessor: done Heres the markup youd use to emulate the Java statement for int i=10; i0; i-=2 : xsl:call-template name=for-loop xsl:with-param name=i select=10 xsl:with-param name=increment select=-2 xsl:with-param name=operator select=gt; xsl:with-param name=testValue select=0 xsl:call-template In this case, the values of i decrease from 10 to : Transforming... Iteration 1: i=10 Iteration 2: i=8 Iteration 3: i=6 Iteration 4: i=4 Iteration 5: i=2 transform took 110 milliseconds XSLProcessor: done

4.7.3 The Complete Example

Heres our complete stylesheet: ?xml version=1.0? xsl:stylesheet xmlns:xsl=http:www.w3.org1999XSLTransform version=1.0 xsl:output method=text xsl:variable name=newline xsl:text xsl:text xsl:variable xsl:template name=for-loop xsl:param name=i select=1 xsl:param name=increment select=1 xsl:param name=operator select== xsl:param name=testValue select=1 xsl:param name=iteration select=1 page 73 xsl:variable name=testPassed xsl:choose xsl:when test=starts-withoperator, = xsl:if test=i = testValue xsl:texttruexsl:text xsl:if xsl:when xsl:when test=starts-withoperator, = xsl:if test=i = testValue xsl:texttruexsl:text xsl:if xsl:when xsl:when test=starts-withoperator, = xsl:if test=i = testValue xsl:texttruexsl:text xsl:if xsl:when xsl:when test=starts-withoperator, = xsl:if test=i = testValue xsl:texttruexsl:text xsl:if xsl:when xsl:when test=starts-withoperator, xsl:if test=i testValue xsl:texttruexsl:text xsl:if xsl:when xsl:when test=starts-withoperator, xsl:if test=i testValue xsl:texttruexsl:text xsl:if xsl:when xsl:otherwise xsl:message terminate=yes xsl:textSorry, the for-loop emulator only xsl:text xsl:texthandles six operators xsl:text xsl:value-of select=newline xsl:text | | = | = | = | =. xsl:text xsl:textThe value xsl:text xsl:value-of select=operator xsl:text is not allowed.xsl:text xsl:value-of select=newline xsl:message xsl:otherwise xsl:choose xsl:variable xsl:if test=testPassed=true -- Put your logic here, whatever it might be. For the purpose -- -- of our example, well just write some text to the output stream. -- xsl:textIteration xsl:textxsl:value-of select=iteration xsl:text: i=xsl:text xsl:value-of select=ixsl:value-of select=newline -- Your logic should end here; dont change the rest of this -- -- template -- -- Now for the important part: we increment the index variable and -- -- loop. Notice that were passing the incremented value, not -- -- changing the variable itself. -- xsl:call-template name=for-loop xsl:with-param name=i select=i + increment xsl:with-param name=increment select=increment xsl:with-param name=operator select=operator xsl:with-param name=testValue select=testValue xsl:with-param name=iteration select=iteration + 1