XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / copy-of
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
The instruction xsl:copy-of copies source subtrees (deep copy) starting at a node designated by its select attribute.
Classification | Instruction |
Function group | Copying from the source document |
Introduction | XSLT 1.0 |
Position in the stylesheet and permitted contents:
xsl:copy-of occurs only inside template bodies and is always empty.
Attributes:
The standard attributes apply to xsl:copy-of. In both XSLT 1.0 and 2.0 it also has a required attribute select. XSLT 2.0 additionally provides attributes copy-namespaces and type or validation.
copy-namespaces
Value | "yes" (default) | "no" |
Usage | Optional |
Introduction | XSLT 2.0 |
The attribute copy-namespaces accepts yes or no. The default is yes. It determines whether the namespaces within the subtree should also be copied.
Note: This attribute does not exist in XSLT 1.0.
select
Value | xpath-expression |
Usage | Required |
Introduction | XSLT 1.0 |
The select attribute uses an XPath expression to select a sequence of root nodes of subtrees, usually in the source document, to copy into the result sequence. The generated sequence may also contain atomic values.
type
Value | |
Usage | Optional (not together with the |
Introduction | XSLT 2.0 |
The type attribute names the QName of a schema type to associate with copied element and attribute nodes. It can be a predefined (built-in) type or one from an imported schema. A missing declaration of that name is a static error (ERR XTSE1520). If validation succeeds, meaning the validity property of the resulting PSVI has value valid, the type is associated with the node. Otherwise, stylesheet processing stops (ERR XTTE1540). Only nodes directly selected by the select attribute are affected, not copied children of elements.
Note: The type attribute cannot be used together with the validation attribute. It does not exist in XSLT 1.0.
validation
Value | "strict" | "lax" | "preserve" | "strip" |
Usage | Optional (not together with the |
Introduction | XSLT 2.0 |
The validation attribute sets the validation level. When absent, its default is strip unless the xsl:stylesheet element has a default-validation attribute with another value. If the copied sequence item is not an element or attribute node, the validation attribute is ignored.
Note: The validation attribute cannot be used together with the type attribute. It does not exist in XSLT 1.0.
The four possible explicit values are strict, lax, preserve and strip. For xsl:copy-of they have these effects:
validation="strict"
The valuestrictenforces strict schema validation of the copied node and its content. The node must be declared in a schema available there through the usual XML Schema mechanisms. The node’s resultingvalidityproperty in its PSVI must bevalid. If it isinvalidornotKnown, transformation fails there with a type error (ERR XTTE1510orERR XTTE1512).
validation="lax"
The valuelaxalso requires validation against an available schema, but a missing schema or undeclared node is not an error. The node’svalidityproperty may therefore be eithervalidornotKnown. In the latter case, an unvalidated element node receives typexs:untyped. A copied attribute node’s type remains unchanged. If thevalidityproperty isinvalid, transformation again fails with a type error (ERR XTTE1515).
validation="preserve"
The valuepreserveretains a copied attribute or element node’s type association. (Note: Here,xsl:copy-ofdiffers fromxsl:copy, which does this only for attributes, not elements.) However, valuepreserveis allowed for attributevalidationonly if copying namespace nodes has not been disabled.
validation="strip"
The valuestripremoves any type association, assigning a copied attribute typexs:untypedAtomicand a copied element typexs:untyped. For the latter, itsnilledproperty is always set tofalse. A copied attribute that is an identifierIDor anIDREFSattribute retains itsis-idoris-idrefsproperty.
Purpose:
Use instruction xsl:copy-of to copy a node selected by the select attribute, usually from the source, unchanged with its attributes, namespaces and all its descendants. This includes all element, attribute, comment, PI and text nodes of the subtree, with their namespaces: a deep copy (deep copy).
Since XSLT 2.0, an appropriate copy-namespaces="no" attribute can prevent copying namespaces. Also, the select expression can now return a sequence containing non-nodes, or consisting solely of them.
Applied to an element node, the instruction creates a copy with the same QName in the result sequence. Attributes and content are included as a deep copy.
Applied to a document node, it adds it and its children to the result sequence. During serialization, the document node is replaced by its children, embedding the copied document, potentially including its root element, as a branch of the result.
Applied to other node kinds and atomic values, xsl:copy-of behaves exactly like xsl:copy.
Examples:
Example 1 – Deep copy of a subtree:
<xsl:template match="zweig">
<xsl:copy-of select="."/>
</xsl:template>
This template copies element <zweig> and all its descendants unchanged into the result document.
Example 2 – Copying from the entire source:
<xsl:template match="buchliste">
<titelliste>
<xsl:copy-of select="//titel"/>
</titelliste>
<!-- weitere Instruktionen -->
</xsl:template>
When processing a book list, this template first copies a list of all <titel> elements in the document.
Example 3 – Repeating output fragments:
Subtrees need not be in the source document. Like reusable text fragments, they can be defined in the stylesheet itself.
Source document:
<buecher>
<buch titel="Einstieg in XML"/>
<buch titel="Einstieg in XSLT"/>
</buecher>
Stylesheet document:
<!-- Teilzweig als Variable -->
<xsl:variable name="verlagsdaten">
<verlag>
<name>Galileo Verlag</name>
<ort>Bonn</ort>
</verlag>
</xsl:variable>
<xsl:template match="/">
<verlagsprogramm>
<xsl:apply-templates/>
</verlagsprogramm>
</xsl:template>
<xsl:template match="buch">
<buch>
<titel><xsl:value-of select="@titel"/></titel>
<xsl:copy-of select="$verlagsdaten"/>
</buch>
</xsl:template>
Serialized result document:
<verlagsprogramm>
<buch>
<titel>Einstieg in XML</titel>
<verlag>
<name>Galileo Verlag</name>
<ort>Bonn</ort>
</verlag>
</buch>
<buch>
<titel>Einstieg in XSLT</titel>
<verlag>
<name>Galileo Verlag</name>
<ort>Bonn</ort>
</verlag>
</buch>
</verlagsprogramm>
A source-code section that needs repeating can be stored in a variable and copied in full into the result when needed using xsl:copy-of.
Element definition:
XSLT 1.0:
<!-- Category: instruction -->
<xsl:copy-of
select = expression
/>
XSLT 2.0:
<!-- Category: instruction -->
<xsl:copy-of
select = expression
copy-namespaces? = "yes" | "no"
validation? = "strict" | "lax" | "preserve" | "strip"
type? = qname
/>
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn