When an XML document is converted to other form or another copy of the document is made, XSLT is used to copy the elements of the source document. If entire document has to be copied with all the elements, child elements, attributes, etc, a deep copy is made using <xsl:copy-of> element. If a partial document with selected elements, attributes, etc, has to be copied to another document, shallow copy  is done by using <xsl:copy> element.

Deep copy

When an exact copy of elements, attributes and child nodes has to be made, <xsl:copy-of> is used. Consider an XML file given below:

Example of deep copy with XSLT

Copy.xml

<?xml version="1.0" encoding="UTF-8"?>

<?xml-stylesheet type="text/xsl" href="copy.xsl"?>

<root>

    <cinema id="1">

    <film>Predestination</film>

    <director>Spierig Brothers</director>

    <year>2014</year>

    </cinema>

   

    <cinema id="2">

    <film>Inception</film>

    <director>Christopher Nolan</director>

    <year>2010</year>

    </cinema>

</root>

Using “@*|node()”  all elements and attributes and child nodes is copied to another document as given below:

Copy.xsl

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

<xsl:output method="xml" encoding="utf-8" indent="no"/>                              

<xsl:template match="/">

       <xsl:copy-of select="@*|node()"/>

</xsl:template>                                   

</xsl:stylesheet>                             

Output of this XSLT transformation is

<?xml-stylesheet type="text/xsl" href="copy.xsl">

<root>

    <cinema id="1">

    <film>Predestination</film>

    <director>Spierig Brothers</director>

    <year>2014</year>

    </cinema>

   

    <cinema id="2">

    <film>Inception</film>

    <director>Christopher Nolan</director>

    <year>2010</year>

    </cinema>  

</root>

Shallow copy

If there is a requirement to copy only an element and not it’s child element, attribute ..etc, a shallow copy has to be used. It is done by using <xsl:copy>. It copies only one element at a time. It gives more control to the programmer to write a template to copy the nodes. The template should be written such that it matches each attribute, element, child nodes and copies the required node only. This is recursively done to all nodes in the source tree.

The example below copies only the film name and id of the xml file using shallow copy:

Example of shallow copy with XSLT

Copy.xml

<?xml version="1.0" encoding="UTF-8"?>

<?xml-stylesheet type="text/xsl" href="copy.xsl"?>

<root>

    <cinema id="1">

    <film>Predestination</film>

    <director>Spierig Brothers</director>

    <year>2014</year>

    </cinema>

   

    <cinema id="2">

    <film>Inception</film>

    <director>Christopher Nolan</director>

    <year>2010</year>

    </cinema>  

</root>

The template is defined to match all attributes and elements using “@*|*”. It is applied to the “cinema” node and value of attribute “id” and name of the film, “film” is written in output document.

Copy.xsl

<?xml version="1.0" encoding="UTF-8"?>

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

   <xsl:template match="@*|*">

        <xsl:apply-templates/>

   </xsl:template>    

    <xsl:template match="cinema">

        <xsl:copy>

            <xsl:value-of select="@id"/>

            <xsl:text>&#160;</xsl:text>

            <xsl:value-of select="film"/>

        </xsl:copy>

    </xsl:template>

</xsl:stylesheet>

The generated output document gives below information:

<?xml version="1.0"?>

<cinema>1 Predestination</cinema>

<cinema>2 Inception</cinema>

 

›› go to examples ››