Nested conditional expressions : if « XQuery « XML






Nested conditional expressions


File: Data.xml

<order>
  <car model="ACC">
    <id>0001</id>
    <name language="en">name 1</name>
  </car>
  <car model="BCC">
    <id>0002</id>
    <name language="en">name 2</name>
  </car>
  <car model="BCC">
    <id>0003</id>
    <name language="en">name 3</name>
  </car>
</order>


File: Query.xquery

for $prod in (doc("Data.xml")/order/car)
return if ($prod/@model = 'ACC')
       then <accessory>{data($prod/id)}</accessory>
       else if ($prod/@model = 'WMN')
            then <womens>{data($prod/id)}</womens>
            else if ($prod/@model = 'MEN')
                 then <mens>{data($prod/id)}</mens>
                 else <other>{data($prod/id)}</other>

Output:

<?xml version="1.0" encoding="UTF-8"?>
<accessory>0001</accessory>
<other>0002</other>
<other>0003</other>

 








Related examples in the same category

1.Conditional expression
2.Conditional expression returning multiple expressions
3.If statement in return clause
4.Converting values without a lookup table
5.If then else stateCt