XSLT Stylesheet Structure

The general order for elements in an XSL stylesheet is as follows:

<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:import/>
   <xsl:include/>
   <xsl:strip-space/>
   <xsl:preserve-space/>
   <xsl:output/>
   <xsl:key/>
   <xsl:decimal-format/>
   <xsl:namespace-alias/>
   <xsl:attribute-set>...</xsl:attribute-set>
   <xsl:variable>...</xsl:variable>
   <xsl:param>...</xsl:param>

   <xsl:template match="...">
      ...
   </xsl:template>

   <xsl:template name="...">
      ...
   </xsl:template>

</xsl:stylesheet>

Essentially, this ordering boils down to a few simple rules. First, all XSL stylesheets must be well-formed XML documents, and each XSL element must use the namespace specified by the xmlns declaration in the stylesheet element (commonly xsl:). Second, all XSL stylesheets must begin with the XSL root element tag, xsl:stylesheet, and close with the corresponding tag, /xsl:stylesheet. Within the opening tag, the XSL namespace must be defined:

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

After the root element, you can import external stylesheets with xsl:import elements, which must always be first within the xsl:stylesheet element. Any other elements can then be used in any order and in multiple occurrences if needed.

Get XML Pocket Reference, Second Edition 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.