EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign up
Home Software Development Software Development Tutorials XML Tutorial XSLT copy-of

XSLT copy-of

Updated April 18, 2023

XSLT copy-of

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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.

  1. 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.
  2. 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.
  3. 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:

xml 1

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:

xml

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 3

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 –

  1. XML ampersand
  2. XML Mapping
  3. XML File 
  4. XML Element
ADVERTISEMENT
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW