Constructing Your XSLT Stylesheet

xslt for dummies
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 element

The 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="" 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:

  • Namespace: In the preceding code, the XSLT namespace is defined as xmlns:xsl="" (Dont worry about what a namespace is just yet; youll find out about namespaces later in this chapter.)

  • Version: The version attribute defined providing the version of XSLT used, which is currently 1.0.

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="" 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 elements

An 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.

Table 3-1: Top-level XSLT Elements




Defines a template rule.


Specifies the output format for the result document.


Defines a variable.


Defines a parameter, which is a special kind of variable.


Loads an external stylesheet.


Loads an external stylesheet as part of the current stylesheet.

xsl: preserve-space

Preserves whitespace in the result document.

xsl: strip-space

Removes whitespace in the result document.


Defines a key that can be used to link together XML elements.


Defines the decimal format to use when converting numbers to strings.

xsl: namespace-alias

Maps a namespace to another namespace.


Defines a named set of attributes for use in the result document.

The following code snippet shows an XSLT stylesheet with some of these top-level elements defined:

 <xsl:stylesheet xmlns:xsl="" 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="" 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.


A 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="" 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.

2000-2002    Feedback

XSLT For Dummies
XSLT for Dummies
ISBN: 0764536516
EAN: 2147483647
Year: 2002
Pages: 148

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: