Copying Attributes and Texts

This section provides a tutorial example on how to use the 'copy' element to copy attributes, texts and other information from the source XML document to the result document.

In the previous section, we saw how the "copy" element works when applied to an element in the source XML document. It only copied the element name.

In order to copy an attribute or a text, the "copy" element must be included in a template that applied to the attribute or the text. In fact, the "copy" element can copy any types of nodes from source XML document when applied properly. In another word, the behavior of the "copy" element depends on the type of the current node:

A good example of how to use the "copy" element is this identity template:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- identity.xsl
 - Copyright (c) 2002-2018 HerongYang.com. All Rights Reserved.
-->

<xsl:template match="*|@*|comment()|processing-instruction()|text()">
 <xsl:copy>
  <xsl:apply-templates
   select="*|@*|comment()|processing-instruction()|text()"/>
 </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Note that:

Here is my dictionary.xml XML document:

<?xml version="1.0"?>
<dictionary>
<!-- dictionary.xml
 - Copyright (c) 2002-2018 HerongYang.com. All Rights Reserved.
-->
 <word acronym="true">
  <name>XML</name>
  <definition reference="Herong&apos;s Notes">eXtensible Markup
Language.</definition>
  <update date="2002-12-23"/>
 </word>
 <word symbol="true">
  <name>&lt;</name>
  <definition>Mathematical symbol representing the "less than" logical
operation, like: 1&lt;2.</definition>
  <definition>Reserved symbol in XML representing the beginning of
tags, like: <![CDATA[<p>Hello world!</p>]]>
  </definition>
 </word>
 <word symbol="false" acronym="false">
  <name>extensible</name>
  <definition>Capable of being extended.</definition>
 </word>
</dictionary>

After applying my identify template, you should get:

<?xml version="1.0" encoding="UTF-8"?><dictionary>
<!-- dictionary.xml
 - Copyright (c) 2002-2018 HerongYang.com. All Rights Reserved.
-->
 <word acronym="true">
  <name>XML</name>
  <definition reference="Herong's Notes">eXtensible Markup
Language.</definition>
  <update date="2002-12-23"/>
 </word>
 <word symbol="true">
  <name>&lt;</name>
  <definition>Mathematical symbol representing the "less than" logical
operation, like: 1&lt;2.</definition>
  <definition>Reserved symbol in XML representing the beginning of
tags, like: &lt;p&gt;Hello world!&lt;/p&gt;
  </definition>
 </word>
 <word symbol="false" acronym="false">
  <name>extensible</name>
  <definition>Capable of being extended.</definition>
 </word>
</dictionary>

What do you think about the result? I think that all parts were copied, but some characters got converted to XML entity names. The CDATA protection was also removed.

Table of Contents

 About This Book

 Introduction of XML (eXtensible Markup Language)

 XML File Syntax

 XML File Browsers

 XML-JSON Document Conversion

 DOM (Document Object Model) Programming Interface

 SAX (Simple API for XML) Programming Interface

 DTD (Document Type Definition) Introduction

 Syntaxes of DTD Statements

 Validating an XML Document against the Specified DTD Document Type

 XSD (XML Schema Definition) Introduction

 Syntaxes of XSD Statements

 Validating XML Documents Against Specified XML Schemas

 XSL (Extensible Stylesheet Language) Introduction

 Java Implementation of XSLT

 XSLT (XSL Transformations) Introduction

 XPath (XML Path) Language

 XSLT Elements as Programming Statements

Control and Generate XML Element in the Result

 "output" - The Output Format Control Element

 Generating Transformation Output in XML Format

 "element" and "attribute" - Constructing Output Elements

 "copy" - Copy Elements from Source to Result

Copying Attributes and Texts

 PHP Extensions for XML Manipulation

 Processing XML with Python Scripts

 XML Notepad - XML Editor

 XML Tools Plugin for Notepad++

 XML Plugin Packages for Atom Editor

 XML 1.1 Changes and Parsing Examples

 Archived Tutorials

 References

 Full Version in PDF/EPUB