The Complete Example A Stylesheet That Emulates a for Loop

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 page 74 xsl:call-template xsl:if xsl:template xsl:template match= 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= xsl:with-param name=testValue select=0 xsl:call-template xsl:template xsl:stylesheet If you want to modify the for loop to do something useful, put your code between these comments: -- 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 --

4.8 A Stylesheet That Generates a Stylesheet That Emulates a for Loop

Weve emulated a for loop now, but what about a stylesheet that generates another stylesheet that emulates the for loop? As we beat this dead horse one more time, well create a stylesheet that generates the iteration for us, along with an XML syntax that automates the process.

4.8.1 XML Input

Heres the XML template well use to generate the stylesheet: ?xml version=1.0? html xmlns:xsl=http:www.w3.org1999XSLTransform head titleText generated by our for loop processortitle head body h1Text generated by our for loop processorh1 table border=1 tr thIteration th thValue of iiith tr for-loop index-variable=0 increment=1 operator== test-value=10 tr td align=center xsl:value-of select=iteration td td align=center xsl:value-of select=i td tr for-loop table body html page 75

4.8.2 Template Design

The design of our stylesheet-generating stylesheet is as follows: 1. Output the xsl:stylesheet element. 2. Generate the for-loop template. This will be a named template that well invoke while processing the rest of the document. 3. Generate the root element template. To do this, everything except the for-loop element is copied to the output document. The for-loop element will be converted into a call to the for-loop template we generated in the previous step. 4. Close out the xsl:stylesheet element.

4.8.3 Complications

There are a couple of complications in producing our stylesheet-generating stylesheet. First, we need to have some way to distinguish among the XSLT elements in the stylesheet being processed and the XSLT elements were generating. Heres one way to do it: xsl:element name=xsl:template namespace=http:www.w3.org1999XSLTransform xsl:attribute name=namefor-loopxsl:attribute xsl:element name=xsl:param namespace=http:www.w3.org1999XSLTransform xsl:attribute name=nameixsl:attribute xsl:attribute name=select xsl:value-of select=index-variable xsl:attribute xsl:element xsl:element name=xsl:param namespace=http:www.w3.org1999XSLTransform xsl:attribute name=nameincrementxsl:attribute xsl:attribute name=select xsl:value-of select=increment xsl:attribute xsl:element xsl:element name=xsl:param namespace=http:www.w3.org1999XSLTransform xsl:attribute name=nameoperatorxsl:attribute xsl:attribute name=select xsl:textxsl:text xsl:value-of select=operator xsl:textxsl:text xsl:attribute xsl:element xsl:element name=xsl:param namespace=http:www.w3.org1999XSLTransform xsl:attribute name=nametestValuexsl:attribute xsl:attribute name=select xsl:value-of select=test-value xsl:attribute xsl:element xsl:element name=xsl:param namespace=http:www.w3.org1999XSLTransform xsl:attribute name=nameiterationxsl:attribute xsl:attribute name=select1xsl:attribute xsl:element ... This lengthy listing generates this simple XML fragment: ns1:template name=for-loop ns1:param name=i select=0 ns1:param name=increment select=1 ns1:param name=operator select== ns1:param name=testValue select=10 ns1:param name=iteration select=1 ...