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>
  <city>Cityville</city>
  <state>OH</state>
  <zip>12345</zip>
  <phone>123-456-7890</phone>
  <phone>987-654-3210</phone>
</PostAdr>

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:

/PostAdr/name/first

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:

/element-name[/element-name...][/|@attribute-name]

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:

/PostAdr/

/PostAdr/name/first/

/PostAdr/name@title

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:

/PostAdr/phone@type

consists of several nodes:

/PostAdr/

/phone/

@type