XML Input A Stylesheet That Generates a Stylesheet That Emulates a for Loop

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 ... page 76 This approach works, but were doing an awful lot of work to create some fairly simple elements. For all the XSLT elements were generating with xsl:element elements, we have to declare the namespace for each one. The obvious way of handling this would be to generate a namespace declaration on the xsl:stylesheet element: xsl:attribute name=xmlns:xsl http:www.w3.org1999XSLTransform xsl:attribute Unfortunately, the XSLT specification states in section 7.1.3 that this isnt legal. What we did in our previous example was add the namespace attribute to all XSLT elements we need to generate. The XSLT processor is not required to use the namespace prefix we specified in the xsl:element , by the way. To help us get around this awkward problem, the XSLT specification provides the xsl:namespace-alias element. This provision allows us to define an alias for the XSLT namespace or any other namespace we want to use; well use the normal XSLT namespace for the stylesheet elements we use, and well use the alias for the stylesheet elements we generating. Heres how our new stylesheet looks: ?xml version=1.0? xsl:stylesheet version=1.0 xmlns:xsl=http:www.w3.org1999XSLTransform xmlns:xslout=can be anything, doesnt matter xsl:output method=xml indent=yes xsl:namespace-alias stylesheet-prefix=xslout result-prefix=xsl xsl:template match=||text|comment|processing-instruction xsl:copy xsl:apply-templates select=||text|comment|processing-instruction xsl:copy xsl:template xsl:template match=for-loop xslout:call-template name=for-loop xslout:with-param name=i select={index-variable} xslout:with-param name=increment select={increment} xslout:with-param name=operator xsl:attribute name=select xsl:textxsl:text xsl:value-of select=operator xsl:textxsl:text xsl:attribute xslout:with-param xslout:with-param name=testValue select={test-value} xslout:call-template xsl:template xsl:template match=for-loop mode=generate-template xslout:variable name=newline xslout:text xslout:text xslout:variable xslout:template name=for-loop xslout:param name=i select=index-variable xslout:param name=increment select=increment xslout:param name=operator select=operator xslout:param name=testValue select=test-value xslout:param name=iteration select=1 xslout:variable name=testPassed xslout:choose xslout:when test=starts-withoperator, = xslout:if test=i = testValue xslout:texttruexslout:text