| name() Function | |
| Returns the qualified name of a node. The qualified name includes the appropriate namespace prefix. For information on the namespace URI (not the prefix), XPath provides the namespace-uri() function. | |
| Inputs | |
|
An optional node-set. If no node-set is given, the name() function creates a node-set with the context node as its only member. |
|
| Output | |
|
The expanded name of the node. If the argument node-set is empty, or if the first node in the node-set does not have an expanded name, an empty string is returned. |
|
| Defined in | |
|
XPath section 4.1, Node Set Functions. |
|
| Example | |
|
Here is the XML document we'll use to demonstrate the name() function:
<?xml version="1.0"?>
<report>
<title>Miles Flown in 2001</title>
<month sequence="01">
<miles-flown>12379</miles-flown>
<miles-earned>35215</miles-earned>
</month>
<month sequence="02">
<miles-flown>32857</miles-flown>
<miles-earned>92731</miles-earned>
</month>
<month sequence="03">
<miles-flown>19920</miles-flown>
<miles-earned>76725</miles-earned>
</month>
<month sequence="04">
<miles-flown>18903</miles-flown>
<miles-earned>31781</miles-earned>
</month>
</report>
We'll use this stylesheet to output the value of the name() function for each node in the XML document:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:months="Lookup table for month names">
<months:name sequence="12">December</months:name>
<months:name sequence="01">January</months:name>
<months:name sequence="02">February</months:name>
<months:name sequence="03">March</months:name>
<months:name sequence="04">April</months:name>
<months:name sequence="05">May</months:name>
<months:name sequence="06">June</months:name>
<months:name sequence="07">July</months:name>
<months:name sequence="08">August</months:name>
<months:name sequence="09">September</months:name>
<months:name sequence="10">October</months:name>
<months:name sequence="11">November</months:name>
<xsl:output method="text"/>
<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>
<xsl:template match="/">
<xsl:value-of select="$newline"/>
<xsl:text>A test of the name() function:</xsl:text>
<xsl:value-of select="$newline"/>
<xsl:value-of select="$newline"/>
<xsl:for-each select="document('')//*">
<xsl:text>name: </xsl:text>
<xsl:value-of select="name()"/>
<xsl:value-of select="$newline"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
When we transform the XML document with this stylesheet, here are the results: A test of the name() function: name: xsl:stylesheet name: months:name name: months:name name: months:name name: months:name name: months:name name: months:name name: months:name name: months:name name: months:name name: months:name name: months:name name: months:name name: xsl:output name: xsl:variable name: xsl:text name: xsl:template name: xsl:value-of name: xsl:text name: xsl:value-of name: xsl:value-of name: xsl:for-each name: xsl:text name: xsl:value-of name: xsl:value-of |
|