Microsoft XML 2.5 SDK


 

Filters and Filter Patterns

[This is preliminary documentation and subject to change.]

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. That is, the expression "book[author]" means for every book element that is found, test whether it has an author subelement. Likewise, "book[author = 'Bob']" means 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"]

This pattern looks at author children for each book element until an author child is found with value "Bob". If any are found, the book element will be returned.

book[$any$ author="Bob"]

This pattern looks at author children for each book element. If there is an author element whose value is not "Bob", the corresponding book element will not be returned.

book[$all$ 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

Sample Data, XSL Pattern Examples