XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / copy
(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
Instruction xsl:copy copies a node to the result sequence used for serialization, effectively the result document. This is shallow copying: element children and attributes are not copied automatically, but namespace nodes are.
Classification | Instruction |
Function group | Copying from the source document |
Introduced in | XSLT 1.0 |
Stylesheet position and permitted contents:
xsl:copy is allowed only inside elements xsl:template.
Usually empty, the instruction may contain a sequence-constructor template body. This runs only when an element or document node is copied.
Attributes:
For xsl:copy, standard attributes apply. It also has optional attribute use-attribute-sets and, since XSLT 2.0, optional attributes copy-namespaces, inherit-namespaces, type and validation.
copy-namespaces
Value | "yes” (default) | "no” |
Usage | Optional |
Introduced in | XSLT 2.0 |
Attribute copy-namespaces applies only to copied elements. Values yes or no control whether namespaces are copied too. Without it, the default is yes. Normally namespaces are copied and inherited by children created by the constructor inside instruction xsl:copy; see attribute inherit-namespaces.
Note: This attribute does not exist in XSLT 1.0.
inherit-namespaces
Value | "yes" (default) | "no" |
Usage | Optional |
Introduced in | XSLT 2.0 |
Attribute inherit-namespaces applies only to copied elements. Values yes or no control inheritance of copied namespaces by children created by the constructor inside xsl:copy. Without it, the default is yes, so namespaces are normally inherited, provided their copying was not disabled.
Note: This attribute does not exist in XSLT 1.0.
type
Value | |
Usage | Optional, mutually exclusive with attribute |
Introduced in | XSLT 2.0 |
Attribute type names a schema datatype for copied elements and attributes. It may be predefined (built-in) or imported. Missing declarations are static errors (ERR XTSE1520). Successful validation, meaning property validity of the corresponding PSVI equals valid, attaches the datatype. Otherwise stylesheet processing stops (ERR XTTE1540).
Copied attributes cannot receive a complex type: attributes cannot have complex types, so this is static error ERR XTSE1530.
Note: Attribute type cannot coexist with attribute validation and is unavailable in XSLT 1.0.
use-attribute-sets
Value | QNames |
Usage | Optional |
Introduced in | XSLT 1.0 |
Attribute use-attribute-sets attaches named attribute sets to the copied element. List their QNames separated by whitespace.
validation
Value | "strict" | "lax" | "preserve" | "strip" |
Usage | Optional, mutually exclusive with attribute |
Introduced in | XSLT 2.0 |
Attribute validation sets validation level, defaulting to strip unless element xsl:stylesheet has attribute default-validation with another value. For a copied item other than element or attribute, attribute validation is ignored.
Note: Attribute validation cannot coexist with attribute type and is unavailable in XSLT 1.0.
The four values are strict, lax, preserve and strip, whose effects for xsl:copy are:
validation="strict"
Valuestrictforces strict schema validation. The node must be declared in a schema available through standard methods of XML Schema. Propertyvalidityin its PSVI must equalvalid. ValuesinvalidornotKnowncause a type error (ERR XTTE1510orERR XTTE1512).
validation="lax"
Valuelaxalso validates against an available schema, but a missing schema or declaration is not an error. Propertyvaliditymay bevalidornotKnown. For the latter, an unvalidated copied element receives typexs:untyped; copied attributes retain their types. If propertyvalidityisinvalid, a type error stops transformation (ERR XTTE1515).
validation="preserve"
Valuepreservepreserves copied attribute types, but assigns copied elements general typexs:anyType. Their content may not be copied and may thus change. Copied content can nevertheless retain its type.
validation="strip"
Valuestripdisables validation and removes existing type bindings. Copied attributes receivexs:untypedAtomicand copied elements receivexs:untyped. For elements, propertynilledis always set tofalse. A copied attribute that is an identifierIDor an attribute of typeIDREFSretains its propertyis-idoris-idrefsrespectively.
Purpose:
Instruction xsl:copy copies the current item into the result sequence, serialized as the result document. It applies to all node types and atomic values.
Atomic values are copied unchanged. A node’s children are not automatically included; use xsl:copy-of instead to include them.
Optional content of instruction xsl:copy creates children of the copied node as a sequence constructor (sequence constructor). It runs only for types permitting children, namely element and document nodes. Copied namespace nodes and attribute sets precede constructor-generated nodes: they appear before any nodes generated by the constructor.
The body often uses instruction xsl:apply-templates to process copied elements’ children. For atomic values, attributes, text or comments that cannot accept children, the body is ignored without error.
Namespaces attached to copied elements or attributes are copied automatically unless attribute copy-namespaces says otherwise. Conversely, when xsl:copy inserts an element in the null namespace as another element’s child, the parent’s nonempty default namespace is not transferred automatically. Serialization may add a namespace undeclaration with xmlns="" on the copied element.
Applying xsl:copy to element nodes:
Serialization creates the element copy with namespaces. A constructor inside xsl:copy runs and appends its output as children. Children inherit copied namespaces unless attribute inherit-namespaces is set to no.
When xsl:copy has attribute use-attribute-sets, the listed attribute sets apply to the copied element. Attributes type or validation validate the element accordingly.
Since XSLT 2.0, setting the new attribute copy-namespaces to no prevents automatic namespace copying. Inside, instruction xsl:namespace can attach a new namespace, including a default namespace valid in the result.
Applying xsl:copy to document nodes:
xsl:copy also works on document nodes, such as a variable’s temporary tree. It adds only the contents, not the document node itself, to the primary result sequence. A constructor inside xsl:copy runs, treating the document like an element here.
Attributes type or validation validate the copied document. Thus an existing temporary tree can be validated while copying. Validating a temporary tree during its creation is impossible because xsl:variable lacks these attributes. Instruction xsl:document would permit validation of a created tree, however.
An attribute on xsl:copy named use-attribut-sets is ignored for documents, which cannot have attributes.
Applying xsl:copy to attributes:
Applied to attributes, xsl:copy copies the attribute and value, with namespace fixup as needed. At serialization an open target element must accept it. An existing same-named attribute is overwritten.
A copied attribute is validated through attribute type or validation, but its constructor is ignored. Attribute sets cannot apply here either; attribute use-attribute-sets is ignored.
Applying xsl:copy to text nodes:
Text is copied to the result, equivalent to xsl:value-of or xsl:apply-templates.
Attribute sets on xsl:copy are ignored, as are attributes type or validation and any constructor.
Applying xsl:copy to other nodes:
Namespaces accompany copied elements and need no explicit xsl:copy. Comments and processing instructions are also copied when xsl:copy applies. Built-in template rules ignore these nodes, so explicit rules must use xsl:copy, as in example 5.
These nodes also ignore constructors and attributes use-attribute-sets, type or validation.
Other cases:
For an atomic value rather than a node, xsl:copy passes it unchanged. A constructor inside xsl:copy does not run.
Examples:
Example 1 – Copying elements:
<xsl:template match="a | b | c">
<xsl:copy>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:template>
This copies elements <a>, <b> or <c> and, using instruction xsl:value-of, their string values.
Example 2 – Copying elements with attribute sets:
<xsl:template match="a | b | c">
<xsl:copy use-attribute-sets="set1 set2 set3"/>
</xsl:template>
This copies elements <a>, <b> or <c> without content. Copies receive attributes from the three listed sets.
Example 3 – Copying all attributes:
<xsl:template match="a | b | c">
<xsl:copy>
<xsl:for-each select="@*">
<xsl:copy/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
The outer instruction xsl:copy copies elements <a>, <b> or <c>, and a loop xsl:for-each with another instruction xsl:copy copies every attribute.
Example 4 – Deep one-to-one copy:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
Recursive use of xsl:copy allows deep copying. This template uses xsl:copy to copy child and attribute axes, then recurses to the next level. Specifying exactly which nodes to process inside xsl:copy gives more control than general copying with xsl:copy-of.
Example 5 – Copying comments and processing instructions:
<xsl:template match="comment()">
<xsl:copy/>
</xsl:template>
<xsl:template match="processing-instruction()">
<xsl:copy/>
</xsl:template>
Instruction xsl:copy copies comments and processing instructions, overriding default template rules. These nodes have no child structure, making xsl:copy appropriate.
Element definition:
XSLT 1.0:
<!-- Category: instruction -->
<xsl:copy
use-attribute-sets = qnames >
<!-- Content: template -->
</xsl:copy>
XSLT 2.0:
<!-- Category: instruction -->
<xsl:copy
copy-namespaces? = "yes" | "no"
inherit-namespaces? = "yes" | "no"
type? = qname
use-attribute-sets? = qnames
validation? = "strict" | "lax" | "preserve" | "strip">
<!-- Content: sequence-constructor -->
</xsl:copy>
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