Quantified Expressions

A quantified expression determines whether some or all of the items in a sequence meet a particular condition. For example, if you want to know whether any of the items in an order are from the accessory department, you can use the expression shown in Example 6-8. This expression will return true.

Example 6-8. Quantified expression using the some keyword

some $dept in doc("catalog.xml")//product/@dept
satisfies ($dept = "ACC")

Alternatively, if you want to know if every item in an order is from the accessory department, you can simply change the word some to every, as shown in Example 6-9. This expression will return false.

Example 6-9. Quantified expression using the every keyword

every $dept in doc("catalog.xml")//product/@dept
satisfies ($dept = "ACC")

A quantified expression always evaluates to a Boolean value (true or false). As such, it is not useful for selecting the elements or attributes that meet certain criteria, but rather for simply determining whether any exist. Quantified expressions can generally be easily rewritten as FLWORs or even as simple path expressions. However, the quantified expression can be more compact and easier for implementations to optimize.

A quantified expression is made of several parts:

  • A quantifier (the keyword some or every)

  • One or more in clauses that bind variables to sequences

  • A satisfies clause that contains the test expression

The syntax of a quantified expression is shown in Figure 6-4.

Figure 6-4. Syntax of a quantified ...

Get XQuery now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.