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

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

One common requirement is to sort data on the value of a sort key that is supplied as a runtime parameter (this might be because the user has asked interactively for a table to be sorted on a particular column). XSLT does not allow the expression that defines the sort criteria to be completely dynamic. But very often the possible sort keys are all element children of the elements that represent the rows being sorted. In this situation it is possible to define the sort key like this:


   

If you are using namespaces it is safer to do such tests using the
node-name()
function, which gives you an
xs:QName
as its result: an
xs:QName
represents an expanded name (namespace URI plus local name) and is not sensitive to the choice of prefix. Alternatively, use the
namespace-uri()
and
local-name()
functions to test the two components of the expanded name separately.

If you want to select all attributes except the
description
attribute, you can write:

@*[name() != ‘description’]

This is namespace-safe, because an unprefixed attribute name always represents a name in no namespace. But in XPath 2.0, I prefer:

@* except @description

In XSLT, avoid using
name()
to generate a name in the result document, for example, by writing

. The problem is that any prefix in
name()
is interpreted in the light of namespace declarations appearing in the stylesheet, not namespace declarations in the original source document. The correct tool for this job is

. There are cases where

won't do the job; for example, you may want to use the name of an attribute in the input document to generate the name of an element in the output document. In this case, use
local-name()
and
namespace-uri()
separately, for example:


Examples

Consider the source document:


    text 


Expression
Result
name(/)
“”
name(/*)
“my:doc”
name(/*/@*[.=‘high’])
“security”
name(/*/@*[.=‘A23’])
“xml:id”
name(//comment())
“”
name((//text())[1])
“”
name(//processing-instruction())
“action”
name(//namespace::my)
“my”

See Also

local-name()
on page 824

namespace-uri()
in the following section

node-name()
on page 843

namespace-uri

The
namespace-uri()
function returns a string that represents the URI of the namespace in the expanded name of a node. Typically, this will be a URI used in a namespace declaration, that is, the value of an
xmlns
or
xmlns:*
attribute in the source XML.

For example, if you apply this function to the outermost element of an XSLT stylesheet by writing the expression
namespace-uri(doc(‘’)/*)
, the result will be the string
http://www.w3.org/1999/XSL/Transform
.

Changes in 2.0

The result is an
xs:anyURI
value rather than a string. This makes very little difference in practice.

Signature

Argument
Type
Meaning
node
(optional)
node()?
Identifies the node whose namespace URI is required. If the argument is an empty sequence, the function returns a zero-length string. If the argument is omitted, the target node is the context node. It is then an error if there is no context item, or if the context item is not a node.
Result
xs:anyURI
The namespace URI of the expanded name of the target node
.

Effect

The namespace URI of a node depends on the kind of node, as follows:

Node kind
Namespace URI
document
None, a zero-length URI is returned.
element
If the element name as given in the source XML contained a colon, the value will be the URI from the namespace declaration corresponding to the element's prefix. Otherwise, the value will be the URI of the default namespace. If this is null, the result will be a zero-length URI.
attribute
If the attribute name as given in the source XML contained a colon, the value will be the URI from the namespace declaration corresponding to the attribute's prefix. Otherwise, the result will be a zero-length URI.
text
None, a zero-length URI is returned.
processing instruction
None, a zero-length URI is returned.
comment
None, a zero-length URI is returned.
namespace
None, a zero-length URI is returned.

Except for element and attribute nodes,
namespace-uri()
returns an empty string.

The result of the function is an
xs:anyURI
value rather than an
xs:string
. However, the type promotion rules ensure that an
xs:anyURI
can be used almost anywhere that an
xs:string
is allowed. There are one or two exceptions, for example the casting rules are different, but unless you choose strings such as
true
and
false
as namespace URIs and then attempt to cast them to
xs:boolean
the differences are unlikely to be noticed.

Other books

Willing Hostage by Marlys Millhiser
Bitten: A Vampire Blood Courtesans Romance by Kim Faulks, Michelle Fox
The True Prince by J.B. Cheaney
A Future for Three by Rachel Clark
Prize Problems by Janet Rising
Stolen Girl by Katie Taylor
Dark Specter by Michael Dibdin
The Billionaire's Con by Crowne, Mackenzie