Common Use of Predicate Filtering in XPath Expressions

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XPath Developer's Guide

Common Use of Predicate Filtering in XPath Expressions

There are a number of predicate functions, but you can use a few fairly simple XPath expressions to get surprisingly sophisticated results.

The following expression matches a given element by name without having to specify the name explicitly. This is useful for parameterization.

*[name(.)='myName'] or *[name(.)=$myNameVar]

The following expression returns even elements only. This is useful for creating alternating schemes.

*[position() mod 2 = 0]

The following expression matches all elements in the tree for which the id attribute corresponds to the idref attribute of the current node. This is useful when you have multiple types of information in an XML document (such as employees and offices), and need to relate one to the other.

//*[@id=./@idref]