XML/XQuery/if — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:25, 26 мая 2010
Содержание
Conditional expression
File: Data.xml
<order>
<car model="A">
<id>0001</id>
<name language="en">name 1</name>
</car>
<car model="B">
<id>0002</id>
<name language="en">name 2</name>
</car>
<car model="B">
<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 <accessoryid>{data($prod/id)}</accessoryid>
else <otherid>{data($prod/id)}</otherid>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<otherid>0001</otherid>
<otherid>0002</otherid>
<otherid>0003</otherid>
Conditional expression returning multiple expressions
File: Data.xml
<order>
<car model="ACC">
<id>0001</id>
<name language="en">name 1</name>
<colorChoices>navy black</colorChoices>
</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 (<accessoryid>{data($prod/id)}</accessoryid>,
<accessoryName>{data($prod/name)}</accessoryName>)
else <otherid>{data($prod/id)}</otherid>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<accessoryid>0001</accessoryid>
<accessoryName>name 1</accessoryName>
<otherid>0002</otherid>
<otherid>0003</otherid>
Converting values without a lookup table
File: Data.xml
<order>
<car model="A">
<id>0001</id>
<name language="en">name 1</name>
</car>
<car model="B">
<id>0002</id>
<name language="en">name 2</name>
</car>
<car model="B">
<id>0003</id>
<name language="en">name 3</name>
</car>
</order>
File: Query.xquery
let $cat := doc("Data.xml")/order
for $model in distinct-values($cat/car/@model)
return <li>Department: {if ($model = "B")
then "Accessories"
else if ($model = "C")
then "Menswear"
else if ($model = "A")
then "Womens"
else ()
} ({$model})</li>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<li>Department: Womens (A)</li>
<li>Department: Accessories (B)</li>
If statement in return clause
File: Data.xml
<order>
<car model="A">
<id>0001</id>
<name language="en">name 1</name>
</car>
<car model="B">
<id>0002</id>
<name language="en">name 2</name>
</car>
<car model="B">
<id>0003</id>
<name language="en">name 3</name>
</car>
</order>
File: Query.xquery
let $prods := doc("Data.xml")//car
let $idProds := count($prods)
for $prod at $count in $prods
return if ($count = $idProds)
then concat($prod/name,".")
else concat($prod/name,",")
Output:
<?xml version="1.0" encoding="UTF-8"?>name 1, name 2, name 3.
If then else stateCt
File: Data.xml
<prices>
<priceList effDate="2006-11-15">
<prod num="1">
<price currency="USD">29.99</price>
<discount type="CLR">10.00</discount>
</prod>
<prod num="2">
<price currency="USD">69.99</price>
</prod>
<prod num="3">
<price currency="USD">39.99</price>
<discount type="CLR">3.99</discount>
</prod>
</priceList>
</prices>
File: Query.xquery
let $bargains := doc("Data.xml")/prices/priceList/prod[price < 30]
return if ($bargains)
then <bargain-bin>{$bargains}</bargain-bin>
else ()
Output:
<?xml version="1.0" encoding="UTF-8"?>
<bargain-bin>
<prod num="1">
<price currency="USD">29.99</price>
<discount type="CLR">10.00</discount>
</prod>
</bargain-bin>
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>