DB2 10.5 for Linux, UNIX, and Windows

Predicates

A predicate filters a sequence by retaining the qualifying items. A predicate consists of an expression, called a predicate expression, that is enclosed in square brackets ([]).

The predicate expression is evaluated once for each item in the sequence, with the selected item as the context item. Each evaluation of the predicate expression returns an xs:boolean value called the predicate truth value. Those items for which the predicate truth value is true are retained, and those for which the predicate truth value is false are discarded.

The following rules are used to determine the predicate truth value:
  • If the predicate expression returns a non-numeric value, the predicate truth value is the effective boolean value of the predicate expression.
  • If the predicate expression returns a numeric value, the predicate truth value is true only for the item whose position in the sequence is equal to that numeric value. For other items, the predicate truth value is false. This kind of predicate is called a numeric predicate or positional predicate. For example, in the expression $products[5], the numeric predicate [5] retains only the fifth item in the sequence bound to the variable $products.
    Important: The item that is selected from a sequence by a numeric predicate is deterministic only if the sequence has a deterministic order.
Tip: The behavior of a predicate depends on whether the predicate expression returns a numeric value or not, which might not be clear from looking at the predicate expression. You can force a predicate to use an effective boolean value by using the fn:boolean function, as in [fn:boolean(PredicateExpression)]. Alternatively, you can force a predicate to behave like a positional predicate by using the fn:position function, as in [fn:position() eq PredicateExpression].
The following examples have predicates:
  • chapter[2] selects the second chapter element that is a child of the context node.
  • descendant::toy[@color = "Red"] selects all of the descendants of the context node that are elements named toy and have a color attribute with the value "Red".
  • employee[secretary][assistant] selects all of the employee children of the context node that have both a secretary child element and an assistant child element.
  • (<cat />, <dog />, 47, <zebra />)[2] returns the element <dog />.