XSLT » Elements » xsl:copy

Syntax:
<xsl:copy
  use-attribute-sets="name-list"
>
</xsl:copy>

The xsl:copy element copies the current node in the source document to the output. The copy has the same name, namespace, and type as the original node, but any attributes, children, and other descendants are not copied. (Note that you can apply this element recursively to copy attributes and children.)

The xsl:copy-of element can be used to copy a node set. Children are copied with this element.
 
A common use of this element is to make identity transformations.

Examples

Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/ | @* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-16"?>
<?xml-stylesheet type="text/xsl" href="xslt_example_copy.xsl"?>
<devguru_staff>
   <programmer>
      <name>Bugs Bunny</name>
      <dob>03/21/1970</dob>
      <age>31</age>
      <address>4895 Wabbit Hole Road</address>
      <phone>865-111-1111</phone>
   </programmer>
   <programmer>
      <name>Daisy Duck</name>
      <dob>08/09/1949</dob>
      <age>51</age>
      <address>748 Golden Pond</address>
      <phone>865-222-2222</phone>
   </programmer>
   <programmer>
      <name>Minnie Mouse</name>
      <dob>04/13/1977</dob>
      <age>24</age>
      <address>4064 Cheese Factory Blvd</address>
      <phone>865-333-3333</phone>
   </programmer>
   <programmer>
      <name>Pluto</name>
      <dob>07/04/1979</dob>
      <age>21</age>
      <address>414 Dog Lane</address>
      <phone/>
   </programmer>
   <programmer>
      <name>Porky Pig</name>
      <dob>11/30/1956</dob>
      <age>44</age>
      <address>555 Mud Pit Pike</address>
      <phone>865-444-4444</phone>
   </programmer>
   <programmer>
      <name>Road Runner</name>
      <dob>01/19/1953</dob>
      <age>48</age>
      <address>135 Desert View Street</address>
      <phone>865-555-5555</phone>
   </programmer>
</devguru_staff>
Explanation:

For our example, we display the standard code for making an identity transformation. This is the code for xslt_example_copy.xsl.
 
We use the DevGuru Staff List XML file for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_copy.xsl"?>

Language(s): XSLT

See Also: