XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / output

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z

 

Declaration xsl:output controls stylesheet output format for result serialization. Its attributes correspond to serialization parameters; the main one, method, defines the result document type.

Classification

Declaration

Function group

Output format definition

Introduced in

XSLT 1.0

Stylesheet position and content:

xsl:output is a top-level direct child of xsl:stylesheet or xsl:transform. A stylesheet may contain any number of xsl:output declarations. The element is always empty.

Attributes:

For xsl:output, standard attributes apply except version, here an element-specific attribute with another role. (Note: Backwards compatibility explains this: XSLT 1.0 had attribute version before the same-named standard attribute appeared in 2.0. The corresponding attribute of xsl:result-document is named output-version to avoid a similar conflict.)

It additionally has nine attributes in 1.0, sixteen from 2.0, to define output precisely. New in 2.0, attribute name names the declaration for association with an xsl:result-document instruction.

Attribute value templates are forbidden. Attributes correspond to serialization parameters from “XSLT 2.0 and XQuery 1.0 Serialization”.

byte-order-mark

Value

"yes" | "no"

Usage

Optional

Introduced in

XSLT 2.0

Attribute byte-order-mark specifies whether output starts with a byte-order mark (BOM). There is no default: without the attribute, encoding determines whether a mark appears. Attribute byte-order-mark explicitly forces or suppresses it.

The BOM is the Unicode character U+FEFF (zero width no-break space) at the resource start. Encoding turns it into bytes identifying the encoding to readers (see table).

The mark tells readers the encoding, though they can often infer it from initial characters. Especially relevant are UTF-16 (two bytes per character) and UTF-32 (four), distinguishing big-endian and little-endian. (Note: Intel CPUs internally use little-endian, Motorola big-endian. Language Java also favors big-endian.)

This is byte order: most significant first (big-endian) or least significant first (little-endian). UTF-32 fills two of four bytes with zeros. It is virtually ISO 10646 UCS-4, so the BOM is identical.

Encoding

BOM bytes

UTF-8

EF BB BF

UTF-16, big-endian

FE FF

UTF-16, little-endian

FF FE

UTF-32/UCS-4, big-endian

00 00 FE FF

UTF-32/UCS-4, little-endian

FF FE 00 00

Table: BOM bytes by encoding (source: Wikipedia).

For UTF-16 the default is "yes"; for UTF-8 it depends on implementation, since UTF-8 needs no BOM. All other encodings default to "no". Non-Unicode formats have no BOM.

Note: This attribute does not exist in XSLT 1.0.

cdata-section-elements

Value

qnames

Usage

Optional

Introduced in

XSLT 1.0

Attribute cdata-section-elements is permitted with method="xml". Its whitespace-separated QNames identify elements whose text nodes should appear in CDATA sections in XML output:

<![CDATA[ ... Textnode ... ]]>

This avoids escaping markup or special/control characters in content sections, or protects content that could harm the result’s well-formedness, such as JavaScript. For XHTML documents, this applies to containers <script> and <style>.

doctype-public

Value

string

Usage

Optional

Introduced in

XSLT 1.0

Attribute doctype-public, together with attribute doctype-system, specifies the additional optional PUBLIC identifier of a doctype declaration. It applies to methods allowing doctypes: "xml", "html" and, from XSLT 2.0, "xhtml". Otherwise it is ignored.

Note: Without attribute doctype-system, attribute doctype-public is ignored.

doctype-system

Value

string

Usage

Optional

Introduced in

XSLT 1.0

Attribute doctype-system assigns a potential SYSTEM identifier as a URI for the result’s doctype. It applies to methods allowing doctypes: "xml", "html" and, from XSLT 2.0, "xhtml". Otherwise it is ignored.

Note: With attribute doctype-system, attribute doctype-public may also be used.

encoding

Value

string

Usage

Optional

Introduced in

XSLT 1.0

