Determining the Position Number of a Node

The position() function returns an integer that indicates the position of the node within the parent. Positions start with 1; a node with a position of 1 is always the first node in the collection.

For example, the following query returns the first three degree elements in the document:

(//degree)[position() < 4] 


The next query finds the first two book children in the current context:

book[position() <= 2] 


The XPath processor executes the position() function with respect to the parent node. Consider the following data:



</x> <x>


The following expression returns the first y element contained in each x element:

x/y[position() = 1] 


For more information, see also Finding an Element with a Particular ID.

Free Stylus Studio XML Training:
W3C Member