Abbey Workshop

XSLT: Copying a Portion or All of an XML File

How do you copy all of or a portion of an XML document exactly? The following code snippet shows how to do that exactly. Using this template for an entire document or for a portion of a document, will copy the contents of a particular node exactly. This can be very useful if you want to leave a large portion of a document unchanged.

Listing for: copy.xsl

   1 <xsl:stylesheet xmlns:xsl="" version="1.0">
   2 <xsl:output method="xml"/>
   4 <!-- copy all elements and their attributes-->
   5 <xsl:template match="* | @*">
   6 <xsl:copy><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:copy>
   7 </xsl:template>
   9 </xsl:stylesheet>

The xsl:copy command gets each element while the xsl:copy-of is used to get each attribute. Everything is copied including namespaces.