XML Tutorials - Herong's Tutorial Examples - v5.25, by Herong Yang
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's Notes">eXtensible Markup Language.</definition> <update date="2002-12-23"/> </word> <word symbol="true"> <name><</name> <definition>Mathematical symbol representing the "less than" logical operation, like: 1<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><</name> <definition>Mathematical symbol representing the "less than" logical operation, like: 1<2.</definition> <definition>Reserved symbol in XML representing the beginning of tags, like: <p>Hello world!</p> </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
Introduction of XML (eXtensible Markup Language)
DOM (Document Object Model) Programming Interface
SAX (Simple API for XML) Programming Interface
DTD (Document Type Definition) Introduction
Validating an XML Document against the Specified DTD Document Type
XSD (XML Schema Definition) Introduction
Validating XML Documents Against Specified XML Schemas
XSL (Extensible Stylesheet Language) Introduction
XSLT (XSL Transformations) Introduction
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
PHP Extensions for XML Manipulation
Processing XML with Python Scripts
XML Tools Plugin for Notepad++
XML Plugin Packages for Atom Editor