Conditional (if-then-else) Expressions

XQuery allows conditional expressions using the keywords if, then, and else. The syntax of a conditional expression is shown in Figure 3-1.

Syntax of a conditional expression

Figure 3-1. Syntax of a conditional expression

The expression after the if keyword is known as the test expression. It must be enclosed in parentheses. If the test expression evaluates to true, the value of the entire conditional expression is the value of the then expression. Otherwise, it is the value of the else expression.

Example 3-1 shows a conditional expression (embedded in a FLWOR).

Example 3-1. Conditional expression

Query
for $prod in (doc("catalog.xml")/catalog/product)
return if ($prod/@dept = 'ACC')
       then <accessoryNum>{data($prod/number)}</accessoryNum>
       else <otherNum>{data($prod/number)}</otherNum>
Results
<otherNum>557</otherNum>
<accessoryNum>563</accessoryNum>
<accessoryNum>443</accessoryNum>
<otherNum>784</otherNum>

If the then expression and else expression are single expressions, they are not required to be in parentheses. However, to return the results of multiple expressions, they need to be concatenated together using a sequence constructor. For example, if in Example 3-1 you wanted to return an accessoryName element in addition to accessoryNum, you would be required to separate the two elements by commas and surround them with parentheses, effectively constructing a sequence of two elements. This is shown in Example 3-2.

Example 3-2. Conditional expression returning multiple expressions

Query
for $prod in (doc("catalog.xml")/catalog/product)
return if ($prod/@dept = 'ACC')
       then (<accessoryNum>{data($prod/number)}</accessoryNum>,
            <accessoryName>{data($prod/name)}</accessoryName>)
       else <otherNum>{data($prod/number)}</otherNum>
Results
<otherNum>557</otherNum>
<accessoryNum>563</accessoryNum>
<accessoryName>Floppy Sun Hat</accessoryName>
<accessoryNum>443</accessoryNum>
<accessoryName>Deluxe Travel Bag</accessoryName>
<otherNum>784</otherNum>

The else keyword and the else expression are required. However, if you want the else expression to evaluate to nothing, it can simply be ( ) (the empty sequence).

Conditional Expressions and Effective Boolean Values

The test expression is interpreted as an xs:boolean value by calculating its effective Boolean value. This means that if it evaluates to the xs:boolean value false, the number 0 or NaN, a zero-length string, or the empty sequence, it is considered false. Otherwise, it is generally considered true. For example, the expression:

if (doc("order.xml")//item) then "Item List: " else ""

returns the string Item List: if there are any item elements in the order document. The if expression doc("order.xml")//item returns a sequence of element nodes rather than a Boolean value, but its effective Boolean value is true. Effective Boolean values are discussed in more detail in Chapter 11.

Nesting Conditional Expressions

You can also nest conditional expressions, as shown in Example 3-3. This provides an "else if" construct.

Example 3-3. Nested conditional expressions

Query
for $prod in (doc("catalog.xml")/catalog/product)
return if ($prod/@dept = 'ACC')
       then <accessory>{data($prod/number)}</accessory>
       else if ($prod/@dept = 'WMN')
            then <womens>{data($prod/number)}</womens>
            else if ($prod/@dept = 'MEN')
                 then <mens>{data($prod/number)}</mens>
                 else <other>{data($prod/number)}</other>
Results
<womens>557</womens>
<accessory>563</accessory>
<accessory>443</accessory>
<mens>784</mens>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.81.98