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

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

node-name

The
node-name()
function returns a value of type
xs:QName
containing the expanded name of a node, that is, the namespace URI and local name.

Signature

Argument
Type
Meaning
input
node()?
The node whose name is required
Result
xs:QName?
The name of the node if it has a name, or an empty sequence if it has no name

Effect

If the node is an element or attribute, then the function returns an
xs:QName
whose components are the namespace URI, local name, and prefix of this node. If the node is not in a namespace, then the namespace URI component of the
xs:QName
will be absent (the function
namespace-uri-from-QName()
will return the empty sequence).

If the node is a processing instruction, the function returns an
xs:QName
whose local name is the name of the processing instruction, and whose namespace URI and prefix are absent.

If the node is a text node, comment, or document node, or if an empty sequence is supplied, then the function returns an empty sequence.

If the node is a namespace node, then the function returns an
xs:QName
whose local name represents the namespace prefix and whose namespace URI and prefix are absent; except when the namespace node represents the default namespace, in which case the function returns an empty sequence.

Examples

It's difficult to illustrate function calls that return
xs:QName
values, because displaying an
xs:QName
as a string loses information about the namespace URI. In these examples I'll display the value in so-called Clark notation (after James Clark, the editor of the XSLT 1.0 and XPath 1.0 specifications), which uses the format
{uri}local-name
.

Assume the following source document:

      xmlns:xs=“http://www.w3.org/2001/XMLSchema”>

  

    

                xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

                xmlns:soap=“”>

      Hello

    

  


Expression
Result (in Clark notation)
node-name(/*)
{http://schemas.xmlsoap.org/soap/envelope/}Envelope
node-name(/*/*/*)
{http://example.com/soapdemo}echoString
node-name(//@*:type)
{http://www.w3.org/2001/XMLSchema-instance}type

Usage

To access the components of the
xs:QName
returned by the
node-name()
function, you can use the functions
local-name-from-QName()
and
namespace-uri-from-QName()
. Alternatively, if you don't like long function names, you can use the
local-name()
and
namespace-uri()
functions to get these two components directly from the node itself.

See Also

local-name-from-QName()
on page 826

namespace-uri-from-QName()
on page 841

local-name()
on page 824

name()
on page 835

namespace-uri()
on page 837

normalize-space

The
normalize-space()
function removes leading and trailing whitespace from a string, and replaces internal sequences of whitespace with a single space character.

For example, the expression
normalize-space(‘x y’)
returns the string
x y
.

Changes in 2.0

None.

Signature

Argument
Type
Meaning
value
(optional)
xs:string?
The input string. If the argument is omitted, it defaults to
string(.)
. If an empty sequence is supplied, the function returns a zero-length string.
Result
xs:string
A string obtained by removing leading and trailing whitespace from the input string, and replacing internal sequences of whitespace by a single space character
.

Effect

When the function is called with no arguments, the argument defaults to the result of applying the
string()
function to the context item; an error is reported if there is no context item.

Whitespace is defined, as in the XML specification, as a sequence of space, tab, newline, and carriage return characters (
#x9
,
#xA
,
#xD
, and
#x20
).

Examples

Expression
Result
normalize-space(“ the quick brown fox ”)
“the quick brown fox”
normalize-space(“ ”)
“”
normalize-space(“piano”)
“piano”
normalize-space(())
“”

Usage

It is often a good idea to apply the
normalize-space()
function to any string read from the source document before testing its contents, as many users will assume that leading and trailing whitespace has no significance and that within the string, multiple spaces or tabs are equivalent to a single space.

Don't imagine that the XSLT

declaration does this for you. The only thing it does is to remove text nodes that contain whitespace only.

Using
normalize-space()
shouldn't be necessary when accessing structured information in a schema-validated document. The schema should specify for each type (in the
xs:whiteSpace
facet) how whitespace is to be treated, and this will normally ensure that redundant whitespace is removed automatically when nodes are atomized. Note that the action of the
normalize-space()
function is equivalent to the option

in XML Schema. This removes whitespace more vigorously than the schema type
xs:normalizedString
, which uses the option

(this doesn't replace runs of spaces with a single space, it only replaces individual newlines, carriage returns or tabs with single space characters.)

However, if you access the string value of an element with a mixed content type (typically by calling the
string()
function explicitly, or by accessing the text nodes of an element explicitly), then schema-defined whitespace normalization will not be applied, so using
normalize-space()
is a good idea.

The
normalize-space()
function can be particularly useful when processing a whitespace-separated list of values. Such lists are used in some document designs. With a schema processor, the system can deliver the value as a sequence of strings, but in the absence of a schema you have to tokenize the sequence yourself. You can call
normalize-space()
to ensure that there is a single space between each string, and it is then possible to use
substring-before()
to get the next token. To make this easier still, I usually add a space at the end of the string after normalization, so that every token is followed by a single space.

One situation where it isn't safe to use
normalize-space()
is where you are processing mixed element content containing character-level formatting attributes. For example, if you process the nodes that result from the element:

Other books

No Place Like Home by Debra Clopton
The Book of Transformations by Newton, Mark Charan
Never Say Sty by Johnston, Linda O.
An Ermine in Czernopol by Gregor von Rezzori
Keeper of the Heart by Lindsey, Johanna
Autopilot by Andrew Smart
Promise to Cherish by Elizabeth Byler Younts
Fear by Gabriel Chevallier