fn:name function

The fn:name function returns the prefix and local name parts of a node name.

Syntax

Read syntax diagram
>>-fn:name(-+------+-)-----------------------------------------><
            '-node-'     

node
The qualified name of a node for which the name is to be retrieved. If node is not specified, fn:name is evaluated for the current context node.

Returned value

The returned value is an xs:string value. The value depends on the value of node:
  • If node meets any of the following conditions, a string of length 0 is returned:
    • node is the empty sequence.
    • node is a document node, a comment, or a text node. These nodes have no name.
  • In the following cases, an error is returned:
    • The context node is undefined.
    • The context item is not a node.
    • node is a sequence of more than one node.
  • Otherwise, an xs:string value is returned that contains the prefix (if present) and local name for node.

Example

The following example returns the qualified name for node b.
SELECT XMLSERIALIZE(
 XMLQUERY (  'declare namespace ns1="http://posample.org";
 fn:name($d/x/ns1:b)'
 PASSING XMLPARSE(DOCUMENT
 '<x xmlns:n="http://posample.org">
  <n:b><n:c></n:c></n:b></x>')
 AS "d")
 AS CLOB(1K) EXCLUDING XMLDECLARATION)
 FROM SYSIBM.SYSDUMMY1

The returned value is "n:b".

The following example demonstrates that fn:name() with no argument returns the context node.

In the sample CUSTOMER table, the customer document for customer 1001 looks like this:

<customerinfo xmlns="http:⁄⁄posample.org" Cid="1001">
  <name>Kathy Smith<⁄name>
  <addr country="Canada">
    <street>25 EastCreek<⁄street>                        
    <city>Markham<⁄city>                                 
    <prov-state>Ontario<⁄prov-state>                     
    <pcode-zip>N9C 3T6<⁄pcode-zip>                       
  <⁄addr>                                                      
  <phone type="work">905-555-7258<⁄phone>               
<⁄customerinfo>

The following example returns the qualified name for the context node.

SELECT                                                            
XMLSERIALIZE(                                                     
XMLQUERY('declare default element namespace "http://posample.org";
 $X/customerinfo/phone/fn:name()'                                 
 PASSING INFO AS "X")                                             
 AS CLOB(1K))
 FROM CUSTOMER WHERE CID=1001                                                     

The returned value is "phone".