XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / result-document
(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
Instruction xsl:result-document is needed when a transformation must generate one or more secondary result documents. Each document’s URI can be specified and a named output format xsl:output assigned to it.
Classification | Instruction |
Function group | Secondary result document |
Introduced in | XSLT 2.0 |
Stylesheet position and content:
Instruction xsl:result-document may occur any number of times within template rules, but not in XSLT elements that create a temporary tree, such as xsl:variable, or, like xsl:message, a tree that is not serialized as a document.
The instruction contains template content in the form of a sequence constructor generating a sequence of nodes that forms the secondary result document’s tree.
Attributes:
Standard attributes apply. Instruction xsl:result-document also has twenty optional element-specific attributes, all of which except use-character-maps may contain attribute value templates. Some correspond to serialization parameters introduced by specification “XSLT 2.0 and XQuery 1.0 Serialization”, as also specified in declaration xsl:output.
Note: If an xsl:result-document instruction uses its format attribute to reference a named declaration xsl:output, its attributes take precedence over those of the output declaration where applicable. The same applies without a format attribute, except that the unnamed output declaration is referenced in that case.
byte-order-mark
Value | {"yes" | "no"} (also as an AVT) |
Usage | optional |
Introduced in | XSLT 2.0 |
The byte-order-mark attribute determines whether a byte-order mark (BOM) is placed at the start of the output file. Its value may be supplied through an attribute value template.
This attribute of xsl:result-document takes precedence over the same-named attribute of the xsl:output declaration, or its default, selected by the instruction’s format attribute, or of the unnamed output declaration if no format attribute is set.
Thus, the byte-order-mark attribute can always force a BOM or suppress one that the output declaration would otherwise request.
The byte-order mark consists of the Unicode character U+FEFF (zero width no-break space), placed at the start of the resource. Depending on the encoding, this character becomes a specific byte sequence that identifies the encoding to a processing application; see the following table.
The mark tells an application reading the resource which encoding it uses. Applications can often infer this from the resource’s first characters. This matters particularly for encodings UTF-16 (two bytes per character) and UTF-32 (four bytes per character), which require distinguishing big-endian from little-endian encoding. (Note: Intel CPUs internally use little-endian memory organization, whereas Motorola CPUs use big-endian. Programming language Java also favors big-endian.)
This concerns the order in which a character’s bytes are stored: most significant bits first (big-endian) or least significant bits first (little-endian). In UTF-32, two of the four bytes are filled with zero values.
Note: UTF-32 is virtually identical to UCS-4 as defined in ISO 10646, so its BOM has the same form.
Encoding | BOM byte sequence |
|---|---|
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 byte sequences in different character encodings (source: Wikipedia).
Note: For UTF-16, the default is "yes"; for UTF-8, it depends on the implementation, since UTF-8 needs no BOM. For all other encodings, the default is "no". Non-Unicode formats have no BOM.
cdata-section-elements
Value | {qnames} (also as an AVT) |
Usage | optional |
Introduced in | XSLT 2.0 |
The cdata-section-elements attribute is permitted with method="xml". Its value may be supplied through an attribute value template. It lists, separated by spaces, the QNames of all elements whose text nodes in the secondary result document should be output within CDATA sections:
<![CDATA[ ... Textnode ... ]]>
Defining CDATA sections is useful when parts of the content should not have markup, special or control characters escaped, or when outputting content that could affect the result document’s well-formedness, such as JavaScript code. For output of XHTML documents, this particularly applies to containers <script> and <style>.
Note – This attribute does not override the output declaration:
The value of attribute cdata-section-elements does not override or erase an existing value in the relevant output declaration. Instead, these element names supplement those already specified. The combined effect is the union (union) of both attribute values.
doctype-public
Value | {string} (also as an AVT) |
Usage | optional |
Introduced in | XSLT 2.0 |
The doctype-public attribute, together with the doctype-system attribute, specifies the additional optional PUBLIC identifier for the secondary result document’s doctype declaration. Its value may be supplied through an attribute value template.
This attribute is useful with all output methods permitting a doctype declaration, namely "xml", "html" and "xhtml". Otherwise it is ignored.
This attribute of xsl:result-element overrides the same-named output-declaration attribute where applicable, but is ignored unless, on either xsl:result-document or its associated xsl:output declaration, a value for doctype-system is also set.
doctype-system
Value | {string} (also as an AVT) |
Usage | optional |
Introduced in | XSLT 2.0 |
The doctype-system attribute assigns a potential SYSTEM identifier as a URI string for use in the secondary result document’s doctype declaration. Its value may be supplied through an attribute value template.
This attribute is useful with all output methods permitting a doctype declaration, namely "xml", "html" and, since XSLT 2.0, "xhtml". Otherwise it is ignored.
This attribute of xsl:result-element overrides the same-named output-declaration attribute where applicable. If a doctype-system attribute is set, whether on xsl:result-document or its associated xsl:output declaration, the doctype-public attribute can also take effect.
encoding
Value | {string} (also as an AVT) |
Usage | optional |
Introduced in | XSLT 2.0 |
The encoding attribute specifies the character encoding of the secondary result document, describing the binary representation of character data, for example UTF-8, ISO-8859-1. Its value may be supplied through an attribute value template.
This attribute of xsl:result-element overrides the same-named output-declaration attribute where applicable. Permitted values name valid encoding formats (charsets) registered with IANA under RFC 2278. Charset names are recognized regardless of case; the attribute value is not case-sensitive. For example, UTF-8 and utf-8 are equivalent.
If the requested encoding is unrecognized or unsupported by the XSLT processor, UTF-8 is used as the standard encoding. The encoding attribute may be used with all values of method.
escape-uri-attributes
Value | {"yes" | "no"} (also as an AVT) |
Usage | optional |
Introduced in | XSLT 2.0 |
The escape-uri-attributes attribute determines whether attribute values of type xs:anyURI are escaped in secondary HTML or XHTML result documents. Its value may be supplied through an attribute value template.
Escaping is generally considered useful, so the attribute defaults to "yes".
Escaping follows the method recommended in RFC 2396, Section 2.4.1. Unrepresentable characters are replaced by hexadecimal pairs preceded by a % character; for example, a space becomes %20. Besides spaces, control characters and URI delimiters must be escaped when needed literally, including the percent sign itself: <, >, #, %, ".
format
Value | {qname} (also as an AVT) |
Usage | optional |
Introduced in | XSLT 2.0 |
The value of attribute format must be a QName matching the name of a named xsl:output declaration. Its value may be supplied through an attribute value template. Names are compared in expanded form: identical local names with different prefixes are still considered equal when they refer to the same namespace.
If the supplied value is not a valid QName, or no output declaration with that name exists, the processor reports a runtime error (ERR XTDE1460). If the name is not supplied through an AVT, the error can also be reported statically.
If attribute format names a valid output declaration, that declaration is used to serialize the document with its serialization parameters, meaning attribute values or defaults, except where overridden individually by same-named attributes of xsl:result-document.
If the format attribute is omitted, the xsl:result-document instruction similarly references the unnamed xsl:output declaration. If none exists, the secondary document is serialized using the defaults specified for xsl:output.
href
Value | {uri-reference} (also as an AVT) |
Usage | optional |
Introduced in | XSLT 2.0 |
The href attribute specifies the document’s filename and path, absolutely or relative to the XSLT stylesheet’s base URI. Its value may be supplied through an attribute value template.
The value must be a relative or absolute URI string, and the XSLT processor must accept every syntactically correct URI string.
The empty string is also a valid relative URI string and is the default when no href attribute is used. This can intentionally generate an unnamed document whose URI is the base URI from the static context.
At most one xsl:result-document instruction may occur without an href attribute. This necessarily follows because no two such instructions may share a target document, meaning they cannot have the same value, including no value, for attribute href.
include-content-type
Value | {"yes" | "no"} (also as an AVT) |
Usage | optional |
Introduced in | XSLT 2.0 |
The include-content-type attribute determines whether the XSLT processor should add a meta tag with a content-type description to the secondary result document, as follows:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Its value may be supplied through an attribute value template. The attribute works with HTML or XHTML output and defaults to "yes".
indent
Value | {"yes" | "no"} (also as an AVT) |
Usage | optional |
Introduced in | XSLT 2.0 |
The indent attribute determines whether the secondary result document’s source text is indented. Its value may be supplied through an attribute value template. The default is "yes" for html and xhtml output. For xml output, however, the default is "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". Furthermore, the indent attribute does not affect whitespace already present in the result document, so it neither normalizes nor strips whitespace before indenting.
media-type
Value | {string} (also as an AVT) |
Usage | optional |
Introduced in | XSLT 2.0 |
The media-type attribute adds an appropriate MIME type to the secondary result document, potentially needed for HTTP transmission. Its value may be supplied through an attribute value template. It works with all values of method. It affects only file properties for storage or transmission according to runtime environment, not the document’s content.
All output formats have defaults that apply when the media-type attribute is omitted. For XML output the default is "text/xml", HTML/XHTML use "text/html", plain text uses "text/plain".
method
Value | {"xml" | "xhtml" | "html" | "text" | qname-but-not-ncname} |
Usage | optional |
Introduced in | XSLT 2.0 |
The method attribute specifies the secondary result document’s type. Permitted predefined values are "xml", "xhtml", "html" and "text". Its value may be supplied through an attribute value template.
Without attribute method, output defaults to "xml" unless the result root (final result tree) is named <html>. In this case, format "html" is selected. If that root element also belongs to the XHTML namespace, the format is "xhtml" (only from XSLT 2.0 onward; for XSLT 1.0 stylesheets, the processor falls back to format "xml" ).
Note: Processor vendors may implement additional output formats named by QNames with namespace prefixes; these use 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 documents 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 declaration, but not an internal subset of a DTD.
method="xhtml"
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".
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 with this media type is inserted unless the include-content-type attribute 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 characters or characters absent from the selected output encoding are replaced by corresponding HTML entities. If no such entity exists, the character is represented by a numeric character reference. 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 as secondary result documents with output method "text", but require method "xml". There is no way to output a text declaration with output format "text".
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 secondary result document. Its value may be supplied through an attribute value template.
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 singleton characters consisting of a single 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.
omit-xml-declaration
Value | {"yes" | "no"} (also as an AVT) |
Usage | optional |
Introduced in | XSLT 2.0 |
The omit-xml-declaration attribute determines whether to omit the XML declaration normally added to the secondary result document by the XSLT processor. Its value may be supplied through an attribute value template. 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".
output-version
Value | nmtoken |
Usage | optional |
Introduced in | XSLT 2.0 |
The output-version attribute, together with the method attribute, describes the output format’s version according to W3C guidelines. It corresponds to the version attribute of the associated xsl:output declaration, not the standard attribute version, and overrides its value where applicable. Its value may be supplied through an attribute value template.
The attribute can only be used with output formats "xml", "html" and "xhtml". Only valid versions should be used; for method="xml", currently output-version="1.0" or output-version="1.1". For output format "text", attribute output-version does not apply and is ignored.
Otherwise, the defaults of the output declaration’s version attribute apply; for method="xml" and method="xhtml", it is version="1.0"; for method="html", it is version="4.0".
standalone
Value | {"yes" | "no" | "omit"} (also as an AVT) |
Usage | optional |
Introduced in | XSLT 2.0 |
The standalone attribute is permitted with method="xml". It determines whether, and with what value, a standalone declaration is added to the secondary result document’s XML prolog. Its value may be supplied through an attribute value template.
The attribute overrides the value of the same-named attribute standalone in the output declaration where applicable. With value "yes", pseudo-attribute standalone="yes" is added to the XML declaration; similarly, with "no", the value is standalone="no". Setting the attribute to "omit" suppresses insertion of the standalone pseudo-attribute in the secondary result document. Simply omitting the attribute instead activates the output attribute or its default.
type
Value | QName |
Usage | optional |
Introduced in | XSLT 2.0 |
Since the generated document node itself has no type, the type attribute is not applied directly to it. The supplied schema-type QName instead refers to the primary node of the generated document tree, its root element node.
This attribute does not permit an attribute value template. It is a static error if no declaration with the supplied QName exists (ERR XTSE1520).
Validation of the root element follows the usual procedure for element nodes (Note: A formal, though rather theoretical, description appears in the XML Schema specification, Part 1, Section 3.3.4). If validation succeeds, meaning that property validity in the generated PSVI has value "valid" for the node, the supplied type is associated with the element. Otherwise stylesheet processing stops (ERR XTTE1540).
undeclare-prefixes
Value | {"yes" | "no"} (also as an AVT) |
Usage | optional |
Introduced in | XSLT 2.0 |
This attribute is used for XML output when the secondary result document’s XML version is newer than 1.0, meaning at least 1.1. Namespace undeclaration is fundamentally impossible for XML 1.0 documents. Its value may be supplied through an attribute value template.
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.
use-character-maps
Value | qnames |
Usage | Optional |
Introduced in | XSLT 2.0 |
Attribute use-character-maps lists, as tokens, the QNames of named xsl:character-map declarations to use with the output declaration. It works with all output formats. This attribute does not permit an attribute value template.
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   (non-breaking space) can be replaced directly by string &nbsp;. Line breaks can likewise be controlled by replacing the LF line-break character 
 with string 
 (CR followed by LF) for Windows environments.
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 override the output declaration:
The value of attribute use-character-maps does not override or erase an existing value in the relevant output declaration. Instead, the character-map names supplement those already specified. The combined effect is the union (union) of both attribute values.
validation
Value | "strict" | "lax" | "preserve" | "strip" |
Usage | optional |
Introduced in | XSLT 2.0 |
The validation attribute determines whether, and under which rule, the root element of the generated document tree’s document node is validated. This attribute does not permit an attribute value template.
There is no direct default: if the attribute is absent, the value of the stylesheet element’s default-validation attribute (xsl:stylesheet or xsl:transform) containing the instruction is used instead. If the stylesheet element has no default-validation attribute, its default applies: "strip".
The four possible explicit values of attribute validation are strict, lax, preserve and strip. Their effects are as follows:
validation="strict"
Value strict forces strict schema validation of the document tree’s root element. The root element and its permitted content must therefore be declared in a schema available at this point through the usual XML Schema methods. This can happen implicitly through the namespace or explicitly for its elements through an attached xsi:schemaLocation attribute. The node’s resulting validity property in the corresponding PSVI must have value "valid". The root element then receives the corresponding type annotation. Existing type annotations within the document tree are preserved.
If the property has value "invalid" or "notKnown", the transformation fails here with a type error (type error). A type error also occurs if the document node’s content, besides a single element node as root, is not exclusively comment and processing-instruction nodes.
validation="lax"
Value lax requires validation of the document tree’s root element against an available schema. However, no error occurs if no schema is available or the root element is not declared in an existing schema. The node’s validity property can therefore have not only value "valid", but also "notKnown". In the latter case, the unvalidated root element is assigned type xdt:untyped. Existing type annotations within the document tree are preserved.
If, however, the validity property has value "invalid", the transformation fails here with a type error (type error). A type error also occurs if the document node’s content, besides a single element node as root, is not exclusively comment and processing-instruction nodes.
validation="preserve"
Value preserve preserves explicit associations of elements and attributes in the validated document tree with particular data types. An explicit association may, for example, have been established through the validation attribute on xsl:element or xsl:copy-of. All other element and attribute nodes lose any type annotation and are assigned type xs:anyType for elements and xs:anySimpleType for attributes. For these nodes, this matches the behavior of validation="none".
validation="strip"
Value strip removes every type annotation from all nodes in the validated document tree and assigns all element nodes type xs:untyped and all attribute nodes type xs:anySimpleType.
Purpose:
Instruction xsl:result-document generates, besides the primary result tree produced during transformation (principal result tree), additional so-called secondary result trees. This fills a long-felt gap in XSLT 1.0, though various proprietary processor-specific extension functions already provided solutions: xt:document for XT, saxon:output for Saxon, exslt:document in the EXSLT extensions.
The xsl:result-document instruction now allows documents to be created or modified without processor-specific extensions. The resulting document need not be XML: referencing named xsl:output declarations also allows plain-text files such as CSV files or SQL statements to be generated.
No effect on the primary result tree
The xsl:result-document instruction contributes nothing to the primary result tree. Technically, it outputs an empty sequence into the sequence underlying that tree’s serialization, so evaluating the instruction cannot add nodes to this tree.
No output to temporary trees
An important restriction on instruction xsl:result-document is that it can generate its result tree only as a final secondary result tree, not as a temporary tree. This ensures that stylesheets and their results remain consistent even if the evaluation order of xsl:result-document instructions changes, for example through performance optimization.
Instruction xsl:result-document therefore cannot be used within XSLT instructions whose result is a temporary tree, including xsl:variable, xsl:param and xsl:function. If a validatable document must be generated as a temporary tree, instruction xsl:document can be used.
Other instructions within which this instruction is forbidden are xsl:with-param, xsl:attribute, xsl:comment, xsl:processing-instruction, xsl:namespace and xsl:message.
URI of the secondary document tree
Each xsl:result-document instruction creates a document node when instantiated and attaches the tree generated by its sequence constructor. The optional href attribute sets the document tree’s URI, which can be absolute or relative. If a file already exists at the target, serialization overwrites it; otherwise a new file is created if write permissions allow.
However, no two xsl:result-document instructions in a stylesheet may use the same URI. A supplied relative URI, including the empty string, is resolved against the static context’s base URI.
Validating the result tree:
A secondary result tree can be validated before serialization. Validation is determined by the value of its validation attribute or, if absent, the default-validation attribute of the xsl:stylesheet element.
If the value of attribute validation is "strict" or "lax", a type error occurs if the document node does not contain exactly one element node plus optionally any number of comment and processing-instruction nodes in any order.
Validation applies not to the document node but to its child element, the document element. Only this element is validated; its existing child nodes are retained without further changes. Any schema conditions (constraints) for the validated element, in the form of xs:key-, xs:keyref- or xs:unique definitions in the schema, are considered and must be satisfied.
Timing and format of serialization:
Each secondary document is serialized only after the entire transformation has completed. This first guards against failure of the transformation after an xsl:result-document instruction has been instantiated. It also allows the same XML document to serve as data source through function document() and, after transformation, as write target through xsl:result-document.
The format attribute can also name the xsl:output declaration used for serialization. This allows one transformation to generate result documents of different types, such as an HTML frameset with corresponding content pages using method="html" or "xhtml" and media-type="text/html", and additionally embedded illustrations in SVG format using method="xml" and media-type="image/svg+xml".
Note – Secondary result documents and write permissions:
Generating multiple result documents during a transformation will generally be restricted to environments granting the XSLT processor filesystem write access. This is likely to be problematic for browser-based XSLT transformations such as those supported by Internet Explorer and Mozilla.
Examples:
Example 1 – Generating an HTML frameset:
XML document:
<beispiel>Hallo Welt!</beispiel>
Stylesheet:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="html"/>
<xsl:output name="inhalt" method="html" indent="no"/>
<xsl:template match="/">
<!-- Frameset schreiben: -->
<html>
<frameset rows="*,*">
<frame name="oben" src="oben.html" />
<frame name="unten" src="unten.html" />
</frameset>
</html>
<!-- Inhalte schreiben: -->
<xsl:result-document format="inhalt" href="oben.html">
<html>
<body bgcolor="red">
<h1><xsl:apply-templates/></h1>
</body>
</html>
</xsl:result-document>
<xsl:result-document format="inhalt" href="unten.html">
<html>
<body bgcolor="blue">
<p><xsl:apply-templates/></p>
</body>
</html>
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>
This stylesheet first generates a frameset as its primary result document, then uses xsl:result-document to generate two secondary result documents referenced as content documents in the first. A named xsl:output declaration is used for the content documents.
Example 2 – Updating information stored in XML:
Data source 'zaehler.xml':
<zaehler>100</zaehler>
Stylesheet:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="zaehler" select="number(document('zaehler.xml')/zaehler)"/>
<html>
<body>
<p><xsl:text>Der aktuelle Zählerstand ist: </xsl:text><!-- Zaehlerstand auslesen --><xsl:value-of select="$zaehler"/></p>
</body>
</html>
<xsl:result-document href="zaehler.xml">
<!-- neuen Zaehlerstand schreiben -->
<zaehler><xsl:value-of select="$zaehler + 1"/></zaehler>
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>
The same XML document serves both as data source and write target. This can also accompany a transformation generating a primary result document, for example to record how many times the stylesheet has been called.
This example only reads a small XML file holding a counter through function document() and stores it in a variable. Instantiating xsl:result-document reads the variable, increments the counter and generates a new XML document with the updated counter, overwriting and replacing the original.
Element definition:
XSLT 1.0:
Element unavailable in XSLT 1.0.
XSLT 2.0:
<!-- Category: instruction -->
<xsl:result-document
format? = {qname}
href? = {uri-reference}
validation? = "strict" | "lax" | "preserve" | "strip"
type? = 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
output-version? = {nmtoken}>
<!-- Content: sequence-constructor -->
</xsl:result-document>
Note: All twenty attributes of xsl:result-document are optional. Only attributes type, use-character-maps and validation do not permit attribute value templates. Except for attributes format, href, type and validation, all attributes correspond to same-named attributes of an output declaration xsl:output and override their values where applicable. Only output-version corresponds in xsl:output, for historical reasons, to its version attribute.
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn