XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (148 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
11.23Mb size Format: txt, pdf, ePub


which does nothing when a

element is found. Because it does nothing, nothing is written to the result tree, so the

element is effectively deleted. There are many variations on this theme—whenever you want to copy most of the input document to the result, but making a few changes as you go, you can do it by writing a stylesheet that uses the identity template for elements that are to be copied unchanged, supplemented by more specific templates that match the nodes you want to handle specially.

A variant of the identity template is often used which copies attributes unconditionally rather than applying templates to them. Here it is:


   

      

      

   


Examples

The following template rule is useful if the source document contains HTML-like tables that are to be copied directly to the output, without change to the structure.


   

      

      

   


The effect is that all of these elements are copied to the output destination, along with their attributes, but their child elements are processed using whatever template rule is appropriate, which might be the same one in the case of a child element that is part of the table model, or it might be a different template for some other element.

The following template rule matches any elements in the source document that are in the SVG namespace, and copies them unchanged to the output, along with their attributes. The SVG namespace node itself will also be included automatically in the output tree. (SVG stands for Scalable Vector Graphics, which is an XML-based standard for representing diagrams.)

              xmlns:svg=“http://www.w3.org/2000/svg”>

   

      

      

   


This example uses
copy-namespaces=“no”
to avoid copying the namespace nodes attached to the SVG elements. It is safe to do this because SVG elements and attributes do not include data that depends on in-scope namespaces. The namespaces used in the element and attribute names will automatically acquire namespace nodes as a result of the namespace fixup process described on page 310. Getting rid of extraneous namespace nodes in this way can be useful. For example, if the SVG document is embedded in an XHTML document, and the purpose of the copy operation is to make it into a freestanding SVG document, then the default value
copy-namespaces=“yes”
would mean that the freestanding SVG document would contain an unwanted reference to the XHTML namespace.

The option
inherit-namespaces=“no”
is useful only when generating XML 1.1 output. XML 1.1 allows namespace undeclarations of the form
xmlns:p=“”
, which can be useful to indicate that a namespace used in the envelope of a message (for example, a SOAP envelope) is not required in the body of the message. The serializer will generate such a namespace undeclaration on a child element if two conditions are satisified: firstly, there must be a namespace node on the parent element that is not present on the child, and secondly the serialization parameters
version=“1.1”
and
undeclare-prefixes=“yes”
must both be set. To meet the first requirement, you need to specify
inherit-namespaces=“no”
when generating the
parent
element.

Other books

The Advent Killer by Alastair Gunn
The Night the Sky Fell by Stephen Levy
Finding Arthur by Adam Ardrey
The Orchid House by Lucinda Riley
The Whole Story of Half a Girl by Veera Hiranandani
Las tres heridas by Paloma Sánchez-Garnica
Fractured by Wendy Byrne
Leticia by Lindsay Anne Kendal