Copying source structures

When transforming XML documents into different XML documents, there may be times when some markup needs to be copied to the output without modification. Two instructions are available for specifying copy-through of XML constructs.

Copying document fragments

A document to be transformed may contain fragments that are to remain unchanged. The Copy Of element can be used to copy such structures directly to output. The Select attribute contains an expression to locate the fragment to copy-through. To copy the current element (and all its descendants), the expression needs to refer to the current element ('.'). For example, the source document may contain tabular structures that are already coded according to the HTML table standard, and the output document will also contain tables coded to this standard:

<template match="table">
  <copy-of select="."/>
</template>

Other expressions copy-through elements from elsewhere in the source document. For example, to copy to output as a single block all the Title elements in a source document, at the beginning of a book, the following would be needed:

<template match="book">
  <copy-of select="//title"/>
</template>

The fragment copied does not have to be an element. The examples below show how this instruction can be used to copy-through other markup, such as comments and processing instructions.

Copying nodes

A more limited variant of copying-through is available that only copies the current node. If the node is an element, its attributes and its children (text and sub-elements) are not copied. The Copy element represents the current node. For example, the templates to copy comments and processing instructions from source to destination files can be made much simpler than previous examples:

<template match="comment()">
  <copy/>
</template>


<template match="processing-instruction()">
  <copy/>
</template>

This works reliably because these node types have no internal structure, unlike elements. However, even element nodes can be usefully processed in this way.

The fact that the original node type is inferred means that the templates shown above can be combined into a single template. When it matches a comment, it is a comment that is created; when it matches a processing instruction, it is a processing instruction that is created:

<template match="comment()|processing-instruction()">
  <copy/>
</template>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset