Start of change

Conditional expressions

Conditional expressions use the keywords if, then, and else to evaluate one of two expressions based on whether the value of a test expression is true or false.

Syntax

Read syntax diagram
>>-if--(--test-expression--)--then--expression1--else--expression2-><

if
The keyword that directly precedes the test expression.
test-expression
An XQuery expression that determines which part of the conditional expression to evaluate.
then
If the effective Boolean value of test-expression is true, then the expression that follows this keyword is evaluated. The expression is not evaluated or checked for errors if the effective Boolean value of the test expression is false.
else
If the effective Boolean value of test-expression is false, then the expression that follows this keyword is evaluated. The expression is not evaluated or checked for errors if the effective Boolean value of the test expression is true.
expression1 and expression2
Any XQuery expression. If the expression includes a top-level comma operator, then the expression must be enclosed in parentheses.
If either the then or else condition branch contains an updating expression, then the conditional expression is an updating expression. An updating expression must be within the modify clause of a transform expression.
For an updating conditional expression, each branch must contain either an updating expression or an empty sequence. Based on the value of the test expression, either the then or else clause is selected and evaluated. The result of the conditional updating expression is a list of updates returned by the selected branch. The containing transform expression performs the updates after merging them with updates returned by other updating expressions within the modify clause of the transform expression.
Restriction: Start of changetest-expression, expression1, and expression2 cannot contain an FLWOR expression.End of change

Example

Suppose that the PORDER column in the PURCHASEORDER table contains the following data:

<ipo:purchaseOrder
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:ipo="http://www.example.com/IPO"
   orderDate="2008-12-01">
  <shipTo exportCode="1" xsi:type="ipo:UKAddress">
    <name>Helen Zoe</name>
    <street>55 Eden Street</street>
    <city>San Jose</city>
    <state>CA</state>
    <postcode>CB1 1JR</postcode>
  </shipTo>
  <shipTo exportCode="1" xsi:type="ipo:UKAddress">
    <name>Joe Lee</name>
    <street>66 University Avenue</street>
    <city>Palo Alto</city>
    <state>CA</state>
    <postcode>CB1 1JR</postcode>
  </shipTo>
  <billTo xsi:type="ipo:USAddress">
    <name>Robert Smith</name>
    <street>8 Oak Avenue</street>
    <city>Old Town</city>
    <state>PA</state>
    <zip>95819</zip>
  </billTo>
  <items>
    <item partNum="833-AA">
      <productName>Lapis necklace</productName>
      <quantity>1</quantity>
      <USPrice>99.95</USPrice>
      <ipo:comment>Want this for the holidays!</ipo:comment>
      <shipDate>2008-12-05</shipDate>
    </item>
    <item partNum="945-ZG">
      <productName>Sapphire Bracelet</productName>
      <quantity>2</quantity>
      <USPrice>178.99</USPrice>
      <shipDate>2009-01-03</shipDate>
    </item>
  </items>
</ipo:purchaseOrder>
In the following example, the query constructs a list of item elements. The value of the shipping element for an item is specified conditionally, based on whether the value of the USPrice element is less than 100. In this example, the test expression constructs an xs:decimal value from the value of the USPrice element. The xs:decimal function is used to force a decimal comparison.
SELECT XMLQUERY(
  'declare namespace ipo="http://www.example.com/IPO";
  for $i in $po/ipo:purchaseOrder/items/item
  return
  <item>
   {$i/productName}
   <shipping>
     { if (xs:decimal($i/USPrice) lt 100) then 5 else 10 }
   </shipping>
  </item>'
  PASSING PORDER as "po") 
  FROM PURCHASEORDER
The query returns the following result:
<item>
  <productName
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ipo="http://www.example.com/IPO">
    Lapis necklace
  </productName>
  <shipping>5</shipping>
</item>
<item>
  <productName
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ipo="http://www.example.com/IPO">
    Sapphire Bracelet
  </productName>
  <shipping>10</shipping>
</item>
End of change