XML/XQuery/variable

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

Attempting to use a counter variable

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 $count := 0
for $prod in doc("Data.xml")//car[@model = ("B", "A")]
let $count := $count + 1
return <p>{$count}. {data($prod/name)}</p>

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



Attempting to use a positional variable with a 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>
</order>
File: Query.xquery
for $prod at $count in doc("Data.xml")//car
where $prod/@model = ("B", "C")
order by $prod/name
return <p>{$count}. {data($prod/name)}</p>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<p>2. name 2</p>
<p>3. name 3</p>



Binding multiple variables in a quantified expression

File: Query.xquery
some $i in (1 to 3), $j in (10, 11)
  satisfies $j - $i = 7
Output:
<?xml version="1.0" encoding="UTF-8"?>true



Compare value

File: Data.xml
<?xml version="1.0"?>
<bib>
  <book year="1988">
    <title>title 1</title>
  </book>
  <book year="2004">
    <title>title 2</title>
  </book>
</bib>

File: Query.xquery
xquery version "1.0";
declare boundary-space strip;
<myNewBib>{
doc("Data.xml")/bib/book[@year > 2005]
}</myNewBib>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<myNewBib/>



Converting values with 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>
  <car model="C">
    <id>0004</id>
    <name language="en">name 4</name>
  </car>
</order>
File: Query.xquery
let $modelNames := <modelNames>
                    <model code="B" name="Accessories"/>
                    <model code="C" name="Menswear"/>
                    <model code="A" name="Womens"/>
                  </modelNames>
let $cat := doc("Data.xml")/order
for $model in distinct-values($cat/car/@model)
return <li>Department: {data($modelNames/model[@code = $model]/@name)
                  } ({$model})</li>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<li>Department: Womens (A)</li>
<li>Department: Accessories (B)</li>
<li>Department: Menswear (C)</li>



Define variable in for statement

File: Data.xml
<?xml version="1.0"?>
<bib>
  <book year="1988">
    <title>title 1</title>
  </book>
  <book year="2004">
    <title>title 2</title>
  </book>
</bib>

File: Query.xquery
<books>{
  for $book in doc("Data.xml")/bib/book
  let $t := $book/title/text() order by $t return
  <book><title>{$t}</title></book>
}</books>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<books>
   <book>
      <title>title 1</title>
   </book>
   <book>
      <title>title 2</title>
   </book>
</books>



Using a positional variable in a for 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
for $prod at $count in doc("Data.xml")//car[@model = ("B", "A")]
return <p>{$count}. {data($prod/name)}</p>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<p>1. name 1</p>
<p>2. name 2</p>
<p>3. name 3</p>