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

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

If you write a stylesheet that produces an XSLT stylesheet as its output, then it becomes very important to get the right namespace declarations into the output, because XSLT makes heavy use of attributes that contain namespace prefixes: they arise both in attributes such as the
name
attribute of

,

, and

, and wherever the stylesheet contains XPath expressions. It's worth remarking that there is nothing in the schema for XSLT stylesheets that marks these attributes out as special. Even the
name
attributes that appear to have QName-valued content do not actually have a type of
xs:QName
. This is because although their lexical space is the same as
xs:QName
, they follow the convention “no prefix means no namespace”, whereas the rule for the
xs:QName
type is “no prefix means default namespace”. So these attributes are simply strings, which means it is the application that must choose a namespace prefix and then create a namespace node to bind this to the correct namespace URI.

The

instruction adds a namespace node to the result sequence produced by the sequence constructor that it is part of. Normally, this result sequence will immediately be used to create the content of an element node. In this case, the attribute and namespace nodes in the sequence need to come before any other nodes. It doesn't matter what order the attributes and namespace nodes are in relative to each other. It is an error if there are two namespace nodes that bind the same namespace prefix to different URIs; this could happen if you create a namespace node manually using

that clashes with one that is copied automatically from a source document by an

or

instruction, or from the stylesheet by a literal result element. This is only really likely to happen in the case of the default namespace. It is permissible to create a default namespace node using

, but it's probably not a good idea.

Namespace fixup happens after all the namespace nodes from this result sequence have been constructed, and it is constrained to generate prefixes that don't clash with these namespace nodes. If you have created a namespace node for the default namespace (that is, the empty prefix), then the system will have a problem if the element node itself is in the null namespace, because an element in the null namespace has to use the empty prefix, and it will no longer be available. This can cause a runtime failure.

See Also


on page 254


on page 306

xsl:namespace-alias

The

element allows a namespace used in the stylesheet to be mapped to a different namespace used in the output. It is most commonly used when writing transformations that produce an XSLT stylesheet as their output.

Changes in 2.0

The rules for generating namespace prefixes have been made stricter. The “null namespace” is now treated in the same way as a real namespace (many XSLT 1.0 products did this, but the specification wasn't clear).

Format

  stylesheet-prefix = prefix | “#default”

  result-prefix = prefix | “#default”/>

Position


is a top-level declaration, which means it must be a child of the

element. It may be repeated any number of times in a stylesheet.

Attributes

Name
Value
Meaning
stylesheet-prefix
mandatory
NCName or
#default
A namespace prefix used in the stylesheet
result-prefix
mandatory
NCName or
#default
The prefix of the corresponding namespace to be used in the output

Content

None. The

element is always empty.

Effect

The

element affects the treatment of namespaces on literal result elements.

Normally, when an element node is output by processing a literal result element, the output element name will have the same local part, the same prefix, and the same namespace URI as those of the literal result element itself. The same applies to the attributes of the literal result element. The namespace nodes on the literal result element must be copied unchanged to the result tree, using the same prefix and namespace URI. (The XSLT specification states that when processing a literal result element, all the namespaces that are in scope for the element in the stylesheet, with certain defined exceptions, will also be present in the output, even if they aren't used. Redundant namespace nodes can be suppressed by using the
xsl:exclude-result-prefixes
attribute. For more details on this, see the section
Literal Result Elements
, on page 112 in Chapter 3.)

Suppose you want the output document to be an XSLT stylesheet. Then you need to create elements such as

that are in the XSLT namespace. However, you can't use

as a literal result element, because by definition, if an element uses the XSLT namespace, it is treated as an XSLT element.

The answer is to use a different namespace on the literal result element in the stylesheet, and include an

declaration to cause this to be mapped to the XSLT namespace when the literal result element is output. So your literal result element might be

, and you could use an

element to indicate that the stylesheet prefix
out
should be mapped to the result prefix
xsl
.

The

element declares that one namespace URI, the stylesheet URI, should be replaced by a different URI, the result URI, when literal result elements are output. The namespace URIs are not given directly, but are referred to by using prefixes that are bound to these namespace URIs as a result of namespace declarations that are currently in force. Either one of the namespace URIs may be the default namespace URI, which is referred to using the pseudoprefix
#default
. If there is no default namespace defined,
#default
denotes the “null namespace URI”, which for this purpose is treated as if it were an ordinary namespace.

Other books

Afterworlds by Scott Westerfeld
1503933547 by Paul Pen
Silent Witness by Rebecca Forster
Darkest Knight by Karen Duvall
Tangles and Temptation by India-Jean Louwe
Premeditated by Mcquein, Josin L.