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

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

    xpath-default-namespace=“http://www.w3.org/1999/xhtml”>

and the names used in expressions and patterns can then remain unprefixed.

As with other attributes on the

element, this attribute can also be specified on other elements in the stylesheet, including literal result elements (as
xsl:xpath-default-namespace
). This allows you to define a different default XPath namespace in different regions of the stylesheet, which is useful if the stylesheet is processing different source documents. (But it might be simpler in this case to split the stylesheet into different modules.) The attribute affects everything that is textually within its scope, and doesn't extend to included or imported modules.

The value is a namespace URI rather than a prefix, which means that the stylesheet doesn't actually need to declare this namespace. This is convenient because it means the namespace won't be automatically copied into the result document. You can also set it to a zero-length string to restore the default setting, which is that unprefixed names are assumed to refer to elements that are in no namespace.

The XPath default namespace affects unprefixed names appearing in the following contexts:

  • Element names (but not attribute names) appearing in path expressions: That is, an
    NCName
    used as a
    NameTest
    in an
    AxisStep
    , when the axis is one that selects elements.
  • Element names used similarly in patterns.
  • Element names and type names (but not attribute names) used in the
    SequenceType
    syntax for defining types, whether the
    SequenceType
    syntax is used within an XPath expression, or on its own within an
    as
    attribute in the stylesheet.
  • Type names used in an XPath
    cast
    expression, for example
    “MF00325Z” cast as part-number
    .
  • Type names used in a
    type
    attribute in the stylesheet.
  • Element names used in

    and

    .

Note that although the namespace applies to unprefixed names appearing in a cast expression, it does not apply to the names of functions, including constructor functions such as
part-number (“MF00325Z”)
where the function name is based on the name of a user-defined type in a schema. With constructor functions the namespace must always be given explicitly; an unprefixed function name can only be used for functions in the core library (that is, standard XSLT and XPath functions).

This means that if you import a schema with no target namespace, you cannot use constructor functions to create instances of the atomic types defined in that schema. Instead, you must use the more long-winded
cast
syntax. (In fact, this is the main reason that the
cast
syntax has been retained in the XPath language.)

The XPath 2.0 specification refers to a concept called the
default function namespace
, which is the namespace in which unprefixed function names are located. Although XPath allows this to be defined as part of the evaluation context, XSLT does not pass this capability on to the user. When XPath expressions are used in XSLT stylesheets, the default function namespace is always the standard namespace
http://www.w3.org/2005/xpath-functions
. This means that calls on standard functions such as
position()
and
last()
never need to be prefixed in XSLT, though you can use a prefix that is bound to the standard namespace if you really want to.

Usage

I would recommend always using this attribute when your source documents use a default namespace declaration; the value should be the namespace URI of this default namespace declaration. Specifying an
xpath-default-namespace
will not stop existing code working that uses explicit prefixes to refer to names in this namespace.

One thing to watch out for is that if your stylesheet creates and uses temporary documents, the chances are that these don't use namespaces. Specifying an
xpath-default-namespace
makes it impossible to refer to names that are in the null namespace, for example elements in such a temporary document (you can't bind a namespace prefix to the null namespace, unfortunately). In this situation you can override the
xpath-default-namespace
in the relevant region of your stylesheet by writing
xpath-default-namespace=“”
.

Other books

St Kilda Blues by Geoffrey McGeachin
Racing Savannah by Miranda Kenneally
Daimon by Jennifer Armentrout
El umbral by Patrick Senécal
GoldLust by Sky Robinson
IMMORTAL MATCHMAKERS, INC. by Mimi Jean Pamfiloff
The Hunger by Whitley Strieber