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:

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.