Role of xsl:output in XML Transformation Format
The xsl:output element in XSLT plays a critical role in
defining the syntax, structure, media type, and character encoding of
the document generated by an XML transformation. Positioned as a
top-level child of the xsl:stylesheet or
xsl:transform root element, it instructs the XSLT processor
on how to serialize the result tree into a specific text-based format,
such as XML, HTML, or plain text, ensuring the final output adheres to
the required technical standards.
The method Attribute
The primary mechanism for dictating the format is the
method attribute. It determines the serialization rules
applied to the transformed data:
method="xml": The default method for most processors. It outputs standard, well-formed XML, complete with standard closing tags, proper character escaping, and an XML declaration header.method="html": Adjusts output rules for HTML syntax. Empty elements are written without closing slashes (e.g.,<br>instead of<br/>), non-ASCII characters are converted to named entities where appropriate, and content within elements like<script>or<style>is left unescaped.method="text": Discards all markup generation and outputs only the string values of text nodes. This is commonly used to generate non-XML formats such as CSV, JSON, SQL scripts, or raw configuration files.
Key Serialization Attributes
Beyond the primary method, xsl:output provides several
attributes that fine-tune the final document structure:
encoding: Defines the character set used in the output document, such asUTF-8,UTF-16, orISO-8859-1. The processor encodes the output stream accordingly and updates the declaration header.omit-xml-declaration: Takes ayesornovalue. Setting this toyesremoves the<?xml version="1.0" ... ?>processing instruction, which is useful when generating XML fragments intended for embedding into other documents.indent: Acceptsyesornoto control whether the processor adds whitespace and newlines to format the output into a human-readable, indented structure.doctype-publicanddoctype-system: Generates a<!DOCTYPE ...>declaration in the output document, which is essential for targeting specific XML DTDs or HTML standards (such as HTML 4.01 or XHTML).media-type: Specifies the MIME type of the resulting file (e.g.,text/xml,text/html,application/json), which helps external systems and web servers correctly handle the generated file.cdata-section-elements: Takes a whitespace-separated list of element names whose text node children must be wrapped in<![CDATA[ ... ]]>sections rather than using standard entity escaping.
Example Configuration
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output
method="html"
encoding="UTF-8"
indent="yes"
doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"
doctype-system="http://www.w3.org/TR/html4/loose.dtd"/>
<!-- Templates follow -->
</xsl:stylesheet>In this configuration, the processor guarantees that the transformed XML source is serialized directly into indented, UTF-8-encoded HTML with the specified public and system identifiers in the DOCTYPE declaration.