XPath tutorial

string-length() function

The string-length function returns the number of characters in the string. You must use < as a substitute for < and > as a substitute for > .

Example 1

XPath Expression: Select elements with three-letter name
//*[string-length(name()) = 3]
XML:
<?xml version="1.0">
     <AAA>
          <Q/>
          <SSSS/>
          <BB/>
          <CCC/>
          <DDDDDDDD/>
          <EEEE/>
     </AAA>

Example 2

XPath Expression: Select elements name of which has one or two characters

//*[string-length(name()) < 3]
XML:
<?xml version="1.0">
     <AAA>
          <Q/>
          <SSSS/>
          <BB/>
          <CCC/>
          <DDDDDDDD/>
          <EEEE/>
     </AAA>

Example 3

XPath Expression: Select elements with name longer than three characters

//*[string-length(name()) > 3]
XML:
<?xml version="1.0">
     <AAA>
          <Q/>
          <SSSS/>
          <BB/>
          <CCC/>
          <DDDDDDDD/>
          <EEEE/>
     </AAA>
C# code:
// BasicSyntax - /xpath/basicsyntax.cs
// Copyright 2003 by Johannes Roith

using System;
using System.Xml.XPath;

class BasicSyntax {
        static void Main() {
                
        }

}