XSLT Identity Transform for XML Filtering

An XSLT identity transform is a fundamental design pattern used to copy an XML document verbatim from source to output while serving as a baseline for targeted modifications. This article explains what the identity transform is, how it functions mechanically within XSLT, and why it is the most efficient and maintainable foundation for selective XML filtering, stripping, and modification.


What Is an XSLT Identity Transform?

An identity transform is an XSLT stylesheet designed to replicate every element, attribute, text node, comment, and processing instruction from an input XML document directly into the output document without making any changes.

In XSLT 1.0 and 2.0, the core identity transform is written using a recursive template:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- Identity Template -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

In XSLT 3.0, this pattern is built natively into the language and can be declared simply with:

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:mode on-no-match="shallow-copy"/>
</xsl:stylesheet>

How It Works

  1. match="@*|node()" captures every attribute (@*) and every node type (elements, text, comments, processing instructions).
  2. <xsl:copy> duplicates the current node without its children or attributes.
  3. <xsl:apply-templates select="@*|node()"/> recursively triggers the template for all attributes and child nodes of the copied node.

Why It Is Ideal for Selective XML Filtering

Without an identity transform, modifying an XML document requires writing explicit templates for every single element you want to keep, which is tedious, error-prone, and fragile when XML schemas evolve.

The identity transform reverses this workflow by establishing a “copy everything by default” rule, providing several distinct advantages for selective filtering:

1. Exception-Based Programming

XSLT uses pattern matching with built-in priority rules. By using the identity transform as a fallback, you only need to write templates for the specific nodes you wish to filter out or modify. The XSLT processor automatically overrides the generic identity template whenever a more specific match rule exists.

2. Simple Element and Attribute Removal

To filter out unwanted data, you match the target node with an empty template. Because the template contains no output instructions, the matched node and its descendants are dropped from the output tree.

Example: Removing sensitive data like <ssn> or an attribute @internal-id:

<!-- Drop the ssn element and its children -->
<xsl:template match="ssn"/>

<!-- Drop the internal-id attribute -->
<xsl:template match="@internal-id"/>

3. Targeted Data Transformation

You can alter specific values, rename elements, or wrap content without disturbing the surrounding hierarchy.

Example: Masking an email address while preserving all other elements:

<xsl:template match="email/text()">
    <xsl:text>***@***.com</xsl:text>
</xsl:template>

4. High Maintainability and Schema Resilience

When the source XML schema updates with new, unrelated elements, stylesheets based on the identity transform automatically pass the new data through without requiring manual updates to the stylesheet.

Summary

The XSLT identity transform establishes an automated, recursive baseline that duplicates entire document trees. By combining it with specific, overriding templates, developers can selectively filter, strip, or alter targeted XML nodes with minimal code, high readability, and reduced maintenance overhead.