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

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

A zero-length string cannot be used as a namespace URI, so a runtime error occurs if the result of the computation just described is a zero-length string.

Note that a namespace node is not the same thing as a namespace declaration. An element has a namespace node for every namespace that is in scope. A namespace undeclaration, such as
xmlns=“”
in XML Namespace 1.0, or
xmlns:z=“”
, which is allowed by XML Namespaces 1.1, is not represented by a namespace node. Rather, it is represented in the data model by the absence of a namespace node.

Usage and Examples

Although namespace declarations are a special kind of attribute in the surface XML syntax, they are represented quite differently in the XDM data model. This means that you cannot produce namespace declarations in the result document by using

, or by any other mechanism that produces attribute nodes.

The namespace declarations produced by the serializer are derived from the namespace nodes that are present in the result tree. However, there isn't a one-to-one mapping between namespace nodes and namespace declarations. An element in the result tree has a namespace node for every namespace prefix that is in scope for this element (even if it's also in scope for the element's parent).

In the vast majority of cases, the namespace nodes needed in a result tree are created automatically.

For namespaces used in the names of elements and attributes, this is guaranteed by the namespace fixup process described under

on page 310. This procedure is invoked whenever an element is created in a result tree, whether by

, a literal result element, or by

or

. It is the namespace fixup procedure that makes the final decision on what prefixes to use for these names.

Creating (or not creating) a namespace node will never change the name of an element or attribute. If you are outputting an element with local name


in no namespace, and you want it to be in namespace
http://garden-furniture.com/ns
, then you need to change the instruction that creates the element node. If you get the element names right (remembering that an element name includes the namespace URI as well as the local part), then the namespace nodes will usually look after themselves.

The only difficulties arise therefore when you need namespaces to be declared in the result document that are not used in element and attribute names. I've come across three reasons for doing this:

1.
You want the namespace to be declared once on an outer element of the document, whereas in the normal course of events it would be declared many times on inner elements. This is essentially cosmetic, but it can make a significant difference to the readability (and the size) of your output. In some cases this kind of declaration is necessary to make the output valid against a DTD, or simply against a written specification—the Locale Data Markup Language, for example, requires the element

to declare the namespace in which its children appear.

2.
Your document uses namespace prefixes in the content (as distinct from in the names) of elements and attributes. This is the most common case and we'll devote the rest of the section to it.

3.
The definition of the result document type uses namespace declarations as some kind of marker, where the mere presence of the namespace declaration carries information, whether or not the prefix that's being declared is ever used. This is a pretty weird way of using namespaces, but there's no law against it. In the case I saw, it was done because adding namespace declarations to a document can be done without changing the schema.

In the rest of this section we'll focus on the use of QNames in element and attribute content.

An example is if you want to output the following:

23.50

Your document must then contain a namespace declaration that binds the namespace prefix
xs
to the namespace URI
http://www.w3.org/2001/XMLSchema
. The serializer will only produce such a declaration if it encounters a namespace node in the result tree that binds this prefix to this URI.

Here is one convenient way to output the above element:


   

                  select=“‘http://www.w3.org/2001/XMLSchema’”/>

   


Note that there is no need to use

to produce a namespace declaration that binds the prefix
xsi
to the namespace
http://www.w3.org/2001/XMLSchema-instance
. Because this namespace is used in an attribute name, the namespace fixup process will ensure that it is declared. In fact, it would be declared in this case even without namespace fixup, because the rules for a literal result element ensure that all namespaces that are in scope for the literal result element in the stylesheet are copied to the result document, and the
xsi
namespace must be in scope here, or the stylesheet fragment would not be valid.

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

Other books

Shadow (Defenders MC Book 1) by Amanda Anderson
A Winter Scandal by Candace Camp
Something She Can Feel by Grace Octavia
Braced to Bite by Serena Robar
Heartbreak Hotel by Deborah Moggach
Hilda and Pearl by Alice Mattison
Body Dump by Fred Rosen
Extraordinary by Nancy Werlin
The Last Hedge by Green, Carey

© 100 Vampire Novels China Edition 2015 - 2024    Contact for me [email protected]