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

QName

Usage

Optional (not together with the validation attribute)

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 type attribute)

Introduction

XSLT 2.0

The validation attribute sets the validation level. When absent, its default is strip unless the xsl:styles­heet 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 value strict enforces 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 resulting validity property in its PSVI must be valid. If it is invalid or notKnown, transformation fails there with a type error (ERR XTTE1510 or ERR XTTE1512).

  • validation="lax"
    The value lax also requires validation against an available schema, but a missing schema or undeclared node is not an error. The node’s validity property may therefore be either valid or notKnown. In the latter case, an unvalidated element node receives type xs:untyped. A copied attribute node’s type remains unchanged. If the validity property is invalid, transformation again fails with a type error (ERR XTTE1515).

  • validation="preserve"
    The value preserve retains a copied attribute or element node’s type association. (Note: Here, xsl:copy-of differs from xsl:copy, which does this only for attributes, not elements.) However, value preserve is allowed for attribute validation only if copying namespace nodes has not been disabled.

  • validation="strip"
    The value strip removes any type association, assigning a copied attribute type xs:untypedAtomic and a copied element type xs:unty­ped. For the latter, its nilled property is always set to false. A copied attribute that is an identifier ID or an IDREFS attribute retains its is-id or is-idrefs property.

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 
/>

   

  

back to top