XPath tutorial

Values of attributes as selection criteria

Values of attributes can be used as selection criteria. Function normalize-space removes leading and trailing spaces and replaces sequences of whitespace characters by a single space.

Example 1

XPath Expression: Select BBB elements which have attribute id with value b1
//BBB[@id='b1']
XML:
<?xml version="1.0">
     <AAA>
          <BBB id="b1"/>
          <BBB name="bbb"/>
          <BBB name="bbb"/>
     </AAA>

Example 2

XPath Expression: Select BBB elements which have attribute name with value 'bbb'

//BBB[@name='bbb']
XML:
<?xml version="1.0">
     <AAA>
          <BBB id="b1"/>
          <BBB name="bbb"/>
          <BBB name="bbb"/>
     </AAA>

Example 3

XPath Expression: Select BBB elements which have attribute name with value bbb, leading and trailing spaces are removed before comparison

//BBB[normalize-space(@name)='bbb']
XML:
<?xml version="1.0">
     <AAA>
          <BBB id="b1"/>
          <BBB name="bbb"/>
          <BBB name="bbb"/>
     </AAA>
C# code:
// BasicSyntax - /xpath/basicsyntax.cs
// Copyright 2003 by Johannes Roith

using System;
using System.Xml.XPath;

class BasicSyntax {
        static void Main() {
                
        }

}