Chapter 3 - Transforming with Style (Stylesheets, That Is) | |
XSLT For Dummies | |
by Richard Wagner | |
Hungry Minds 2002 |
As you read in Chapter 1, an XSLT stylesheet is a well- formed XML document. By convention, it has an .xsl file extension. xsl:stylesheet elementThe xsl:stylesheet element serves as the topmost element (or document element) of an XSLT stylesheet. The shell of any XSLT stylesheet consists of: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- XLST code goes here--> </xsl:stylesheet> As you can see in the preceding snippet, an xsl:stylesheet element must have two parts defined:
This information tells the XSLT processor how to process the stylesheet. Alternatively, you can also use the xsl:transform element, which is synonymous to xsl:stylesheet : <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- XLST code goes here--> </xsl:transform> Tip Although both xsl:stylesheet and xsl:transform are valid, xsl:stylesheet is by far the most commonly used of the two elements. I use xsl:stylesheet throughout this book. Top-level elementsAn xsl:stylesheet element contains all the XSLT code that appears in the stylesheet. By and large, the basic building block of the stylesheet is the template rule, defined by using the xsl:template , but you can actually add 11 additional XSLT elements directly inside the xsl:stylesheet element. These elements are called top-level elements and are shown in Table 3-1.
The following code snippet shows an XSLT stylesheet with some of these top-level elements defined: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/> <xsl:preserve-space elements="chapters"/> <xsl:template match="book"> <p><xsl:apply-templates/></p> </xsl:template> <xsl:include href="moretemplates.xsl"/> </xsl:stylesheet> Generally, you can put the top-level elements in any sequence you wish. The XSLT processor processes these elements the same way regardless of order. For example, if I move the elements around, the code generates the same results as the original: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:include href="moretemplates.xsl"/> <xsl:template match="book"> <p><xsl:apply-templates/></p> </xsl:template> <xsl:preserve-space elements="chapters"/> <xsl:output method="html"/> </xsl:stylesheet> However, there are a couple of exceptions to this rule, which tend to occur only in advanced situations. Specifically, when you use the xsl:import element, it must be the first top-level element defined under xsl:stylesheet . Also, in some error checking routines, element placement can become critical. CommentsA comment is text included in your stylesheet for behind-the-scenes use that the XSLT processor ignores during the transformation. In stylesheets, people typically use comments to label a template rule or other part of the code describing its functionality. Just like in HTML, a comment is any text surrounded by a <!-- prefix and --> suffix. For example, the heavily commented XSLT stylesheet shown here produces the same results as the preceding example: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- Developed by: R. Wagner --> <!-- Last modified: 04/22 --> <!-- This stylesheet will output an HTML document using several template rules, one defined in this file and the others from moretemplates.xsl --> <!-- Output document to HTML format --> <xsl:output method="html"/> <!-- Preserve space for chapters elements --> <xsl:preserve-space elements="chapters"/> <!--- For each book element, surround its content with HTML paragraph tags --> <xsl:template match="book"> <p><xsl:apply-templates/></p> </xsl:template> <!-- Include more template rules, which are stored in a separate file --> <include href="moretemplates.xsl"/> </xsl:stylesheet> When I say the processor ignores any comment, I mean it. You can even insult the processor with <!-- Hey processor, youre a loser! --> and this still doesnt impact its performance. Now thats service. Tip Use comments freely . As you can see by the preceding examples, comments make XSLT code much more readable than without it. This is especially true if you are trying to read a stylesheet someone else wrote.
|