Attribute encoding defines the result’s binary character encoding, for example UTF-8 or ISO-8859-1. Values identify encoding formats (charsets) registered with IANA under RFC 2278. Names are case-insensitive: UTF-8 and utf-8 are equivalent.

If the XSLT processor does not recognize or support the encoding, UTF-8 is used. Attribute encoding works with every value of method.

escape-uri-attributes

Value

"yes" (default) | "no"

Usage

Optional

Introduced in

XSLT 2.0

Attribute escape-uri-attributes controls escaping of attribute values of type xs:anyURI in HTML/XHTML results. Escaping is usually appropriate, so the default is "yes".

Escaping follows RFC 2396 section 2.4.1: unavailable characters become hexadecimal pairs prefixed with %, for example a space becomes %20. Escape spaces, control characters and URI delimiters used literally, including % itself: <, >, #, %, ".

Note: This attribute does not exist in XSLT 1.0.

include-content-type

Value

"yes" (default) | "no"

Usage

Optional

Introduced in

XSLT 2.0

Attribute include-content-type controls whether the processor adds a meta tag with a content-type description such as:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

For HTML/XHTML, the default is "yes".

Note: This attribute does not exist in XSLT 1.0.

indent

Value

"yes" | "no"

Usage

Optional

Introduced in

XSLT 1.0

Attribute indent requests, without binding the processor, source indentation in results. The default is "yes" for html/xhtml, but xml defaults to "no", because whitespace is initially considered significant. For method “text”, attribute indent is inapplicable.

Indentation may add whitespace only between elements without changing content; existing whitespace may be replaced.

Never indent within or next to text nodes: adding whitespace changes content, even its character count.

Nor add whitespace where attribute xml:space has value "preserve". The indent attribute does not normalize or strip existing whitespace before indenting.

