El Comment
π
2026-06-14 | π XML
## XSLT `
` Element
The `` element is a structural XSLT element used to generate a comment node within the output (result) tree.
In modern web development and document transformation pipelines, dynamically generating comments is highly useful for adding metadata, debugging information, licensing headers, or structural markers to the output HTML, XML, or text documents.
---
## Definition and Usage
The `` element allows you to insert standard XML/HTML comments (``) into your output document dynamically.
### Key Characteristics:
* **Output Format:** The content inside `` is wrapped in `` in the final output document.
* **Dynamic Content:** You can use XSLT expressions (such as ``) inside the comment to output dynamic data, such as generation timestamps, version numbers, or system variables.
* **Safety:** The XSLT processor automatically handles special characters inside the comment to ensure the output remains well-formed. For example, it will prevent double hyphens (`--`) from breaking the comment structure according to XML standards.
---
## Syntax
```xml
```
### Attributes
* **None.** The `` element does not accept any attributes. Its behavior is entirely defined by its text content or child templates.
---
## Code Examples
### Example 1: Static Comment
This basic example demonstrates how to write a simple, static comment into the output document.
**XSLT Template:**
```xml
This is a static comment in the output document!
```
**Output:**
```html
```
---
### Example 2: Dynamic Comment with ``
You can make your comments dynamic by evaluating expressions inside the `` block. This is highly useful for adding metadata like document authors or dynamic IDs.
**Source XML:**
```xml
XSLT Programmer's Reference
Michael Kay
```
**XSLT Template:**
```xml
Generated from book title:
Author:
```
**Output:**
```html
XSLT Programmer's Reference
```
---
## Important Considerations
1. **Handling Double Hyphens (`--`):**
According to the XML specification, a comment must not contain the string `--` (double hyphen) and must not end with a `-` (hyphen). If your template generates a double hyphen inside ``, the XSLT processor may throw an error or automatically insert spaces (e.g., changing `--` to `- -`) to keep the output document well-formed.
2. **No Markup Inside Comments:**
You cannot output actual XML elements inside a comment. If you place elements like `` or `` inside ``, their tags will either be stripped or converted into plain text, depending on the XSLT processor.
3. **Whitespace Preservation:**
Whitespace and line breaks inside the `` block are preserved in the output. If you want a clean, single-line comment, write the opening and closing tags on the same line as the text.