XML/XQuery/where — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 08:25, 26 мая 2010

A where clause with multiple expressions

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>
  <car model="C">
    <id>0004</id>
    <name language="en">name 4</name>
  </car>
</order>

File: Query.xquery
for $prod in doc("Data.xml")//car
let $prodmodel := $prod/@model
where $prod/id > 100
      and starts-with($prod/name, "F")
      and exists($prod/colorChoices)
      and ($prodmodel = "B" or $prodmodel = "A")
return $prod



Embedding the where 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>
  <car model="C">
    <id>0004</id>
    <name language="en">name 4</name>
  </car>
</order>

File: Query.xquery
let $sortedProds := for $prod in doc("Data.xml")//car
                    where $prod/@model = "B" or $prod/@model = "C"
                    order by $prod/name
                    return $prod
for $sortedProd at $count in $sortedProds
return <p>{$count}. {data($sortedProd/name)}</p>

Output:
<?xml version="1.0" encoding="UTF-8"?>
<p>1. name 2</p>
<p>2. name 3</p>
<p>3. name 4</p>



where with or operator

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")//car
let $prodmodel := $prod/@model
where $prodmodel = "B" or $prodmodel = "A"
return $prod/name
Output:
<?xml version="1.0" encoding="UTF-8"?>
<name language="en">name 1</name>
<name language="en">name 2</name>
<name language="en">name 3</name>