What Is the Identity Transform Pattern in XSLT?
The identity transform pattern in XSLT is a foundational idiom that copies an XML document verbatim from input to output unless explicit templates override specific nodes. By acting as a non-destructive baseline, it allows developers to write targeted rules for modifying, adding, or deleting individual elements without rebuilding the entire document structure from scratch.
Understanding the Identity Transform Pattern
In XSLT, default template rules process child nodes and output only
plain text content, discarding XML element tags and attributes. The
identity transform overrides this built-in behavior by using an explicit
template matching every node and attribute (@*|node()).
Instead of stripping markup, it clones the current node with
<xsl:copy> and recursively applies templates to all
child nodes and attributes using
<xsl:apply-templates select="@*|node()"/>.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- The Identity Template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>In XSLT 3.0, this idiom is streamlined through declarative modes like
<xsl:mode on-no-match="shallow-copy"/>, which
internalizes the identity logic.
Why It Is a Foundational Idiom
The identity transform serves as the bedrock of declarative XML processing for several core reasons:
- Selective Modification: It establishes a "copy everything by default" rule. To modify an element, rename an attribute, or delete a subtree, developers only write small, highly focused templates.
- Separation of Concerns: Each transformation rule handles only the specific XML structure it targets, leaving the rest of the document processing to the generic copy template.
- Maintainability and Resilience: If the input XML schema evolves with new elements or unexpected metadata, the identity transform carries those additions through safely without requiring stylesheet updates.
- Idiomatic Declarative Style: It fully embraces the pattern-matching model of XSLT, replacing brittle procedural traversal with clean, rule-based node overrides.
By shifting the developer's focus from recreating an entire document tree to defining only the intended deltas, the identity transform pattern remains the primary design pattern for XML manipulation in XSLT.