Intro to XPath

The term XPath describes the location of an entity in an XML document. You can think of an entity as being one of the things that make up an XML document (element tags, element content, attributes, and the like).

XPath’s notation looks similar to an RPG qualified data structure or the path to a document on your desktop’s C:\ drive. Take the following XML document:

<PostAdr residential="true">
  <name title="Mr.">Sample Resident</name>
  <street>999 Totally A Real Street</street>

In order to verbally describe the location of the <first> element in a qualified manner, we could say, “<first> is a child of <name> and <name> is a child of <PostAdr>.” For use in a program, the XPath describing that relationship would be:


The XPath /PostAdr/zip refers to the <zip> tag. An XPath of /PostAdr/name@title refers to the attribute title in <name> - @ is used instead of / when referring to an attribute. An RPG programmer will use XPaths to tell an XML parser which events should generate notifications as the parser crawls through the document.

Defining a DOM XPath

XPath values must take the following format:


In other words, XPath values must follow these rules:

  • An XPath must start with a slash
  • A slash must appear between element names
  • An XPath must not contain any whitespace
  • An XPath must end with a slash (if specifying an element) OR an @-symbol followed by an attribute name
Valid XPath Names:




Invalid XPath Names:

/PostAdr (no trailing slash)

/PostAdr /name/first (no trailing slash and whitespace exists)

/PostAdr/name/@title (slash before @ symbol)

Additionally, we may refer to the components of an XPath as XPath ‘segments’ – these may consist of a single node, or they may consist of multiple nodes. For instance, the following XPath:


consists of several nodes: