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

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

The second step is achieved by setting the following declaration in the stylesheet:


In this example, the namespace undeclaration is largely cosmetic. But if the

element were the envelope of a SOAP message, and the

element were the payload of the SOAP message, then the namespace undeclarations could be useful: the effect is that if the recipient of the SOAP message extracts the payload using another XSLT transformation, it will be in precisely its original form, not polluted with any declarations of SOAP namespaces.

For information about SOAP, see
http://www.w3.org/TR/soap12-part0/
.

Namespace Prefixes

When a literal result element is copied to the result tree, the element name and attribute names of the new nodes in the result tree will have the same expanded name (that is, local name and namespace URI) as the corresponding nodes in the stylesheet. Unless there is a conflict, the names that are eventually output will also use the namespace prefix that was used in the stylesheet.

There are unusual circumstances when the XSLT processor may need to change the prefix of an element or attribute. Consider the following contrived example:


   http://domain-b.com/


The generated output in this case will look something like this:

   xmlns:ns0=“http://domain-a.com/”

   xmlns:p=“http://domain-b.com/”/>

Here the literal result element and its contained

instruction are in conflict: they are trying to establish different bindings for the same namespace prefix
p
. The specification is clear that the

instruction wins, so the element acquires an arbitrary prefix allocated by the system.

When namespace nodes are copied from the source or stylesheet tree to the result tree, the namespace prefix and namespace URI are both copied unchanged. When element or attribute nodes are copied, the expanded name of the element or attribute (that is, its local name and namespace URI) is always preserved, but the namespace prefix may occasionally need to be changed. If this happens, however, an extra namespace node will be added to the result tree to associate the new namespace prefix with the correct namespace URI.

Namespace Aliasing

In some circumstances, instead of changing the namespace prefix when a literal result element is copied to the result tree, it is necessary to change the namespace URI.

The most obvious situation where this arises is when the output document is itself a stylesheet. This isn't as esoteric a requirement as it may appear—generating a stylesheet can be a very useful technique. For example, if your company changes its house style to use different fonts and colors, you could write an XSLT transformation to convert all your existing stylesheets to the new standard.

When you generate a stylesheet, you will want to generate XSLT elements such as

in the result tree, but you can't include such elements as literal result elements in the stylesheet, because they would be mistaken for instructions. One approach is to generate these elements using the

instruction instead of literal result elements. But there is another way of doing it: you can include them in the stylesheet with a different namespace and then declare in an

element that the URI should be changed when the literal result element is copied to the result tree.

For more details of this mechanism, see

in Chapter 6, on page 394.

Attribute Value Templates

As we've seen, an attribute value template is a special form of parameterized attribute value. There are two ways they can be used:

  • On a literal result element, an attribute value template provides a way of generating an attribute whose value is computed at runtime rather than always taking the same value, for example
    .
    You could achieve the same effect with the

    instruction, but attribute value templates are easier to write and understand.
  • On some XSLT elements, certain attributes can be computed at runtime. For example, when sorting, instead of writing
    order = “ascending”
    or
    order = “descending”
    , you could write
    order = “{$order}”
    so that the order varies, depending on a runtime parameter.

Note that there are very few attributes where this facility is available. They are listed later in this section.

The term
template
here has nothing to do with

elements. Attribute value templates simply provide a notation for embedding variable components into an otherwise fixed attribute value.

An attribute value template is a string in which XPath expressions may be embedded within curly braces (
{
and
}
). The XPath expression is evaluated, and in general the result will be a sequence. This sequence is processed using the same rules as for an

instruction, explained in detail on page 254 in Chapter 6: in short, adjacent text nodes are merged, nodes are atomized, atomic values are converted to strings, and the strings are then concatenated, with a single space character inserted as a separator between each string. The resulting concatenated string is substituted into the attribute value in place of the original XPath expression and curly braces.

Other books

Jeremy Poldark by Winston Graham
Bound By Her Ring by Nicole Flockton
Red Winter by Montgomery, Drew
wcEND.rtf by The Wishing Chalice (uc) (rtf)
The Voice on the Radio by Caroline B. Cooney
Lust Under Licence by Noel Amos
My Fair Lily by Meara Platt
The Fangs of the Dragon by Simon Cheshire