XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / comment
(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
The instruction xsl:comment creates a comment node.
Classification | Instruction |
Function group | Generate output in the result document |
Introduced | XSLT 1.0 |
Position in the stylesheet and permitted contents:
xsl:comment always appears within a block of template instructions. Text and further template instructions are allowed; their output becomes a comment in the result document.
Attributes:
Standard attributes apply. Since XSLT 2.0 the instruction has an optional element-specific attribute select, allowed only when the instruction is empty.
select
Value | xpath-expression |
Usage | Optional, only for an empty instruction! |
Introduced | XSLT 2.0 |
Attribute select can provide the comment node’s value instead of a sequence constructor inside instruction xsl:comment. It is allowed only when the instruction is empty. Violating this causes a static error, ERR XTSE0940.
Note: This attribute does not exist in XSLT 1.0.
Purpose:
The instruction xsl:comment adds a comment node to the result tree, with contents from its instruction block. Only if it has no contents may it have attribute select to determine the comment through an XPath expression. This attribute is unavailable in XSLT 1.0!
The inserted text must not contain “--”, which would end the comment, or end with “-”, which would create an illegal, ambiguous sequence of three hyphens. The XSLT parser is required to insert a space silently between consecutive hyphens or after a final hyphen, without reporting an error. (Note: Earlier Working Drafts suggested reporting an error. Check the target processor’s actual behavior if necessary.)
Source comments are normally not copied to the result: their contents are not considered relevant information. The XSLT parser ignores them through the empty default template unless it is explicitly overridden by a custom template, for example:
<!-- implizites Default-Template für Kommentarknoten -->
<xsl:template match="comment()"/>
<!-- selbst definiertes Override-Template (Beispiel) -->
<xsl:template match="comment()">
<xsl:copy/>
</xsl:template>
Example – Creating a comment node:
<xsl:comment>Dies ist eine Erklärung für alles!</xsl:comment>
produces in the result document:
<!-- Dies ist eine Erklärung für alles! -->Element definition:
XSLT 1.0:
<!-- Category: instruction -->
<xsl:comment>
<!-- Content: template -->
</xsl:comment>
XSLT 2.0:
<!-- Category: instruction -->
<xsl:comment
select? = expression >
<!-- Content: sequence-constructor -->
</xsl:comment>
Cross-references:
Constructors for the other node types exist as xsl:attribute, xsl:element, xsl:processing-instruction, xsl:text and, since XSLT 2.0, xsl:namespace.
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