XPath tutorial

name() function

Function name() returns name of the element, the starts-with function returns true if the first argument string starts with the second argument string, and the contains function returns true if the first argument string contains the second argument string.

Example 1

XPath Expression: Select all elements with name BBB, equivalent with //BBB
//*[name()='BBB']
XML:
<?xml version="1.0">
     <AAA>
          <BCC>
               <BBB/>
               <BBB/>
               <BBB/>
          </BCC>
          <DDB>
               <BBB/>
               <BBB/>
          </DDB>
          <BEC>
               <CCC/>
               <DBD/>
          </BEC>
     </AAA>

Example 2

XPath Expression: Select all elements name of which starts with letter B

//*[starts-with(name(),'B')]
XML:
<?xml version="1.0">
     <AAA>
          <BCC>
               <BBB/>
               <BBB/>
               <BBB/>
          </BCC>
          <DDB>
               <BBB/>
               <BBB/>
          </DDB>
          <BEC>
               <CCC/>
               <DBD/>
          </BEC>
     </AAA>

Example 3

XPath Expression: Select all elements name of which contain letter C

//*[contains(name(),'C')]
XML:
<?xml version="1.0">
     <AAA>
          <BCC>
               <BBB/>
               <BBB/>
               <BBB/>
          </BCC>
          <DDB>
               <BBB/>
               <BBB/>
          </DDB>
          <BEC>
               <CCC/>
               <DBD/>
          </BEC>
     </AAA>
C# code:
// BasicSyntax - /xpath/basicsyntax.cs
// Copyright 2003 by Johannes Roith

using System;
using System.Xml.XPath;

class BasicSyntax {
        static void Main() {
                
        }

}