Stylesheet Structure

As the final part of our introduction to XSLT, we’ll look at the contents of the stylesheet itself. We’ll explain all the things in our stylesheet and discuss other approaches we could have taken.

The <xsl:stylesheet> Element

The <xsl:stylesheet> element is typically the root element of an XSLT stylesheet.

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0">

First of all, the <xsl:stylesheet> element defines the version of XSLT we’re using, along with a definition of the xsl namespace. To be compliant with the XSLT specification, your stylesheet should always begin with this element, coded exactly as shown here. Some stylesheet processors, notably Xalan, issue a warning message if your <xsl:stylesheet> element doesn’t have these two attributes with these two values. For all examples in this book, we’ll start the stylesheet with this exact element, defining other namespaces as needed.

The <xsl:output> Element

Next, we specify the output method. The XSLT specification defines three output methods: xml, html, and text. We’re creating an HTML document, so HTML is the output method we want to use. In addition to these three methods, an XSLT processor is free to define its own output methods, so check your XSLT processor’s documentation to see if you have any other options.

<xsl:output method="html"/>

A variety of attributes are used with the different output methods. For example, if you’re using method="xml", you can use doctype-public ...

Get XSLT now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.