Filtering Results of Queries

Sometimes you want to retrieve only those elements that meet a certain condition. For example, you might want information about a particular book. In this case, you can include a filter in your query. You enclose filters in brackets ( [ ] ).

The following figure shows how the XPath processor interprets a query with a filter:

This query checks each book element to determine whether it has a title child element whose value is "History of Trenton". If it does, the query returns the book element. Using the sample data, this query returns the second book element.

The following topics provide details about filters:

Quotation Marks in Filters

Suppose you define the following filter:

[title="History of Trenton"] 

If you need to specify this filter as part of an attribute value, use single quotation marks instead of double quotation marks. This is because the attribute value itself is (usually) inside double quotation marks. For example:

<xsl:value-of select="/bookstore/book[title='History of Trenton']"> 

Strings within an expression may contain special characters such as [, {, &, `, /, and others, as long as the entire string is enclosed in double quotes ("). When the string itself contains double quotes, you may enclose it in single quotes ('). When a string contains both single and double quotes, you must handle these segments of the string as if they were individual phrases, and concatenate them.

More Filter Examples

Following is another example of a query with a filter clause. This query returns book elements if the price of the book is greater than 25 dollars:

/bookstore/book[price > 25] 

The next query returns author elements if the author has a degree:


The next query returns the date attributes that match "3/1/00":


The next query returns manufacturer elements in the current context for which the rwdrive attribute of the model is the same as the vendor attribute of the manufacturer:

manufacturer[model/@rwdrive = @vendor] 

How the XPath Processor Evaluates a Filter

You can apply constraints and branching to a query by specifying a filter clause. The filter contains a query, which is called the subquery. The subquery evaluates to a Boolean value, or to a numeric value. The XPath processor tests each element in the current context to see if it satisfies the subquery. The result includes only those elements that test true for the subquery.

The XPath processor always evaluates filters with respect to a context. For example, the expression book[author] means for every book element that is found in the current context, determine whether the book element contains an author element. For example, the following query returns all books in the current context that contain at least one excerpt:


The next query returns all titles of books in the current context that have at least one excerpt:


Multiple Filters

You can specify any number of filters in any level of a query expression. Empty filters
( [ ] ) are not allowed.

A query that contains one or more filters returns the rightmost element that is not in a filter clause. For example:


The previous query returns author elements. It does not return degree elements. To be exact, this query returns all authors who have at least one degree if the author is of a book for which the document contains at least one excerpt. In other words, for all books in the current context that have excerpts, this query finds all authors with degrees.

The following query finds each book child of the current context that has an author with at least one degree:


The next query returns all books in the current context that have an excerpt and a title:


Filters and Attributes

Following is a query that finds all child elements of the current context with specialty attributes:


The following query returns all book children in the current context with style attributes:


The next query finds all book child elements in the current context in which the value of the style attribute of the book is equal to the value of the specialty attribute of the bookstore element:

book[/bookstore/@specialty = @style]  

Free Stylus Studio XML Training:
W3C Member