XPath tutorial

Expresion in square brackets

Expresion in square brackets can further specify an element. A number in the brackets gives the position of the element in the selected set. The function last() selects the last element in the selection.

Example 1

XPath Expression: Select the first BBB child of element AAA
/AAA/BBB[1]
XML:
<?xml version="1.0">
     <AAA>
          <BBB/>
          <BBB/>
          <BBB/>
          <BBB/>
     </AAA>

Example 2

XPath Expression: Select the last BBB child of element AAA

/AAA/BBB[last()]
XML:
<?xml version="1.0">
     <AAA>
          <BBB/>
          <BBB/>
          <BBB/>
          <BBB/>
     </AAA>
C# code:
// BasicSyntax - /xpath/basicsyntax.cs
// Copyright 2003 by Johannes Roith

using System;
using System.Xml.XPath;

class BasicSyntax {
        static void Main() {
                
        }

}