Sponsored

XSLT Reference

xsl:output

XSLT 1.0 element

Controls how the result tree is serialised — output method, encoding, indentation, DOCTYPE, and other serialisation options.

Syntax
<xsl:output method="xml|html|text" indent="yes|no" encoding="UTF-8"/>

Description

xsl:output is a top-level declaration that controls the serialisation of the result tree — how the abstract tree of nodes is converted to a stream of characters or bytes. It does not affect what nodes are in the result tree; it only affects how those nodes are written to the output.

The three standard output methods are:

  • xml — Serialises as an XML document. Special characters are escaped, an XML declaration may be added, and the result is always well-formed XML.
  • html — Serialises following HTML conventions: void elements like <br> are written without a closing tag, boolean attributes are written without values, and the DOCTYPE is added if specified.
  • text — Serialises only the string values of text nodes; all markup is omitted.

Multiple xsl:output declarations in the same stylesheet are allowed and their attributes are merged. When the same attribute appears in multiple declarations, the one with the highest import precedence wins; within the same precedence level, the last declaration wins.

Attributes

AttributeTypeRequiredDescription
methodxml / html / text / QNameNoSerialisation method. Default is xml unless the root element is html.
versionNMTOKENNoVersion of the output format (e.g., "4.0" for HTML).
encodingencoding nameNoCharacter encoding (e.g., "UTF-8", "ISO-8859-1").
omit-xml-declarationyes / noNoWhether to suppress the <?xml ...?> declaration.
standaloneyes / noNoValue of the standalone attribute in the XML declaration.
doctype-publicstringNoPublic identifier for the DOCTYPE declaration.
doctype-systemstringNoSystem identifier for the DOCTYPE declaration.
cdata-section-elementswhitespace-separated QNamesNoElements whose text content should be serialised as CDATA sections.
indentyes / noNoWhether to add whitespace indentation.
media-typeMIME typeNoMedia type of the output.

Examples

XML output with indentation

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<data><item>A</item><item>B</item></data>

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="xml" indent="yes" encoding="UTF-8"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="UTF-8"?>
<data>
  <item>A</item>
  <item>B</item>
</data>

HTML output with DOCTYPE

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<page><title>Hello</title><body>World</body></page>

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="html" indent="yes" encoding="UTF-8"
    doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"
    doctype-system="http://www.w3.org/TR/html4/loose.dtd"/>

  <xsl:template match="/page">
    <html>
      <head><title><xsl:value-of select="title"/></title></head>
      <body><p><xsl:value-of select="body"/></p></body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Output:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>Hello</title></head>
<body><p>World</p></body>
</html>

Notes

  • xsl:output is advisory: a processor is not required to serialise the result at all (it may hand the result tree to a further processing step). When serialisation does occur, the processor should honour the settings.
  • indent="yes" may add whitespace-only text nodes to the output. This can cause problems if the consuming application is whitespace-sensitive; use it only for human-readable output.
  • cdata-section-elements is useful for elements containing JavaScript or CSS in HTML, where <, >, and & should not be escaped.
  • In XSLT 2.0+, xsl:output gains additional attributes including use-character-maps, normalization-form, undeclare-prefixes, and html-version.

See also