<xsl:copy> | |
Makes a shallow copy of an element to the result tree. This element only copies the current node and its namespace nodes. The children of the current node and any attributes it has are not copied. | |
Category | |
Instruction |
|
Required Attributes | |
None. |
|
Optional Attributes | |
|
|
Content | |
An XSLT template. |
|
Appears in | |
<xsl:copy> appears in a template. |
|
Defined in | |
XSLT section 7.5, Copying. |
|
Example | |
We'll demonstrate <xsl:copy> with an example that copies an element to the result tree. Notice that we do not specifically request that the attribute nodes of the source document be processed, so the result tree will not contain any attributes. Here is our stylesheet: <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml"/> <xsl:template match="*"> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template> </xsl:stylesheet> We'll test our stylesheet with the following XML document: <?xml version="1.0"?> <report> <title>Miles Flown in 2001</title> <month sequence="01"> <miles-flown>12379</miles-flown> <miles-earned>35215</miles-earned> </month> <month sequence="02"> <miles-flown>32857</miles-flown> <miles-earned>92731</miles-earned> </month> <month sequence="03"> <miles-flown>19920</miles-flown> <miles-earned>76725</miles-earned> </month> <month sequence="04"> <miles-flown>18903</miles-flown> <miles-earned>31781</miles-earned> </month> </report> Here are the results: <?xml version="1.0" encoding="UTF-8"?> <report> <title>Miles Flown in 2001</title> <month> <miles-flown>12379</miles-flown> <miles-earned>35215</miles-earned> </month> <month> <miles-flown>32857</miles-flown> <miles-earned>92731</miles-earned> </month> <month> <miles-flown>19920</miles-flown> <miles-earned>76725</miles-earned> </month> <month> <miles-flown>18903</miles-flown> <miles-earned>31781</miles-earned> </month> </report> The <xsl:copy> does a shallow copy, which gives you more control over the output than the <xsl:copy-of> element does. However, you must explicitly specify any child nodes or attribute nodes you would like copied to the result tree. The <xsl:apply-templates> element selects all text, element, comment, and processing-instruction children of the current element; without this element, the result tree would contain only a single, empty <report> element. Compare this approach with the example in the <xsl:copy-of> element. |