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

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

Effect

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

Node kind
Name
document
None, a zero-length string is returned.
element
The element name (a lexical QName), as it appears in the source XML.
attribute
The attribute name (a lexical QName), as it appears in the source XML.
text
None, a zero-length string is returned.
processing instruction
The
target
used in the processing instruction to identify the application for which it is intended.
comment
None, a zero-length string is returned.
namespace
The namespace prefix; or the zero-length string if this is the default namespace. (This is
not
prefixed with
xmlns:
.)

Except for element and attribute nodes,
name()
returns the same value as
local-name()
.

For elements and attributes, the
name()
function is the only XPath construct whose result depends directly on the namespace prefixes used in the node name, as distinct from the local name and the namespace URI.

Where the node is an element or attribute from a source document, the
QName
returned will normally use the same prefix as appeared in the original XML source. However, it is good practice not to rely on a particular prefix being used, because originators of XML documents generally assume that they have a free choice of namespace prefixes, and any preprocessing applied to the document before the transformation starts could change the prefixes. When the node was constructed during the course of the transformation, for example by an

or

instruction, the XSLT language specification in most cases mandates the prefix that will be used. There are a few cases, however, where the system may need to choose a different prefix in order to avoid conflicts, and in these cases the result of the
name()
function becomes unpredictable.

Usage

The
name()
function is useful when you want to display the element name, perhaps in an error message, because the form it takes is the same as the way in which users will generally write the element name.

So, for example, you could use
name()
in the output of the diagnostic
trace()
function:

for $e in child::* return

  trace(string(.), concat(“contents of element ”, name()))

You can also use the
name()
function to test the name of a node against a string, for example,
doc:title[name(..)=‘doc:section’]
. However, it's best to avoid this if you can:

  • Firstly, this fails if the document uses a different prefix to refer to the namespace. There's nothing here to tell the system to treat
    doc:section
    as a
    QName
    , so if the writer of a particular document chose to use the prefix
    DOC
    instead of
    doc
    for this namespace, the test would fail, even though the names are equivalent.
  • Secondly, there is usually a better way of doing it: this particular example can be written as
    doc:title[parent::doc:section]
    . In fact, in most cases where you want to test whether a node has a particular name, you can do it using a predicate of this form. The
    self
    axis is particularly useful. For example, to test whether the current node is a
    figure
    element, write
    if (self::figure) then ...
    . This doesn't work for attribute nodes (because the principal node kind of the self axis is element nodes; see
    Name Tests
    in Chapter 9, page 614). Instead you can write
    if
    (self::attribute(figure)) then...
    .

Other books

A Million Miles Away by Avery,Lara
Swastika by Michael Slade
Sustenance by Chelsea Quinn Yarbro
Wishes & Tears by Nancy Loyan
Through the Shadows by Gloria Teague
The Bohemian Girl by Frances Vernon
New America by Poul Anderson
Lo sagrado y lo profano by Mircea Eliade