fn:position function

The fn:position function returns the position of the context item in the sequence that is currently being processed.

The position function is typically used in a predicate. However it can also be used to produce the position of each occurrence of its context item.

Syntax

Read syntax diagramSkip visual syntax diagramfn:position()

Returned value

The returned value is an xs:integer value that indicates the position of the context item in the sequence that is currently being processed. The first item in the sequence has position 1. If the context item is undefined, an error is returned. The position function returns a deterministic result only if the sequence that contains the context item has a deterministic order.

Examples

The following query returns the second element in the sequence of <c> elements in the document <x xmlns="http://posample.org"><b><c>x</c><c>y</c><c>z</c></b></x>.

SELECT XMLSERIALIZE(
 XMLQUERY ('declare default element namespace "http://posample.org"; 
 $d/x/b/c[fn:position()=2]'
 PASSING XMLPARSE(DOCUMENT
 '<x xmlns="http://posample.org">
 <b><c>x</c><c>y</c><c>z</c></b></x>')
 AS "d")
 AS CLOB(1K) EXCLUDING XMLDECLARATION)
 FROM SYSIBM.SYSDUMMY1

The returned value is "y".

The following query returns the position of each occurrence of <a><b><c>.

SELECT XMLSERIALIZE(                                   
XMLQUERY('/a/b/c/fn:position()'                        
   PASSING XMLPARSE(DOCUMENT                           
   '<a><b><c>c1</c></b><b><c>c2</c><c>c3</c></b></a>'))
   AS CLOB(1K))                                        
   FROM SYSIBM.SYSDUMMY1                              

The returned values is "1 2 3".