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

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

The prefix, as always, is simply a way of referring to the associated namespace URI: it is the namespace URI that is really being excluded, not the prefix itself. So if the same namespace URI is declared again with a different prefix, it is still an excluded namespace.

What exclude-result-prefixes Doesn't Do

The
[xsl:]exclude-result-prefixes
attribute applies only to namespace nodes copied from the stylesheet using literal result elements. It does not affect namespace nodes copied from the source document using

or

; these can be suppressed by using the
copy-namespaces
attribute on the instruction itself.

Like the other attributes on the

element, the
exclude-result-prefixes
attribute applies only to elements actually within the stylesheet module, not to those brought in using

or

.

What happens if you try to exclude a namespace that is actually needed because it is used in the result tree? The XSLT processor is obliged to generate output that conforms to the Namespaces Recommendation, so it will ignore the request to exclude this namespace. Or more accurately, the evaluation of the literal result element will not cause the namespace node to be copied, but the namespace fixup process that comes into play when the element has been constructed will then generate namespace nodes for any namespaces that were missing. Namespace fixup is described under

on page 310.

Using
exclude-result-prefixes
will never move an element or attribute into a different namespace. People sometimes see an element such as

in the output, and ask how to get rid of the
xmlns=“”
. The answer is: look at the code that's generating the

element, and change it to put the element in the proper namespace. If the

element is in the same namespace as its parent, then the serializer won't need to generate the
xmlns=“”
declaration.

Usage

The simplest way to decide which namespace prefixes to list here is by trial and error. Run the stylesheet, and if the output document contains namespace declarations that clearly serve no useful purpose, add them to the
exclude-result-prefixes
attribute and run the stylesheet again.

The XSLT namespace itself and namespaces used for extension elements will be excluded automatically. However, the stylesheet is also likely to contain references to the schema namespace, which you may not want in your result document. It will also contain any namespace you have used for defining your local stylesheet functions, which you almost certainly don't want in the result document.

A common cause of unwanted namespace declarations finding their way into the result document is where your stylesheet needs to refer to namespaces used in the source document; for example, in a template
match
pattern, but where none of these elements is copied into the destination document.

For example:

          version=“2.0”

          xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”

          xmlns:po=“http://accounting.org/xslt”

          exclude-result-prefixes=“po”

>


   

   …

   



Here the
po
namespace would be copied into the result document if it weren't for the
exclude- result-prefixes
attribute, because it is in scope when the literal result element

is evaluated.

As with the other

attributes, you don't have to apply the exclusion to the whole stylesheet if you don't want to, you can also apply it to any part of the stylesheet by using the
xsl:exclude-result-prefixes
attribute on any literal result element. It's probably a good idea in practice to keep the declaration of a namespace and the directive that excludes it from the result document together in one place.

The extension-element-prefixes Attribute

This attribute identifies a set of namespaces used for extension instructions. Extension instructions may be defined by an implementor, a user, or a third party. An example is the

instruction in Saxon, which allows data to be fetched from a relational database. They can be used anywhere an instruction can be used, that is, within a sequence constructor. If an element is found in a sequence constructor that is not in the XSLT namespace, then it must either be an extension instruction or a literal result element. If its namespace is the same as a namespace identified in the
[xsl:]extension-element-prefixes
attribute of the containing

element, of some other containing element, or of the element itself, then it will be treated as an extension instruction; otherwise, it will be treated as a literal result element.

Effect

The value of the attribute is a whitespace-separated list of prefixes; each of these prefixes must identify a namespace declaration present on the containing element. The default namespace (the namespace declared using the
xmlns
attribute) may be designated as an extension element namespace by using the pseudoprefix
#default
.

The scope of the
extension-element-prefixes
attribute is the stylesheet module. It does not affect included or imported modules.

If a namespace is designated as an extension element namespace, then every XSLT processor will recognize that these elements are extension instructions. However, some XSLT processors may be unable to evaluate them. For example, if the namespace
http://saxon.sf.net/
is designated as an extension namespace, then both Saxon and AltovaXML will recognize that these elements are extensions, but the likelihood is that Saxon will know how to handle them and AltovaXML won't. If the processor knows how to evaluate the instruction, it does so; otherwise, it looks to see if the element contains an

instruction. If it does, the

instruction is evaluated; otherwise, an error is reported.

Other books

A Cutthroat Business by Jenna Bennett
Ruby Rose by Alta Hensley
A Game Worth Watching by Gudger, Samantha
Tuesday's Child by Clare Revell
Journey into the Unknown by Tillie Wells
Sula by Toni Morrison
A Christmas Promise by Annie Groves
Accidental Evil by Ike Hamill
The Colour of Memory by Geoff Dyer