"copy" - Copy Elements from Source to Result

This section provides a tutorial example on how to generate transformation output in XML format. The third approach is to copy elements using XSLT 'copy' elements in the transformation template to copy element from the source XML document to the result document.

The third way to generate output XML elements is to use "copy" elements to copy elements from the source XML document to the output document.

"copy": A child element used in the content of a "template" element. A "copy" element serves an output statement, which will be evaluated to copy the current element into the result document without its attributes and content.

The syntax of the "copy" element is shown in 2 examples below:



where "content" is optional. If provided, "content" should be a list of "apply-templates" elements.

Let's see an example of using "copy" elements to copy element from the source XML document to the result document.

Here is the source XML document, output_xml.xml:

<?xml version="1.0"?>
<user name="Herong">
 Administrator can delete users.
 Points are earned by posting messages.

Here is the transformation template that copies some elements from the source XML document:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
<xsl:output method="xml"/>
<xsl:template match="user">
  <xsl:apply-templates select="role"/>
  <xsl:apply-templates select="point"/>
<xsl:template match="role">
<xsl:template match="point">

Run XSLTransformer.java to perform the transformation, you should get:

<?xml version="1.0" encoding="UTF-8" ?>
 <role />
 <point />

The output looks good. All 3 XML elements are copy in the result XML document correctly. But only element names are copied over. Attributes and text contents are not copied.

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


 Full Version in PDF/EPUB