XSLT and XPath function reference in alphabetical order
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 5, translated from German)
Node names, identifiers and URI information
A xs:string string in the lexical form of a QName which represents the identifier of the node passed on to the function.
Optional. A sequence of nodes. If no argument is passed on, the QName of the context node is outputted; where applicable (for a nameless context node) the empty string.
Purpose of use:
The fn:name() function outputs a xs:string string in the form of the not expanded QName of a node. As a rule, it is a tag name or an attribute name (generic identifier) as it can be found in the source code of the XML document.
Without any argument passed on, the function is applied to the context node and returns its name. However, an error message occurs if no context node exists (err:XPDY0002) or the context item is not a node (err:XPTY0004).
An argument passed on must be a sequence of nodes. If the sequence passed on contains exactly one node, its not expanded QName consisiting of a possible prefix and the local identifier is returned. If there are several nodes in the sequence, the QName of the first node is returned.
For an empty sequence passed on, also an empty string is returned. In case the value passed on is not a node sequence (which means, for example, atomic values), the parser shall indicate an error.
In case the node passed on or the first node of the sequence passed on is a nameless node (document node, text node, comment node), an empty string is returned.
For element names or attribute names which can be found in a namespace, the appropriate namespace prefix is outputted.
Usually, it is the same prefix as used in the source code of the document. Sometimes, however, it will be another one. This may occur, if, within a document or within several documents being only united during the transformation, one and the same namespace URI is declared with various prefixes. Since the parser internally works with expanded QNames, it is not predictable when reconverting them which prefix the parser outputs for fn:name().
Although this is a rather marginal imponderability, it is recommended to act with caution when testing QNames for having the same name by means of fn:name().
In case a return value in the form xs:QName is required
The name() function is only maintained as fn:name() in XPath 2.0 for reasons of compatibility. In addition, the new fn:node-name() function is introduced which expressly has a QName as return value: fn:node-name($srcval as node()?) as xs:string?
Example – distinction between identifier and local identifier:
<?xml version="1.0"?> <example xmlns:test="my-test-namespace"> <my_element> element without namespace </my_element> <test:my_element> element with namespace </test:my_element> </example>
In this document two elements with the same local identifier are contained which differ by means of their namespace.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="example"> <result> <xsl:for-each select="*"> <xsl:value-of select="fn:position()"/>. element: The name of the element is " <xsl:value-of select="fn:name()"/> ". The local name of the element is " </xsl:text><xsl:value-of select="fn:local-name()"/> ". The namespace URI is " <xsl:value-of select="fn:namespace-uri()"/> ". </xsl:for-each> </result> </xsl:template> </xsl:stylesheet>
The stylesheet reads out the local identifier and the namespace URI of all elements in <example> by means of a loop.
<?xml version="1.0" encoding="UTF-8"?> <result> 1. element: The name of the element is "my_element". The local name of the element is "my_element". The namespace URI is "". 2. element: The name of the element is "test:my_element". The local name of the element is "my_element". The namespace URI is "my-test-namespace". </result>
The fn:name() function outputs for both elements the QName which consists for the first element only of the local identifier and for the second element of the local identifier and the namespace prefix. Since this example is unambiguous, the function outputs in each case the actually used prefix.
fn:name() as xs:string
fn:name($arg as node()?) as xs:string
|<< back||next >>|
Copyright © Galileo Press, Bonn 2008
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "XSLT 2.0 & XPath 2.0" is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn, Germany