Note — More XML 1.1 whitespace characters
XML 1.1 adds IBM-mainframe NEL (Newline, U+0085 or #x85) and Unicode line separator #x2028. Previously forbidden, these make XML 1.1 documents not well-formed XML 1.0 documents.

media-type

Value

string

Usage

Optional

Introduced in

XSLT 1.0

Attribute media-type adds a MIME type, potentially needed for HTTP transmission. It works with all values of method, affecting storage or transmission properties according to runtime, not document content.

All output formats have defaults when attribute media-type is absent: XML uses "text/xml", HTML/XHTML use "text/html", plain text uses "text/plain".

method

Value

"xml" | "html" | "text" | qname-but-not-ncname
From XSLT 2.0 also: "xhtml"

Usage

Optional

Introduced in

XSLT 1.0 (extended in 2.0)

Attribute method defines the result document type. Predefined values are "xml", "html" and "text". XSLT 2.0 adds "xhtml" independently. XSLT 1.0 could already approximate it through method="xml" using root element <html>.

Without attribute method, output defaults to "xml" unless the result root (final result tree) is named <html>, selecting "html". If that root is also in the XHTML namespace, output becomes "xhtml" in XSLT 2.0. In 1.0, the processor instead uses "xml".

Note: Additional formats named by a prefixed QName may be vendor-defined, using option qname-but-not-ncname. A value neither predefined nor a QName with a prefix causes static error (ERR XTSE1570).

method="xml"
This explicitly requests XML, already the default unless the root is named <html>. Then the namespace decides whether, without a method attribute, output is "xhtml" or "html".

Method "xml" is required for XML or external general parsed entities requiring an XML prolog or text declaration.

The default media type for "xml" is "text/xml". Attributes doctype-system and doctype-public add a doctype, but not an internal subset of a DTD.

method="xhtml" (new in XSLT 2.0)
XSLT 1.0 required method="xml" for XHTML, whose root <html> alone cannot distinguish it from "html". Unlike html, the root’s local name "html" must be lowercase.

XSLT 2.0 provides independent type "xhtml", considering the root namespace. With "http://www.w3.org/1999/xhtml", the XHTML namespace, the default is "xhtml" even without an explicit method.

For strict, transitional or frameset XHTML using a different namespace, explicitly set method="xhtml".

As for "html", the media type defaults to "text/html". Attribute media-type alternatively accepts "text/xml", "application/xhtml+xml" or "application/xml". A meta content-description tag is inserted unless attribute include-content-type has value "no":

<meta http-equiv="Content-Type" content="text/html; charset= EUC-JP">

The charset attribute takes the value of attribute encoding, also used in the XHTML document’s XML declaration.

Empty HTML tags are XML-compliant, unlike method "html". The processor should currently add a space before the closing slash for compatibility.

Example: <br /> instead of <br/>.

Elements whose content model is not EMPTY must not use abbreviated syntax even when empty.

Example: An empty paragraph always as <p></p>, never as <p />.

Unlike pure XML output, line breaks in generated/copied attribute values should normalize to a space.

Whitespace and special characters in URI attributes such as href or src are automatically escaped under RFC 3236. Attribute escape-uri-attributes disables this, useful only for a different escaping scheme through XPath 2.0 function fn:escape-html-uri().

Conforming to XHTML Strict, Transitional, Frameset or Basic is the author’s task. The processor need not report violations. Attributes doctype-public and doctype-system can add doctypes for later validation.

method="html"
Format "html" is default for a root in no namespace named <html>, in any case combination. All unnamespaced elements are output as HTML 4.0.

Empty HTML elements have no closing tag, usually producing non-well-formed XML. Recognition ignores input case, for example <BR/>, <br></br>, <br/> all become <br>.

HTML 4.0 Boolean attributes are minimized where allowed.

Example: <hr noshade="noshade"/> yields <hr noshade>.

Control or unencodable characters use HTML entities. Without an appropriate entity, numeric character references apply. Do not escape within <script> or <style> elements.

method="text"
Format "text" outputs the source root’s string value: all element text in document order, without escaping. All other node types, including attributes, are ignored. End-of-line characters depend on the system environment.

In conjunction with method="text", only the additional attributes media-type, encoding and unicode-normalisation are recognized. Other attributes are ignored.

The media-type attribute defaults to "text/plain"; for the encoding attribute, the default is system-dependent in this context. If the output text contains characters absent from the selected or default encoding, an error is always reported.

No external general parsed entities can be generated with method="text"
External general parsed entities consisting of plain text preceded by a text declaration such as <?xml version="1.0"?> cannot be generated using output method "text", but require method "xml". There is no way to output a text declaration with output format "text".

name

Value

QName

Usage

Optional

Introduced in

XSLT 2.0

The name attribute names an xsl:output declaration. When multiple result documents are generated using xsl:result-document, a named output format can be assigned to each document. Multiple xsl:output declarations with the same name in a stylesheet are merged into a single declaration.

The xsl:result-document instruction references an output declaration through its format attribute. If an xsl:result-document instruction has no format attribute, it references the stylesheet’s unnamed output declaration.

Note: This attribute does not exist in XSLT 1.0.

normalisation-form

Value

"NFC" | "NFD" | "NFKC" | "NFKD" |
"fully-normalized" | "none" | nmtoken

Usage

Optional

Introduced in

XSLT 2.0

Attribute normalisation-form specifies the normalization form desired for the result document. (Note: This attribute replaces the less flexible attribute normalize-unicode proposed in earlier working drafts of the specification.) Supply a name token from the list above, or an NMTOKEN that activates a normalization form implemented by the processor. The default is "none". The attribute can be used with all output formats.

Four Unicode normalization forms and the W3C-specific normalization FULLY-NORMALIZED can be selected. Processor-specific forms are also available, but behavior when the requested form is unsupported is unspecified (XPath function fn:normalize-unicode() reports an error in a comparable situation).

Normalization forms:
The standard requires applications to support only the most widespread form, NFC (Unicode Normalisation Form C, canonical composition), also favored by the W3C:

  • NFC – Unicode Normalisation Form C, canonical composition

Besides NFC, other rules exist whose names applications may, but need not, recognize:

  • NFD – Unicode Normalisation Form D, canonical decomposition

  • NFKC – Unicode Normalisation Form KC, compatible canonical composition

  • NFKD – Unicode Normalisation Form KD, compatible canonical decomposition

Another form, potentially supported by applications, is outside the Unicode standard:

  • FULLY-NORMALIZED – the W3C normalization rule, described in “Character Model for the World Wide Web 1.0”.
    This form essentially corresponds to NFC, but permits only so-called Base Characters (Unicode Combining Class 0). If a combining character, such as an accent, occurs at the start of a string, normalization inserts a space before it as an additional base character to carry it.

Any string containing composite Unicode characters can usefully be normalized. These include ligatures, accented characters and umlauts. For example, lowercase ä can be represented by the single character U+00E4 LATIN SMALL LETTER A WITH DIAERESIS or by combining characters U+0061 LATIN SMALL LETTER A and U+0308 COMBINING DIAERESIS. Such variants can make direct comparison of two strings, even essentially equivalent ones, virtually unpredictable.

Normalization first decomposes composite characters, then replaces them with a canonical composite representation following a uniform rule. Special characters occurring as so-called singleton characters, consisting of just one Unicode symbol (such as Å for “Ångstrom”), are replaced with their corresponding canonical composite form.

Plain ASCII text, and thus any program source code, remains unchanged by Unicode normalization because ASCII has no composite characters. Normalization is therefore harmless here, but unnecessary.

Characters affected by Unicode normalization:
Comparison tables of all characters affected by Unicode normalization in all character sets.

Note: This attribute does not exist in XSLT 1.0.

omit-xml-declaration

Value

"yes" | "no"

Usage

Optional

Introduced in

XSLT 1.0

The omit-xml-declaration attribute specifies whether to omit the XML declaration normally added to the result document by the XSLT processor. It is permitted only for method="xml" and ignored for other output formats ("text" or "html"). The default is "no". For "yes", the XML declaration is always omitted.

The XML declaration can also include a standalone declaration if the corresponding attribute is set. It always contains the version attribute and a corresponding encoding attribute (usually with value "UTF-8", unless specified otherwise by attribute encoding of xsl:output ).

Note: This attribute cannot force an XML declaration in the form of a so-called text declaration for plain text output method="text". For such an XML fragment, the output format must be set to method="xml"; see example 3.

standalone

Value

"yes" | "no" | "omit"

Usage

Optional

Introduced in

XSLT 1.0

The standalone attribute is permitted with method="xml". It determines whether, and with what value, a standalone pseudo-attribute is added to the result document’s XML declaration.

With value "yes", pseudo-attribute standalone="yes" is added to the XML declaration; similarly, with "no", the value is standalone="no". If the attribute is omitted or its value is set to "omit", the result document’s XML declaration has no standalone pseudo-attribute.

undeclare-prefixes

Value

"yes" | "no"

Usage

Optional

Introduced in

XSLT 2.0

This attribute is used for XML output when the output XML version is newer than 1.0, meaning at least 1.1. Namespace undeclaration is fundamentally impossible for XML 1.0 documents.

From XML 1.1 onward, the requirement for an element to have fewer namespaces in scope than its parent can be reflected during serialization by namespace undeclaration. The namespace to remove is overwritten with a null namespace, an empty namespace URI, on that element. Attribute undeclare-prefixes is set to "yes". If no undeclaration is desired, use "no". Default behavior depends on the processor implementation.

Note: This attribute does not exist in XSLT 1.0.

use-character-maps

Value

qnames

Usage

Optional

Introduced in

XSLT 2.0

This attribute lists, as tokens, the QNames of named xsl:character-map declarations to use with this output declaration. During serialization, character mapping replaces a character, whether used directly or represented by a character entity in the source document or stylesheet, with a definable output string.

A named character map associates each character with a string. Thus, entity &#160; (non-breaking space) can be replaced directly by string &amp;nbsp;. Line breaks can likewise be controlled by replacing the LF line-break character &#xA; with string &#xD;&#xA; (CR followed by LF) for Windows environments.

The attribute works with all output formats. If omitted, no character mapping takes place.

Character mapping does not check whether the result violates well-formedness rules, since strings are inserted literally, as they are.

Note: This attribute does not exist in XSLT 1.0.

version

Value

nmtoken

Usage

Optional

Introduced in

XSLT 1.0

The version attribute, together with the method attribute, describes the output format’s version according to W3C guidelines. It can only be used with output formats "xml", "html" and "xhtml". Only valid versions should be used; for method="xml", currently version="1.0" or version="1.1".

The default for method="xml" and method="xhtml" is version="1.0"; for method="html", it is version="4.0". To output another version, supply the version attribute with the desired value; otherwise it can be omitted. For output format "text", version does not apply and is ignored if set.

Note: The value of attribute version in an output specification can, if necessary, be overridden from xsl:result-document using its attribute output-version.

Purpose:

Instruction xsl:output controls the output format of an XSLT transformation’s result document. Without such a declaration, the result follows the defaults determined by its root element and namespace. If its local name is not html, the result format is always XML. XSLT 2.0 can, however, use the namespace of a root element named <html> to distinguish HTML from XHTML.

The output need not be well-formed XML. This is certainly not the case for output formats "html" and "text", but neither is it guaranteed for output format "xml": a well-balanced XML fragment can be generated, optionally with an XML declaration without a standalone attribute, thus in the form of a text declaration, and used as an external general parsed entity. Such a document can start with a text node or lack a root element.

Behavior under XSLT 1.0:
Any number of xsl:output declarations may occur, from any source, including imported or included stylesheet modules. The XSLT processor effectively merges them all into one xsl:output declaration.

Duplicate attribute values are handled by import precedence, with the declaration having the highest precedence taking priority. For duplicates with equal import precedence, the processor may choose the last value in stylesheet order or report an error, even if the duplicate values are identical. For the combined declaration, attribute cdata-section-elements takes the union of all elements named in the component declarations.

Behavior under XSLT 2.0:
XSLT 2.0 still permits any number of xsl:output declarations, including those included or imported from external stylesheet modules. Handling duplicate attributes and values essentially follows XSLT 1.0. As with cdata-section-elements, attribute use-character-maps in the combined declaration takes the set of all named character maps.

The main innovation distinguishes an unnamed default output declaration from any number of output declarations named using the new name attribute.

Those xsl:output declarations without a name attribute are merged, taking import precedence into account, into one unnamed output declaration replacing the default output declaration.

All xsl:output declarations with identical name attributes are likewise merged, taking import precedence into account, into one named output declaration per name.

For the stylesheet’s immediate result document (initial result tree), the unnamed output declaration is used. If none exists, or only named declarations exist, the default values of xsl:output apply instead.

Named output declarations are needed when generating additional result documents using instruction xsl:result-document. The declaration is referenced through the for­mat attribute of xsl:result-document, which gives the QName of the desired output declaration. If no declaration with that name exists, this can be treated as a static error, stopping stylesheet execution.

If an xsl:result-document instruction is used without a format attribute, it references the unnamed output declaration or, if none exists, the corresponding defaults.

Special considerations for HTML output:

  • No end tags for empty HTML elements:
    Empty HTML elements receive no end tag, even in abbreviated form, although literal result elements in the stylesheet must use this XML notation. Empty HTML elements are <area>, <base>, <basefont>, <br>, <col>, <frame>, <hr>, <img>, <input>, <isindex>, <link>, <meta> and <param>. A line break, which must appear in the stylesheet as <br/> or <br></br>, is therefore output as <br>.

  • Tag-name recognition is case-insensitive:
    The output method should recognize HTML tag names in literal result elements regardless of case. For example, all the following spellings should be recognized as line breaks and output as <br> (without an end tag): <BR>, <Br>, <bR> and so on.

  • Special characters in text and attribute values; URL encoding:
    In output text, the XSLT processor should replace non-ASCII characters with their corresponding HTML character entities. The exact behavior can depend on the chosen HTML version.

    Conversely, HTML output should not escape any instances of < occurring in attribute content in the result document.

    In attributes containing URIs, such as the src attribute of frames or images, or all href attributes, non-ASCII characters should be escaped according to URL-encoding rules (see Section B.2.1 of the HTML 4.0 Recommendation).

  • No escaping of <script> or <style> container contents:
    Contents of containers holding JavaScript or CSS instructions should not be escaped in the result document. Escaping would make operators such as < or > unrecognizable to script interpreters. If the stylesheet contains the following (the character < must be escaped there outside CDATA sections):

    <script> if (a &lt; b) tu-etwas() </script>

    or, this time using a CDATA section (Note: In XHTML, script-block contents are also enclosed in CDATA sections for a similar reason), the following.

    <script><![CDATA[  if (a < b) tu-etwas()  ]]></script>,

    The output should in either case be produced without escaping:

    <script>if (a < b) tu-etwas()</script>

  • Abbreviated Boolean attributes:
    Abbreviated attribute forms permitted in HTML 4.0, known as Boolean attributes, should likewise be output in abbreviated form. In the stylesheet itself they must not be abbreviated; XHTML no longer permits that form either. For the following stylesheet element

    <input type="radio" selected="selected"/>

    The output should be:

    <input type="radio" selected>

  • Automatic content-type meta tag:
    The encoding character set the processor should use is determined by the encoding attribute of the xsl:output element. If a <head> element is output, a <meta> tag identifying the content type is inserted immediately. If no encoding is specified, UTF-8 is used:

    <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

    The media-type can also be set explicitly, but conveniently defaults to text/html, as shown here.

    Characters outside the selected content-type encoding are automatically replaced by the XSLT processor with corresponding Unicode character entities. This only applies to document parts where a browser could interpret such entities, so they are not intended for use in <script> or <style> containers (see above).

  • HTML DOCTYPE declaration:
    If the doctype-public or doctype-system attribute is used, the root element should be preceded by a <!DOCTYPE html ...> declaration with a PUBLIC or SYSTEM identifier. The identifiers are the values of the respective xsl:output attributes. If both attributes are used, the SYSTEM identifier follows the PUBLIC identifier in the declaration, as usual in XML.

Examples:

Example 1 – Generating an HTML 4.0 document:

Source document:

<?xml version="1.0"?>
<beispiel>Dies ist ein Beispieltext.<br/> Mehr Beispieltext.</beispiel>

Stylesheet:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" version="4.0"/>
  <xsl:template match="/">
    <html>
      <body>
       <xsl:copy-of select="."/>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Result document:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Beispiel HTML</title>
  </head>
  <body>Dies ist ein Beispieltext.<br> Mehr Beispieltext</body>
</html>

The result conforms to HTML 4.0. The empty tag <br/> copied from the source document is rewritten as <br> in accordance with HTML.

Example 2 – Generating XHTML with XSLT 1.0:

Source document:

<?xml version="1.0" encoding="ISO-8859-1"?>
<beispiel>Dies ist ein Beispieltext.<br/> Mehr Beispieltext.</beispiel>

Stylesheet:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/TR/xhtml1/transitional">
  <xsl:output method="xml" encoding="ISO-8859-1" media-type="text/html" indent="yes" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>Beispiel HTML</title>
      </head>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="beispiel">
    <p><xsl:copy-of select="node()"/></p>
  </xsl:template>
</xsl:stylesheet>

Result document (Xalan):

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/TR/xhtml1/transitional">
  <head>
    <title>Beispiel HTML</title>
  </head>
  <body>
    <p>Dies ist ein Beispieltext.<br /> Mehr Beispieltext.</p>
  </body>
</html>

The desired XHTML result is achieved in XSLT 1.0 using method="xml". The media-type is set to "text/html". Note the compatible output of the empty tag <br /> with a space before the slash.

Example 3 – Generating a general parsed entity with a text declaration:

Source document:

<?xml version="1.0"?>
<beispiel>Dies ist ein Beispieltext.</beispiel>

Stylesheet:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="ISO-8859-1"/>
  <xsl:template match="/">
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>

Result document:

<?xml version="1.0" encoding="ISO-8859-1"?>
Dies ist ein Beispieltext.

Only the XML output format can generate the preceding text declaration for this XML fragment.

Example 4 – Generating a plain-text document:

Source document:

<?xml version="1.0"?>
<beispiel>Dies ist ein Beispieltext.<br/> Mehr Beispieltext.</beispiel>

Stylesheet:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" media-type="text/plain"/>
  <xsl:template match="/">
    <plaintext>
      <doku>Ein einfacher Text:</doku>
      <xsl:apply-templates/>
    </plaintext>
  </xsl:template>
</xsl:stylesheet>

Result document:

Ein einfacher Text: Dies ist ein Beispieltext. Mehr Beispieltext.

If method="text" is selected as the output format, only the source document’s text nodes are considered. Literal result elements from the stylesheet are not copied either, only their text contents. No XML prolog can be placed before the result document. To force a text declaration, follow example 3.

Example 5 – xsl:output and xsl:result-document

The following example is derived from a corresponding example in the XSLT 2.0 specification, extended and annotated here.

Source document:

<?xml version="1.0" encoding="ISO-8859-1"?>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Roman in 100 Kapiteln</title>
  </head>
  <body>
    <h1>Erstes Kapitel</h1>
    <p>...</p>
    <h1>Zweites Kapitel</h1>
    <p>...</p>
    <p>...</p>
    <!-- mehr Kapitel -->
  </body>
</html>

The source is a large XHTML document divided into chapters, each introduced by a heading <h1>. Structures between headings are arbitrary.

Stylesheet:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <!-- keine unbenannte Outputdeklaration -->
  <!-- benannte Outputdeklaration für das Inhaltsverzeichnisdokument: -->
  <xsl:output name="inhalt-format" method="xhtml" indent="yes" encoding="ISO-8859-1" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"/>
  <!-- benannte Outputdeklaration für die Kapiteldokumente: -->
  <xsl:output name="kapitel-format" method="xhtml" indent="no" encoding="ISO-8859-1" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"/>
  <xsl:template match="/">
    <!-- Erzeugung des Inhaltsverzeichnisdokuments -->
    <xsl:result-document href="inhalt.html" format="inhalt-format" validation="strict">
      <html xmlns="http://www.w3.org/1999/xhtml">
        <head><title>Inhaltsverzeichnis</title></head>
        <body>
          <h1>Inhaltsverzeichnis</h1>
          <xsl:for-each select="/*/xhtml:body/(*[1] | xhtml:h1)">
            <p><a href="abschnitt{position()}.html"><xsl:value-of select="."/></a></p>
          </xsl:for-each>
        </body>
      </html>
    </xsl:result-document>
    <!-- Erzeugung der Kapiteldokumente: -->
    <xsl:for-each-group select="/*/xhtml:body/*" group-starting-with="xhtml:h1">
      <!-- Erzeugung eines Dokuments pro Kapitel: -->
      <xsl:result-document href="kapitel{position()}.html" format="kapitel-format" validation="strip">
        <html xmlns="http://www.w3.org/1999/xhtml">
          <head><xsl:value-of select="."/></head>
          <body>
            <!-- die gesamte Current Group wird kopiert: -->
            <xsl:copy-of select="current-group()"/>
          </body>
        </html>
      </xsl:result-document>
    </xsl:for-each-group>
  </xsl:template>
</xsl:stylesheet>

Explanation of the example:
This XSLT 2.0 stylesheet consists of two named output declarations and a template rule generating result documents using two xsl:result-document instructions. The template rule has no initial output, so no unnamed output declaration is needed and its defaults are not used.

First, the table-of-contents document is generated and assigned URI "inhalt.html". Attribute validation with value "strict" means here that the result document must conform to a schema determined by its namespace. A table of contents is generated within the document using an xsl:for-each (loop).

Next, the contents of the source document’s <body> tag are grouped using xsl:for-each-group into individual chapters, each starting with a heading. A result document is generated for each group, its URI derived from the group’s position in the resulting sequence. Function current-group() is used together with xsl:copy to copy each group into the result document’s body. The validation attribute of the xsl:result-document instruction has value "strip": the result document is not checked against a schema, and all data types are discarded. Since "strip" is the default, the attribute could also be omitted.

Result documents:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Inhaltsverzeichnis</title>
  </head>
  <body>
    <h1>Inhaltsverzeichnis</h1>
    <p><a href="kapitel1.html">Erstes Kapitel</a></p>
    <p><a href="kapitel2.html">Zweites Kapitel</a></p>
    <!-- (Links zu weiteren Kapiteln) -->
  </body>
</html>

Code example: inhalt.html.

The result is a strict XHTML document. It has an XML prolog with a doctype declaration as required by the xsl:output declaration. Its encoding follows the output declaration’s encoding attribute and is also reflected in the content-type meta tag.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitio¬nal.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Erstes Kapitel</title>
</head>
<body>
<h1>Erstes Kapitel</h1>
<p>...</p>
<!-- Rest des ersten Kapitels -->
</body>
</html>

Code example: kapitel1.html, representative of the chapter documents.

The individual chapters are transitional XHTML documents with corresponding XML prologs. Their namespace and content-type description match the table-of-contents document. However, the underlying output declaration has an indent attribute with value "no", so tags are not indented. This also accommodates possible mixed-content elements in the source document, which must not be indented because that would change their contents.

Element definition:

XSLT 1.0:

<!-- Category: top-level-element -->
<xsl:output 
  method = "xml" | "html" | "text" | qname-but-not-ncname 
  version = nmtoken 
  encoding = string 
  omit-xml-declaration = "yes" | "no"
  standalone = "yes" | "no"
  doctype-public = string 
  doctype-system = string 
  cdata-section-elements = qnames 
  indent = "yes" | "no"
  media-type = string 
/>

XSLT 2.0:

<!-- Category: declaration -->
<xsl:output
  name? = qname
  method?= "xml" | "html" | "xhtml" | "text" | qname-but-not-ncname
  byte-order-mark? = "yes" | "no"
  cdata-section-elements? = qnames
  doctype-public? = string
  doctype-system? = string
  encoding? = string
  escape-uri-attributes? = "yes" | "no"
  include-content-type? = "yes" | "no"
  indent? = "yes" | "no"
  media-type? = string
  normalization-form? = "NFC" | "NFD" | "NFKC" | "NFKD" | "fully-normalized" | "none" | nmtoken
  omit-xml-declaration? = "yes" | "no"
  standalone? = "yes" | "no" | "omit"
  undeclare-prefixes? = "yes" | "no"
  use-character-maps? = qnames
  version? = nmtoken 
/>

Note: XSLT 2.0 adds the seven attributes name, byte-order-mark, escape-uri-attributes, include-content-type, normalization-form, undeclare-prefixes and use-character-maps. The value range of the method attribute has been extended.

   

  

back to top