XSLT - Identity Transformation Pattern in XSLT

The Identity Transformation Pattern is one of the most important and widely used techniques in XSLT. It serves as a foundation for many XML transformation tasks because it allows an XML document to be copied exactly as it is while providing the flexibility to modify selected elements, attributes, or content during the transformation process.

Instead of writing separate templates for every element in an XML document, the identity transformation pattern automatically copies all nodes from the source document to the output document. Developers can then create additional templates to override specific parts that need to be changed. This approach makes XSLT stylesheets more efficient, maintainable, and scalable.

Understanding Identity Transformation

The primary purpose of identity transformation is to create an exact copy of the input XML document. It processes every node, including:

  • Elements

  • Attributes

  • Text nodes

  • Comments

  • Processing instructions

The identity template uses recursive processing to traverse the entire XML tree and reproduce it in the output document.

Basic Identity Template

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

Explanation

match="@*|node()"

This pattern matches:

  • All attributes (@*)

  • All nodes (node())

As a result, every part of the XML document becomes eligible for processing.

xsl:copy

The <xsl:copy> instruction creates a copy of the current node.

For example:

Input:

<book>
    <title>XSLT Guide</title>
</book>

Output:

<book>
    <title>XSLT Guide</title>
</book>

The structure is preserved exactly.

xsl:apply-templates

This instruction tells XSLT to continue processing child nodes and attributes recursively.

Without it, only the current node would be copied, and its contents would be omitted.

Example of Complete Identity Transformation

XML Input

<library>
    <book id="101">
        <title>XML Basics</title>
    </book>
    <book id="102">
        <title>XSLT Fundamentals</title>
    </book>
</library>

XSLT Stylesheet

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Output

<library>
    <book id="101">
        <title>XML Basics</title>
    </book>
    <book id="102">
        <title>XSLT Fundamentals</title>
    </book>
</library>

The output is identical to the input.

Modifying Specific Elements

The real power of identity transformation appears when you need to change only selected parts of an XML document.

Example: Change a Book Title

XML Input

<library>
    <book>
        <title>XML Basics</title>
    </book>
</library>

XSLT

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="title">
    <title>Advanced XML</title>
</xsl:template>

Output

<library>
    <book>
        <title>Advanced XML</title>
    </book>
</library>

Only the title element is modified, while everything else remains unchanged.

Removing Elements

Identity transformation can also be used to remove unwanted elements.

XML Input

<employee>
    <name>John</name>
    <salary>50000</salary>
</employee>

XSLT

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="salary"/>

Output

<employee>
    <name>John</name>
</employee>

The empty template matching salary suppresses that element.

Adding New Elements

XML Input

<employee>
    <name>John</name>
</employee>

XSLT

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="employee">
    <employee>
        <xsl:apply-templates select="@*|node()"/>
        <department>IT</department>
    </employee>
</xsl:template>

Output

<employee>
    <name>John</name>
    <department>IT</department>
</employee>

A new department element is inserted while preserving the original content.

Updating Attributes

XML Input

<product id="1001">
    <name>Laptop</name>
</product>

XSLT

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="@id">
    <xsl:attribute name="id">
        P1001
    </xsl:attribute>
</xsl:template>

Output

<product id="P1001">
    <name>Laptop</name>
</product>

The attribute value is modified while preserving the rest of the document.

Benefits of Identity Transformation

Reduced Code

Instead of creating templates for every element, a single identity template handles the entire document.

Easy Maintenance

Changes can be made by adding or modifying specific templates without affecting the rest of the stylesheet.

Flexibility

Developers can:

  • Copy documents unchanged

  • Add elements

  • Remove nodes

  • Modify content

  • Update attributes

Reusability

The identity template can be reused in many XML transformation projects.

Scalability

Large XML documents can be processed efficiently because the transformation automatically handles all nodes.

Common Use Cases

XML Cleanup

Removing unwanted tags, comments, or attributes while preserving the document structure.

Data Migration

Transforming XML data from one format to another with minimal changes.

XML Enrichment

Adding additional information to existing XML documents.

Content Filtering

Keeping only relevant information and removing unnecessary data.

Document Standardization

Converting XML files into a consistent structure across multiple systems.

Best Practices

  1. Always place the identity template near the beginning of the stylesheet.

  2. Create specialized templates only for nodes that require modification.

  3. Use clear match patterns to avoid unexpected changes.

  4. Test transformations with large XML documents to verify performance.

  5. Keep overriding templates simple and focused on a single task.

Conclusion

The Identity Transformation Pattern is a fundamental XSLT technique that copies an XML document while allowing selective modifications. It acts as a default mechanism for processing all nodes and provides a clean, efficient way to update, remove, or add content without rewriting the entire document structure. Because of its simplicity, flexibility, and maintainability, it is considered one of the most valuable design patterns in XSLT development.