Wednesday, February 18, 2026

XSLT copy and copy-of


 

Copying Elements from the Source Document to the Resultant Document

 <xsl:copy> and <xsl:copy-of> elements enable you to copy information from the source document to the resultant document. 

You can also use the <xsl:value-of> element to copy specific values in an XML element from a source document to a resultant document.

 <xsl:copy> Element

<xsl:copy> element copies a specific node from the source document to a resultant document.
This element copies only the current node and its namespace to the resultant document. 
The <xsl:copy> element does not copy child nodes, descendants, or attribute nodes of the current node. This method of copying is called shallow copying.

syntax for the <xsl:copy> element is:

<xsl:copy copy-namespaces = "yes" | "no" use-attribute-sets = qnames validation = "strict" | "lax" | "preserve" | "strip" type = qname> <!-- Content: sequence-constructor --> </xsl:copy>

In the above syntax:
  • copy-namespaces: Copies the namespace of the element in the source document to the resultant document. If the value of this attribute is yes, the namespace is copied to the resultant document. If this attribute has the value no, the namespace is not copied into the resultant document. The default value is yes.

  • use-attribute-sets: Copies attribute-sets while copying the element node to a resultant document. The qnames in the syntax are names of attribute-sets separated by spaces.

  • type, validation: Control the type annotation applied to individual elements and attribute nodes when a new node is created in the resultant document. The validation attribute has the following values:

  • strip: Indicates the type annotation, xs:anyType, if the new node created is an element node. Indicates the type annotation, xdt:unTypedAtomic, if the new node created is an attribute node. This attribute does not validate against the schema definition for the type annotation in the element and the attribute node.

  • preserve: Enables the newly constructed node in the resultant document to retain the type annotation as specified in the source document. The preserve value behaves differently depending on the type of the node created in the result document. For example, if the new node is an element node, the type annotation of the newly created node is xs:anyType. The nodes within the element retain the type annotation as specified in the source document. If the new node is an attribute node, the copied attribute retains the type annotation specified in the source document.

  • strict: Applies the type annotation to newly created nodes by validating the element or attribute nodes with the relevant schema definition declared in the source document. If the element or attribute node is not valid, transformation fails.

  • lax: Applies the type annotation to newly created nodes by validating the element or attribute nodes with the relevant schema definition declared in the source document. If the element or attribute is not valid, the type annotation for an element node is xs:anyType and for an attribute node is xdt:untypedAtomic. 

  • Note

    You need to specify the value of the version parameter as 2.0 because the copy-namespaces attribute is supported only in XSLT 2.0.

Functions of the <xsl:copy> Element with Nodes

Types of Nodes

Function of the <xsl:copy> Element

Root node

Creates an empty resultant document if the current node of the source document is the root node. The use-attribute-sets attribute of the <xsl:copy> element is ignored.

Element node

Copies the element and the namespace of the corresponding element to the resultant document. This element does not copy child or descendant nodes. The use-attribute-sets attribute contains the list of attribute names added as the attribute of the new element node.

Text node

Copies the content of the <xsl:text> element to the resultant document as displayed in the source document. The use-attribute-sets attribute of <xsl:copy> is ignored.

Attribute node

Adds an attribute to the resultant document with the same name as that of the source document. If the <xsl:copy> element does not find an open element node, an error occurs. The use-attribute-sets attribute of the <xsl:copy> element is ignored.

Processing Instruction node

Copies the node name and its value from the source document to the resultant document.

Comment node

Copies the content of the comment node and the node name from the source document to the resultant document.

Namespace node

Copies the namespace from the source document to the resultant document. To copy the namespace, the <xsl:copy> element needs to find an open element node.


 <xsl:copy-of> Element

The <xsl:copy-of> element copies all nodes of the node-set in the source document to the resultant document. 
While copying, the descendant and child nodes are also copied to the resultant document. This method of copying is known as deep copying.

The syntax for the <xsl:copy-of> element is:

<xsl:copy-of select = expression copy-namespaces = "yes" | "no" validation = "strict" | "lax" | "preserve" | "strip" type = qname />

In the above syntax:

  • select: Retrieves all nodes of the node-set and copies the nodes into the resultant document if the value of the expression is a node-set.

  • copy-namespaces: Copies the namespace to the resultant document if the copy-namespace attribute is set to yes. If you specify the value of this attribute as no, the namespace is not copied. Only XSLT 2.0 or later versions support this attribute.

  • validation: Is similar to the validation attribute of the <xsl:copy> element for all values, except the preserve value.

  • type: Enables a similar type annotation as that of the source document in the resultant document.

 



No comments:

Post a Comment

SOA Overview Part-1

  Middleware It provides a mechanism for the process to interact with other processes running on multiple network machines. Advantages...