XSLT - Using <xsl:copy> and <xsl:copy-of> for Node Duplication in XSLT
In XSLT, copying nodes from the source XML document to the output document is a common requirement during transformation. Two important instructions used for this purpose are <xsl:copy> and <xsl:copy-of>. Although both are used to duplicate nodes, they behave differently in how they process elements, attributes, and child nodes. Understanding their differences helps developers control how much of the XML structure is reproduced in the output.
<xsl:copy>
The <xsl:copy> instruction creates a copy of the current node without automatically copying all of its child nodes and attributes. It only copies the node itself. If you want the children or attributes to appear in the output, you must explicitly process them using instructions such as <xsl:apply-templates> or <xsl:copy-of>. This allows more control over how the content inside the node is handled.
Example:
<xsl:template match="book">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
In this example, <xsl:copy> duplicates the <book> element, and <xsl:apply-templates> ensures that its attributes and child nodes are processed and copied according to other templates. This approach is often used when you want to copy a node but still modify or filter some of its content.
<xsl:copy-of>
The <xsl:copy-of> instruction performs a deep copy of the selected node. This means it copies the node along with all its attributes, child elements, text nodes, and sub-elements automatically. It does not require additional template processing to include the full structure.
Example:
<xsl:template match="/">
<library>
<xsl:copy-of select="books/book"/>
</library>
</xsl:template>
In this example, <xsl:copy-of> copies every <book> element inside <books> along with its entire internal structure and places it inside the <library> element in the output.
Key Differences
The main difference between <xsl:copy> and <xsl:copy-of> lies in the level of copying. <xsl:copy> duplicates only the current node and requires additional instructions to process child nodes and attributes. In contrast, <xsl:copy-of> performs a complete copy of the selected node and all its descendants automatically.
Another important difference is flexibility. <xsl:copy> allows developers to modify, filter, or transform the children of a node while copying it. <xsl:copy-of> simply duplicates the node structure exactly as it exists in the source document without applying further template rules.
Practical Use Cases
<xsl:copy> is commonly used when performing an identity transformation where most of the XML document is copied but some elements are modified or removed. It allows selective processing of the content inside the node.
<xsl:copy-of> is useful when entire sections of the XML document need to be replicated in the output without modification. It is frequently used for copying node sets, fragments of XML, or external data retrieved through XPath expressions.
Conclusion
Both <xsl:copy> and <xsl:copy-of> play an important role in XML transformation using XSLT. <xsl:copy> provides controlled copying with the ability to process children separately, while <xsl:copy-of> performs a complete duplication of nodes and their entire structure. Choosing the correct instruction depends on whether the transformation requires exact replication of XML data or more flexible manipulation of the document structure.