Positions in Relation to Parent Nodes

Positions are relative to the parent. Consider the following data, which has line numbers on the left for explanation only.

1 	<x>
               
2 		<z>
               
3 		<z/>
               
4 	</x>
               
5 	<x>
               
6 		<y>
               
7 		<y/>
               
8	</x>
               
9	<x>
               
10		<y>
               
11		<y/>
               
12	</x>
               

            

The following query returns the first y element contained in each x element. It returns the elements on lines 6 and 10. The XPath processor finds all x elements. For each x element, the XPath processor then returns the first y element it finds.

x/y[1] 
               

            

The next query returns the first y element that is contained in an x element that is in the context node set. It returns the element on line 6. The XPath processor finds all y elements inside x elements. The XPath processor then returns the first element in that set.

(x/y)[1] 
               

            

The next query returns the empty set. The XPath processor finds the first x element. It then searches that first x element for the first y. Because the first x element does not contain a y element, this query returns the empty set.

x[1]/y[1] 
               

            

 
Free Stylus Studio XML Training:
W3C Member