Share via


<xsl:copy-of> Element 

Inserts subtrees and result tree fragments into the result tree.

<xsl:copy-of
  select = Expression />

Attributes

  • select
    Required. XPath Expressions identifying nodes to be copied. The subtree below each of these nodes is copied to the result tree in its entirety.

Element Information

Number of occurrences

Unlimited

Parent elements

xsl:attribute, xsl:comment, xsl:copy, xsl:element, xsl:fallback, xsl:for-each, xsl:if, xsl:message, xsl:otherwise, xsl:param, xsl:processing-instruction, xsl:template, xsl:variable, xsl:when, xsl:with-param, output elements

Child elements

(No child elements)

Remarks

When the result of evaluating the expression is a result tree fragment, the complete fragment is copied into the result tree. When the result is a node-set, all the nodes in the set are copied in document order into the result tree. Copying an element node copies the attribute nodes, namespace nodes, and children of the element node, as well as the element node itself. A root node is copied by copying its children. When the result is neither a node-set nor a result tree fragment, the result is converted to a string and then inserted into the result tree, as with <xsl:value-of>.

Example

This sample XSLT file operates against the sample XML file to find a <person> element with <given-name> and <family-name> child elements. The paragraph will contain the first <given-name> child element of the current node, including any attributes and child elements, followed by a space and the first <family-name> child element, including any attributes and child elements, of the current node.

XML File (family.xml)

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="family.xsl"?>
<family>
  <person>
    <given-name age="10">
      <name>Fred</name>
      <nick-name>Freddy</nick-name>
    </given-name>
    <family-name>Smith</family-name>
  </person>
  <person>
    <given-name age="10">
      <name>Robert</name>
      <nick-name>Bob</nick-name>
    </given-name>
    <family-name>Smith</family-name>
  </person>
</family>

XSLT File (family.xsl)

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

<xsl:template match="person">
  <p>
    <xsl:copy-of select="given-name"/>
    <xsl:text> </xsl:text>
    <xsl:copy-of select="family-name"/>
  </p>
</xsl:template>

</xsl:stylesheet>

Output

This is the formatted output:

Fred Freddy Smith

Robert Bob Smith

The following is the processor output, with line breaks added for clarity.

<?xml version="1.0"?>

<p><given-name age="10">

<name>Fred</name>

<nick-name>Freddy</nick-name>

</given-name>

<family-name>Smith</family-name></p>

<p><given-name age="10">

<name>Robert</name>

<nick-name>Bob</nick-name>

</given-name>

<family-name>Smith</family-name></p>

See Also

Reference

<xsl:value-of> Element