XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round two / Copying nodes: xsl:copy and xsl:copy-of

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 2.)

Transforming XML often requires copying the current node, an element or another kind, unchanged into the result, much like literal result elements in the stylesheet.

A shallow copy copies only the node, not content or attributes; a deep copy includes its entire document branch. The first uses xsl:copy, the second xsl:copy-of.

xsl:copy – Shallow copying

The instruction xsl:copy performs a shallow copy (shallow copy). Without a select attribute, it copies only the current item, excluding children or text when this is an element. Attributes are excluded too, since they are not children.

<xsl:copy/>

Often empty, the element can also contain a template-block when element contents must also be copied. In that case, xsl:apply-templates enables child processing:

<xsl:template match="*">
  <xsl:copy>
    <xsl:apply-templates/> 
  </xsl:copy>
</xsl:template>

This rule copies any named element into the result using wildcard name test *, also constructing a sequence of children for processing. Attributes are excluded because xsl:apply-templates does not itself use the attribute axis.

A more specific rule can strip attributes from a selected element type. Meanwhile, xsl:copy can bind a new attribute set to that element:

<xsl:template match="beispiel">
  <xsl:copy use-attribute-sets="beispiel-set">
    <xsl:apply-templates/> 
  </xsl:copy>
</xsl:template>

An element <beispiel> is copied and given new attributes.

With an appropriate XPath expression, the instruction can also copy the current node’s attributes. During serialisation, an open element node must be available to receive them.

This template combination achieves that:

<xsl:template match="*">
  <ergebnis>
    <xsl:apply-templates select="@* | node()"/>
  </ergebnis>
</xsl:template>
<xsl:template match="@*">
  <xsl:copy/>
</xsl:template>

In the result document:

...
<ergebnis kopiertes-attribut="kopierter Wert">
  ... Inhalte ...
</ergebnis>
...

The open element is always <ergebnis> because it is written to the result before switching to attribute-copying template match="@*"; after return, the node closes. The select attribute value "@*|node()" in xsl:apply-tem­plates enables all attributes @* and all children node(), including elements, text and comments, to be processed.

For elements without a higher-ranking rule, this uses template rule match="*"; otherwise default templates apply. Essentially, all source elements without a specific rule are renamed <ergebnis> while preserving original attribute names and values.

Advantages and drawbacks of xsl:copy

The instruction xsl:copy copies the current node with its name. Content and attributes can also be processed, but require explicit instructions or recursion over children.

Recursion can be cumbersome, but gives the best control over what reaches the result.

Entire branches are copied more easily, though less selectively, with xsl:copy-of.

xsl:copy-of – Deep copying

The instruction xsl:copy-of copies a whole source branch starting at an element. It can copy any sequence, including element content, attributes and namespaces. Since all descendants are included, this is a deep copy (deep copy).

Its contents cannot be selected individually: everything is copied. Only namespace nodes can be excluded with attribute copy-namespaces="no" (default: "yes").

The required select attribute specifies the sequence or branch root through XPath. It may be "." for the current node, copying it with every descendant. This is useful when entire structures (subtrees) must be copied repeatedly to the result.

<xsl:copy-of select="XPath-Ausdruck"/>

To copy all <buch> elements of a <regal> element unchanged, write:

<xsl:template match="regal">
  <kiste>
    <xsl:copy-of select="buch"/>
  </kiste>
</xsl:template>

Every book moves unchanged, in the same order, from the XML shelf to the XML box. XSLT is an ideal removal service for data.

Schema-aware processing will benefit from validating a tree copied with xsl:copy-of . Attribute type can supply a sequence type for comparison or a schema type from an available declaration. Alternatively, use the validation attribute.

   

back to top