Name

[2.0] node-name()

Returns an expanded QName for nodes that can have names.

Syntax

xs:QName? node-name(node()?)

Input

A node.

Outputs

For nodes that are allowed to have names, returns the node name. For other kinds of nodes (comment nodes, for example), node-name() returns the empty sequence. If the argument to node-name() is the empty sequence, the function returns the empty sequence, as you’d expect.

Defined in

XQuery 1.0 and XPath 2.0 Functions and Operators section 2, “Accessors.”

Example

We’ll use this stylesheet to illustrate how the node-name() function works with different kinds of nodes:

<?xml version="1.0"?>
<!-- node-name.xsl -->
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>

  <xsl:template match="/">

    <xsl:text>&#xA;Here's a test of the node-name() </xsl:text>
    <xsl:text>function:&#xA;</xsl:text>
    
    <xsl:apply-templates 
      select="*|comment()|processing-instruction()|text()"/>
  </xsl:template>

  <xsl:template match="*">
    <xsl:text>&#xA;</xsl:text>
    <xsl:text>  Element node: node-name(.) = '</xsl:text>
    <xsl:value-of select="node-name(.)"/>
    <xsl:if test="prefix-from-QName(node-name(.))">
      <xsl:text>'&#xA;    Prefix: '</xsl:text>
      <xsl:value-of select="prefix-from-QName(node-name(.))"/>
    </xsl:if>
    <xsl:text>'&#xA;    Local name: '</xsl:text>
    <xsl:value-of select="local-name-from-QName(node-name(.))"/>
    <xsl:if test="namespace-uri-from-QName(node-name(.))">
      <xsl:text>'&#xA;    Namespace URI: '</xsl:text>
 <xsl:value-of select="namespace-uri-from-QName(node-name(.))"/> ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.