Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XPath Reference

Filters and Filter Patterns

Constraints and branching can be applied to any collection by adding a filter clause, [pattern], to the collection. The filter is analogous to the SQL WHERE clause. The filter contains a pattern within it called the filter pattern. The filter pattern evaluates to a Boolean value and is tested for each element in the collection. Any elements in the collection failing the filter pattern test are omitted from the result collection.

For convenience, if a collection is placed within the filter, a Boolean TRUE is generated if the collection contains any members and a FALSE is generated if the collection is empty. An expression such as author/degree implies a collection-to-Boolean conversion function that evaluates to TRUE if there exists an <author> element with a child element named <degree>.

Note that any number of filters can appear at a given level of an expression. Empty filters are not allowed.

Filters are always evaluated with respect to a context. In other words, the expression book[author] means that for every <book> element that is found, test whether it has an <author> child element. Likewise, book[author = 'Bob'] means that for every <book> element that is found, test whether it has an <author> child element with the value Bob. One can examine the value of the context as well by using the period (.) character. For example, book[. = 'Trenton'] means that for every book that is found in the current context, test whether its value is Trenton.

Examples

Expression Refers to
book[excerpt] All <book> elements that contain at least one <excerpt> element.
book[excerpt]/title All <title> elements inside <book> elements that contain at least one <excerpt> element.
book[excerpt]/author[degree] All <author> elements that contain at least one <degree> element, and are inside of <book> elements that contain at least one <excerpt> element.
book[author/degree] All <book> elements that contain at least one <author> element with at least one <degree> child element.
book[excerpt][title] All <book> elements that contain at least one <excerpt> element and at least one <title> element.

See Also

Sample XML File for XPath Syntax (inventory.xml) | XPath Examples