Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
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
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: