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

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

An expanded QName is never written directly in XPath, it is purely an internal value manipulated by the system. However, in some APIs and in error messages you might sometimes see expanded QNames written out in the form
{http://my.com/namespace}local-name
. This format is sometimes called Clark notation, after James Clark, the editor of the XSLT 1.0 and XPath 1.0 specifications.

The job of converting element and attribute names from lexical QNames into expanded QNames is done by the XML parser. The namespace URI of the name is found from the innermost element that carries a namespace declaration of the relevant prefix. If a name has no prefix, then its namespace URI is considered to be the default namespace URI in the case of an element name, or a null URI in the case of an attribute name.

A namespace node represents the binding of a namespace prefix to a namespace URI: it uses the node name to hold the prefix, and the string value of the node to represent the URI. In many cases, namespace nodes are redundant—you could reconstruct them from other information, such as the expanded names of elements and attributes, or from the namespace nodes on the parent element. In fact, a real implementation might well construct many namespace nodes on the fly in this way. But in some cases namespace nodes hold essential information; the explanation for this appears in the section “Namespace Sensitive Content” on the next page.

For any element, it is possible to determine all the namespace declarations in force for that element, by retrieving the associated namespace nodes. These are all made available as if the namespace declarations were repeated on that specific element. The application cannot determine where the namespace declaration actually occurred in the original document, but if there is a namespace node present for a particular element, then it follows that there was a namespace declaration either on that element or on some containing element.

Namespace undeclarations, for example
xmlns=“”
, or in XML Namespaces 1.1
xmlns: ppp=“”
, are not represented as namespace nodes; rather they result in the
absence
of a namespace node for the namespace that has been undeclared. Without the undeclaration, the parser would create a namespace node for that namespace for every element within its scope, whether the element used it or not; the namespace undeclaration stops this happening.

Although the namespace declarations are originally written in the source document in the form of XML attributes, they are not retained as attribute nodes on the tree, and cannot be processed by looking for all the attribute nodes. Similarly, it is not possible to generate a namespace node on the result tree by creating an attribute with a name such as
xmlns:p
: such names are reserved for namespace declarations. In the data model, namespaces and attributes are quite distinct animals. XSLT 2.0 has a special instruction,

, for creating namespace nodes on the rare occasions that you need to do so.

Namespace Sensitive Content

Namespace nodes are needed because of the possibility that elements or attributes will contain namespace-sensitive content. If namespace prefixes were only ever used in element and attribute names, it would be possible to reconstruct all the namespace declarations simply by looking at the prefixes and URIs held in the expanded QNames of the elements and attributes.

Unfortunately, it is quite common for XML documents to contain references to element or attribute names within the content of the document. The obvious examples of XML documents that use this technique are XSLT stylesheets and XML schemas. When you see a stylesheet containing an attribute such as
select=“html:table”
, or a schema containing the attribute
type=“xs:date”
, you are looking at namespace-sensitive content. Similarly, the attribute
xsi:type=“xs:short”
appearing in an instance document is using namespaces both in the attribute name and in the attribute content. Stylesheets and schemas are not the only XML documents to use this technique, but they are probably the ones you will encounter most frequently.

Other books

44 Book Four by Jools Sinclair
Col recalentada by Irvine Welsh
After the Fire by J. A. Jance
Please Don't Leave Me Here by Tania Chandler
The Book of Jane by Anne Dayton
The Queen of Bad Decisions by Janel Gradowski