The copy-of element goes further than its counterpart copy. Where copy by itself only copies element nodes and their associated namespace nodes, copy-of copies element and namespace nodes, plus attribute nodes and children. This is called a deep copy. The stylesheet copy-of.xsl demonstrates the difference between a shallow copy and a deep copy (note bold): <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml"/> <xsl:template match="eu"> <xsl:apply-templates select="member"/> </xsl:template> <xsl:template match="member"> <xsl:apply-templates select="state[2]"/> </xsl:template> <xsl:template match="state"> <xsl:copy-of select="."/> </xsl:template> </xsl:stylesheet> The only difference between copy.xsl and copy-of.xsl is that copy.xsl uses copy with no attributes, and copy-of.xsl uses copy-of with a select attribute. You will see the real evidence when you process eu.xml against copy-of.xsl: xalan eu.xml copy-of.xsl This produces: <?xml version="1.0" encoding="UTF-8"?> <state founding="yes">Belgium</state> Instead of just copying the element node as with copy, copy-of copies the element node state, its attribute founding with its value, and its text node child Belgium. The copy-of element can also copy other kinds of child nodes. The stylesheet candidate.xsl copies all the state children of candidate in eu.xml with the deep copy method using copy-of, and it grabs the eu and candidate elements using a shallow copy method with copy: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="eu"> <xsl:copy> <xsl:apply-templates select="candidate"/> </xsl:copy> </xsl:template> <xsl:template match="candidate"> <xsl:copy> <xsl:copy-of select="state"/> </xsl:copy> </xsl:template> </xsl:stylesheet> Apply the stylesheet using this line: xalan -i 1 eu.xml candidate.xsl and you will get the following output: <?xml version="1.0" encoding="UTF-8"?> <eu> <candidate> <state>Bulgaria</state> <state>Cyprus</state> <state>Czech Republic</state> <state>Estonia</state> <state>Hungary</state> <state>Latvia</state> <state>Lithuania</state> <state>Malta</state> <state>Poland</state> <state>Romania</state> <state>Slovakia</state> <state>Slovenia</state> <state>Turkey</state> </candidate> </eu> This creates a new document that contains only the names of the 13 European state candidates. |