The XSLT <xsl:copy-of> element makes a deep copy of an XML node as defined by an expression in the select attribute.
The expression defined in the mandatory select attribute is either an XPATH expression (for nodes) or a variable reference pointing to a node or node set.
The <xsl:copy-of> elements inserts the node(s) identified by the select attribute into the output document. The function makes a deep copy of the identified node(s) – meaning that the selected nodes are copied; including all the node’s children/descendants, attributes, and namespaces.
Use the <xsl:copy> function if a shallow copy is required; that is – only the node is copied – excluding all children, attributes and descendants.
|select||MANDATORY||an XPATH expression or variable reference.|
For our examples we are using the fictional library as an input XML. Any XSL transform examples we define uses this XML as an input.
How to copy nodes between XML documents
The following XSL transform shows how to copy a node (including its children/descendants and attributes) from the source XML document to the target XML document. The purpose of the transform is to select the cats category and copy it and all its book children to the output XML.
The output XML with copied nodes looks as follows: