O'Reilly logo

XSLT, 2nd Edition by Doug Tidwell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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(.))"/> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required