How Does xsl:output Control XSLT Serialization?
The <xsl:output> element acts as the primary
configuration directive for serialization in XSLT, determining precisely
how an internal result tree is converted into a physical text, HTML, or
XML output stream. By declaring output methods, character encodings,
indentation rules, and document type definitions, this top-level element
gives developers granular control over the final syntax and structure of
generated documents.
The Role of Serialization in XSLT Processing
An XSLT processor executes transformations in two distinct conceptual phases: tree construction and serialization. During the transformation phase, templates match source XML nodes and construct an abstract result tree in memory.
Serialization is the subsequent process of turning that abstract
in-memory tree into a serialized byte or character stream that can be
saved to a file or sent over a network. The
<xsl:output> element configures the serializer,
ensuring that the raw nodes adhere to the structural conventions,
syntactic quirks, and encoding standards required by downstream
consumers.
Core Output Methods
The most fundamental attribute of <xsl:output> is
method, which dictates the serialization grammar.
method="xml": Generates standard, well-formed XML. It ensures all empty tags are self-closing (such as<br/>), outputs an XML declaration header by default, and strictly escapes special markup characters like&and<.method="html": Formats the output for legacy and modern HTML user agents. It serializes void elements without self-closing slashes (such as<br>and<img>), recognizes non-empty tags (writing<div></div>instead of<div/>), and adjusts URL attributes containing special characters.method="text": Strips away markup semantics entirely. The serializer outputs only the string values of text nodes without escaping characters, which is ideal for producing CSV files, JSON payloads, SQL scripts, or plain configuration files.method="xhtml"(XSLT 2.0+): Applies XML serialization rules while respecting XHTML guidelines, ensuring compatibility across both XML parsers and web browsers.
Key Attributes and Formatting Controls
Beyond selecting the general method, <xsl:output>
provides attributes to fine-tune document delivery and human
readability:
encoding: Specifies the character encoding (such asUTF-8,UTF-16, orISO-8859-1). The serializer encodes the output stream accordingly and updates the declaration header to match.indent: Acceptsyesornoto control whether the processor introduces whitespace and line breaks to format output for human readability.omit-xml-declaration: Suppresses the standard<?xml version="1.0" ...?>processing instruction when set toyes, which is essential when generating XML fragments intended for embedding inside other documents.- **
doctype-publicanddoctype-system**: Emits formal<!DOCTYPE ...>declarations at the beginning of the serialized document, pointing to specific Document Type Definitions (DTDs) for HTML or custom XML schemas. cdata-section-elements: Designates specific element names whose text contents must be serialized within<![CDATA[ ... ]]>blocks rather than standard entity-escaped text.
Configuring <xsl:output> correctly ensures that
transformed data conforms to external delivery standards without
requiring manual string manipulation inside template bodies.