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

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

XPath 2.0 also includes operators to test whether one node is before or after another in document order: the expressions
$a<<$b
and
$b>>$a
both test whether node
$a
is before
$b
in document order.

When a node is copied, for example using the XSLT instruction

, the new node has a new position in document order that is quite unrelated to the position of the old node.

Names and Namespaces

XSLT and XPath are designed very much with the use of XML namespaces in mind, and although many source documents may make little or no use of namespaces, an understanding of the XML Namespaces Recommendation (found in
http://www.w3.org/TR/xml-names11
) is essential.

I'll start with an overview of how namespaces work, and then get into more detail of how they are represented in the XDM data model.

Namespaces: An Overview

Expanding on the description in Chapter 1 (page 23), here's a summary of how namespaces work:

  • A namespace declaration defines a namespace prefix and a namespace URI. The namespace prefix needs to be unique only within a local scope, but the namespace URI is supposed to be unique globally. Globally here really does mean globally—not just unique in the document, but unique across all documents around the planet. To achieve that, the advice is to use a URI based on a domain name that you control, for example,
    http://www.mega-utility.com/namespace/billing
    . The W3C specifications are a little ambivalent about whether the namespace name really must be a valid absolute URI, but this is good practice and some products insist on it. In most of our examples we'll use URIs beginning with
    http://
    .
  • Some people also recommend using a URI that leads human readers to a document on the Web that defines and documents the namespace, but this isn't mandatory. The namespace URI does not have to identify any particular resource, and even if it does, XML and XSLT processors won't go looking for it. Two namespace URIs are considered equal if they contain the same sequence of Unicode characters. This means that the two strings
    file:///c:/this.dtd
    and
    file:///c:/THIS.DTD
    are both legal namespace URIs, whether or not there is actually a file of this name, and they represent different namespaces even though when read as filenames they might identify the same file.
  • To emphasize the point: The fact that every XSLT stylesheet uses the namespace URI
    http://www.w3.org/1999/XSL/Transform
    doesn't mean that you need an Internet connection before you can run a transformation. The name is just an elaborate constant, it's not the address of something that the processor has to go and fetch.
  • A namespace declaration for a non-null prefix is written as follows. This associates the namespace prefix
    my-prefix
    with the namespace URI
    http://my.com/namespace
    :


  • A namespace declaration may also be present for the null prefix. This is known as the default namespace. The following declaration makes
    http://your.com/namespace
    the default namespace URI:


  • In the absence of such a declaration, an unprefixed element name is not in any namespace. I will often describe such a name as being in the null namespace, though this is not the officially correct terminology.
  • The default namespace applies only to element names, not to attribute names; an unprefixed attribute name is always in the null namespace.
  • You can undeclare the default namespace like this:


This puts you back in the position you were in at the outermost level of the document: an element name with no prefix is in the null namespace.

  • The latest version of the XML Namespaces Recommendation, version 1.1, also allows you to undeclare other namespaces, like this:


This has the effect that the prefix becomes unavailable for use within this element. This feature is not widely used, but XDM allows for it.

  • The scope of a namespace declaration is the element on which it appears plus all its children and descendants, excluding any subtree where the same prefix is undeclared or redeclared to associate it with a different URI. This scope defines where the prefix is available for use. Within this scope, any name with the given prefix is automatically associated with the given namespace URI.

Namespaces in the Data Model

A namespace-qualified name is referred to as a QName. When a QName appears in a textual XML document, it is written in the form
prefix
:
local-part
. For example, in the name
xsl:template
, the prefix is
xsl
and the local part is
template
. XSLT refers to this form as a
lexical QName
. The real underlying name, however, is the combination of the namespace URI and the local part. When two names are compared, they are considered equivalent if they have the same namespace URI and the same local part; it is irrelevant whether or not they were written with the same prefix. The combination of a namespace URI and a local name is referred to as an
expanded QName
. An expanded QName also retains the original prefix, but this is “for information only”: it is never used when names are compared.

Other books

Shine by Kate Maryon
Eleven Pipers Piping by C. C. Benison
The Killer by Jack Elgos
Olga by Kotelko, Olga
Cypress Grove by James Sallis
Third Grave Dead Ahead by Jones, Darynda
Wild Fyre by Ike Hamill
Under Fire by Rita Henuber