XML/XQuery/let

Материал из Web эксперт
Версия от 11:25, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Adding a let clause

   <source lang="xml">


File: Data.xml <order>

 <car model="A">
   <id>0001</id>
   <name language="en">name 1</name>
   <colorChoices>navy black</colorChoices>
 </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 $car in doc("Data.xml")/order/car let $name := $car/name where $car/@model = "B" order by $name return $name Output: <?xml version="1.0" encoding="UTF-8"?> <name language="en">name 2</name> <name language="en">name 3</name>

</source>
   
  


Intermingled for and let clauses

   <source lang="xml">

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 $doc := doc("Data.xml") for $prod in $doc//car let $prodmodel := $prod/@model let $prodName := $prod/name where $prodmodel = "B" or $prodmodel = "A" return $prodName 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>

</source>
   
  


Let stateCt

   <source lang="xml">

File: Data.xml

<order>

 <car dept="A">
   <number>1</number>
   <name language="en">name 1</name>
 </car>
 <car dept="B">
   <number>2</number>
   <name language="en">name 2</name>
 </car>
 <car dept="B">
   <number>3</number>
   <name language="en">name 3</name>
 </car>
 <car dept="C">
   <number>4</number>
   <name language="en">name 4</name>
 </car>

</order>

File: Query.xquery

let $doc := doc("Data.xml") return $doc//number | $doc//name

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

</source>
   
  


Using a let clause with a range expression

   <source lang="xml">

File: Query.xquery let $i := (1 to 3) return <oneEval>{$i}</oneEval> Output: <?xml version="1.0" encoding="UTF-8"?> <oneEval>1 2 3</oneEval>

</source>