Sponsored

XSLT Reference

xsl:stylesheet

XSLT 1.0 element

Root element of every XSLT stylesheet, declaring the version, namespace, and top-level declarations for the transformation.

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

Description

xsl:stylesheet is the mandatory root element of an XSLT stylesheet document. It binds the XSLT namespace, declares the language version, and acts as the container for all top-level declarations such as templates, variables, parameters, keys, and output settings.

The version attribute is required and tells the processor which version of the XSLT specification governs this stylesheet. Setting it to "1.0" requests strict XSLT 1.0 processing; a conforming XSLT 2.0 or 3.0 processor will then run in backwards-compatibility mode.

The namespace declaration xmlns:xsl="http://www.w3.org/1999/XSL/Transform" must appear on this element (or be inherited by it) because every XSLT instruction element must be in that namespace. You may also declare additional namespaces here — for extension functions, output namespace prefixes, or the document’s own vocabulary.

xsl:stylesheet and xsl:transform are fully interchangeable synonyms; by convention most stylesheets use xsl:stylesheet.

Attributes

AttributeTypeRequiredDescription
version"1.0" / "2.0" / "3.0"YesXSLT version governing this stylesheet.
idIDNoUnique identifier for the element.
extension-element-prefixeswhitespace-separated prefixesNoNamespace prefixes to treat as extension element prefixes rather than literal result elements.
exclude-result-prefixeswhitespace-separated prefixesNoNamespace prefixes that should not be copied into the result tree.
xpath-default-namespaceURINo(2.0+) Default namespace for unprefixed XPath steps.
default-collationURINo(2.0+) Default collation for string comparisons.
input-type-annotationsstrip / preserve / unspecifiedNo(2.0+) Whether schema type annotations on input nodes are retained.

Examples

Minimal XSLT 1.0 stylesheet

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<greeting>Hello, World!</greeting>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>

  <xsl:template match="/greeting">
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>

Output:

Hello, World!

Stylesheet with excluded namespace prefixes

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<report><title>Q1</title><value>42</value></report>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:my="http://example.com/my"
  exclude-result-prefixes="my">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/report">
    <result>
      <xsl:value-of select="title"/>
      <xsl:text>: </xsl:text>
      <xsl:value-of select="value"/>
    </result>
  </xsl:template>
</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="UTF-8"?>
<result>Q1: 42</result>

Notes

  • Every XSLT document must have exactly one xsl:stylesheet (or xsl:transform) element as its document element.
  • Top-level elements must appear in a defined order when import precedence matters: xsl:import elements must come first, before any other children.
  • Extension element prefixes listed in extension-element-prefixes are not written to the output tree as namespace nodes; this keeps the output clean.
  • When a stylesheet is embedded inside an XML source document (using the <?xml-stylesheet?> processing instruction), the stylesheet element still requires the version and namespace attributes.

See also