Updated April 18, 2023
Definition of XSLT copy-of Function
XSLT copy-of function is defined to make a deep copy-of XML node expressed in a select attribute and XSLT 3.0 introduces the function. This function is used exclusively if the current node is to be copied excluding all the child nodes and the attributes defined in the document. While displaying an expression it doesn’t convert them to string in all the situations. This function exactly works like xsl:value-of element in the concern of value is been converted to string in the outcome.
Syntax:
<xsl:copy-of select=" rootname/child" />
Here the select attribute says an expression to copy the node path. This instruction appears inside a template. The next thing is they are self-closing elements without any content inside them.
Following is the working of copy-of function and see how the elements are used.
How copy-of function works in XSLT?
So far, we have seen how to generate output in the sequence, but sometimes it is often required to generate the element in the source as such without any modification. Well, this is accomplished with the copy-of statement.
The properties of copy-of depend on the type of data type the path expression returns.
- If the final result is of the data type Boolean, string, or numeric the XSL:copy-of outputs them in a text node. Therefore, it behaves the same as for xsl:value-of.
- Next, if the expression statement evaluates to a node-set then xsl: copy-of copies all nodes in the order of the tree along with their descendant in the final document.
- If it is a fragment tree then it leads to an unchanged output document.
There is two variety of copy function one is deep copy and the second one is a shallow copy. The deep copy function identifies the selected nodes by coping child nodes, attributes. Using the node () function all the nodes are copied to another document. The latter copies only the node but it excludes children and attributes. And it copies only one at a time and the process is done recursively to all the nodes.
Taking a following sample document like
<firstname>
<content> Making Easy <b> schedule </b> in the plan </content>
</firstname>
So over here we use copy-of in XSL sheet
<xsl:copy-of select="/firstname/content" />
<content> Making Easy <b> schedule </b> in the plan </content>
The given expression is evaluated based on the document type if it is a fragment type the complete fragment is duplicated to the output file. If it is a node-set then the corresponding nodes are copied in the document.
And now let’s say we need only specific elements from the source XML file and we are not caring of other attributes here. The solution is to use XSL: copy means a shallow copy. Therefore, the following template starts like.
<xsl:template match="@*|node()">
<!-- shallow copy- copies only the node.
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
To copy entire source document the following template is used.
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
// copies complete attributes
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Examples
In this section, we shall demonstrate the copy-of instructions with easy stylesheets that copy the source document into the result tree. Let’s Get Started.
Example #1: Simple example copying videos with their movie.
Consider an XML file below for our example
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="copy.xsl"?>
<base>
<video id="1">
<movie>Pirates of Carribean</movie>
<Language>English-UK</Language>
<year>2018</year>
</video>
<video id="2">
<movie>Behind Enemy Lines</movie><Language>German</Language>
<year>2014</year>
</video>
</base>
Taking a stylesheet now
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="no"/>
<xsl:template match="/">
<xsl:copy-of select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
Explanation
The above code is a deep copy format where an exact copy of an element defined is copied to output using <xsl:copy-of> from the source and the output is shown below.
Output:
Example #2
XML file is given as
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="copy.xsl"?>
<Types>
<binary>zero one</binary>
<string>data</string>
<number>5.14</number>
<list-set>
<id>11</id>
<id>12</id>
<id>13</id>
</list-set>
<basetype>
content
<first>
text
<second>
text
<sub-child />
<sub-child />
</second>
<sub-child />
</first>
</basetype>
</Types>
XSL file input is shown as
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable
name="binary"
select="Types/binary='zero'"
/>
<xsl:variable
name="string"
select="string(values/string)"/>
<xsl:variable
name="number"
select="number(Types/number)"/>
<xsl:variable
name="list-set"
select="Types/list-set/*"/>
<xsl:variable name="basetype">
<xsl:copy-of select="values/basetype/*" />
</xsl:variable>
<xsl:text>Value-of binary:</xsl:text>
<xsl:value-of select="$binary" />
<xsl:text>Copy-of binary:</xsl:text>
<xsl:copy-of select="$binary" />
<xsl:text>Value-of string:</xsl:text>
<xsl:value-of select="$string" />
<xsl:text>Copy-of string:</xsl:text>
<xsl:copy-of select="$string" />
<xsl:text>Value-of number:</xsl:text>
<xsl:value-of select="$number" />
<xsl:text>Copy-of number:</xsl:text>
<xsl:copy-of select="$number" />
<xsl:text>Value-of list-set:</xsl:text>
<xsl:value-of select="$list-set" />
<xsl:text>Copy-of list-set:</xsl:text>
<xsl:copy-of select="$list-set" />
<xsl:text>Value-of basetype:</xsl:text>
<xsl:value-of select="$basetype" />
<xsl:text>Copy-of basetype:</xsl:text>
<xsl:copy-of select="$basetype" />
</xsl:template>
</xsl:stylesheet>
Explanation:
Here an attribute selects the elements with descendants and the defined attributes are copied into the final document. A namespace is also copied by default. If the select statements point to the node, the document with descendants is copied into the output. In the above code, select defines the fragment content, then the entire fragment is copied into the result.
Output:
Advantages
- The significance of using the copy function is streaming constraints are not available and the elements are discarded as soon as they processed.
- copy-of element can copy other types of child nodes.
Example #3: Using Banking details
XML
<Bankingdetails>
<bank id="854">
<foreName>George</foreName>
<SurName>Amreal</SurName>
<HomeAddress>Texas</HomeAddress>
</bank>
<bank id="541">
<foreName>Alex</foreName>
<SurName>Houtson</SurName>
<HomeAddress>Sweden</HomeAddress>
</bank>
<bank id="328">
<foreName>Dally</foreName>
<SurName>Furaq</SurName>
<HomeAddress>Australia</HomeAddress>
</bank>
</Bankingdetails>
XSL sheet
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<myoutput>
<xsl:copy-of select="/*/bank[@id=541]" />
</myoutput>
</xsl:template>
</xsl:stylesheet>
Explanation:
The above transformation shows how to copy a root or descendant node from the source XML file to the output XML document. Here it selects the bank element with the id=541 and all its child nodes to the output. Therefore, the copied node looks like follows.
Output:
Example #4
XML file
<?xml version="1.0" encoding="UTF-8"?>
<Machinery>
<product id="1">
<pname>britannica</pname>
<model>bsss</model>
<condition>good</condition>
<vendor>columbia electronics</vendor>
<price>86.95</price>
<seller>India</seller>
</product>
<product id="2">
<pname>coscotco</pname>
<model>cdddd</model>
<condition>medium</condition>
<vendor>china electronics</vendor>
<price>66.95</price>
<seller>Italy</seller>
</product>
<product id="3">
<pname>Escater</pname>
<model>bopph</model>
<condition>bad</condition>
<vendor>Ludhiana</vendor>
<price>60.25</price>
<seller>Pakistan</seller>
</product>
</Machinery>
XSLT file
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:copy-of select="Machinery/product[pname = 'Escater']"/>
</xsl:template>
</xsl:stylesheet>
Explanation:
The above code is the same as the previous example. The example copies an element product attribute with pname. therefore, the complete elements are copied to the output document. conclusion have seen how the copy-of instructions copy the document using select attributes. And therefore, this avoids duplicate modules. As a result, it eliminates the risk of over-written.
Recommended Articles
This is a guide to XSLT copy-of. Here we discuss the definition, How copy-of function works in XSLT? examples with commands respectively. You may also have a look at the following articles to learn more –