O'Reilly logo

XSLT Cookbook by Sal Mangano

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Generating XSLT from XSLT

Problem

You want to generate XSLT from a different XML representation. Alternatively, you want to transform XSLT or pseudo-XSLT into real XSLT.

Solution

Two things about the control structure of XSLT sometimes annoy me. The first is the absence of an if-then-elsif-else construct; the second is the absence of a true looping construct. Of course, I am aware of xsl:choose and xsl:for-each, but each is lacking to some extent. I find xsl:choose annoying because the choose element serves practically no function, except to force an extra level of nesting. The xsl:for-each is not really a looping construct but an iteration construct. To emulate loops with counters, you have to use recursion or the Piez method (see Recipe 1.5), which is awkward.

This example illustrates an XSLT-to-XSLT generation by pretending that XSLT has the elements xslx:elsif, xslx:else, and xslx:loop. Since it really does not, you will create a stylesheet that generates true XSLT from the following pseudo-XSLT. Having an xsl:if and an xslx:if is awkward, but it would be wrong to use the standard XSLT namespace for your extended elements; these elements might be defined in standard XSLT some day:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xslx="http://www.ora.com/XSLTCookbook/ExtendedXSLT" > <xsl:output method="text"/> <xsl:template match="foo"> <xslx:if test="bar"> <xsl:text>You often will find a bar in the neighborhood of foo!</xsl:text> </xslx:if> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required