Share via


Filters and Filter Patterns

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

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 with a child element 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".

If a filter compares values of elements and the filter is to be applied to more than one child element, you might want to use the any and all keywords. When these keywords are not specified, only the first matching child is used for the comparison.

This pattern looks for the first author child of each book element. If the value of the author child is "Bob", the corresponding book element will be returned.

book[author="Bob"]

Examples

Find all books that contain at least one excerpt element.

book[excerpt]

Find all titles of books that contain at least one excerpt element.

book[excerpt]/title

Find all authors of books where the book contains at least one excerpt and the author has at least one degree.

book[excerpt]/author[degree]

Find all books that have authors with at least one degree.

book[author/degree]

Find all books that have an excerpt and a title.

book[excerpt][title]

See Also

Concepts

Sample Data
XPath Examples