XML/XQuery/for

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

for and doc() function

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>
    <colorChoices>white gray</colorChoices>
    <desc>Our <i>favorite</i> shirt!</desc>
  </car>
</order>

File: Query.xquery

for $prod in doc("Data.xml")//car[@dept = "B"]
order by $prod/name
return $prod/name

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



for each loop

File: Data.xml
<?xml version="1.0"?>
<cars>
 <car>A</car>
 <car>B</car>
 <car>C</car>
 <car>D</car>
</cars>
File: Query.xquery
<cars>
{for $i in (1,2,3,4) return <car>{$i}</car>}
</cars>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<cars>
   <car>1</car>
   <car>2</car>
   <car>3</car>
   <car>4</car>
</cars>



for each loop and range

File: Data.xml

<?xml version="1.0"?>
<cars>
  <car>A</car>
  <car>B</car>
  <car>C</car>
  <car>D</car>
</cars>
File: Query.xquery
<cars>
{for $i in (1 to 5, 7, 8)
return <car>{$i}</car>}
</cars>

Output:
<?xml version="1.0" encoding="UTF-8"?>
<cars>
   <car>1</car>
   <car>2</car>
   <car>3</car>
   <car>4</car>
   <car>5</car>
   <car>7</car>
   <car>8</car>
</cars>



For each node in certain level

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

File: Query.xquery
<books>{
for $book in doc("Data.xml")/bib/book where $book/publisher = "Publisher 2" return
 element book {
  attribute year {$book/@year},
  element title {$book/title/text()}
  }
 }
</books>

Output:
<?xml version="1.0" encoding="UTF-8"?>
<books>
   <book year="2004">
      <title>title 2</title>
   </book>
</books>



for loop and node text

File: Data.xml
<?xml version="1.0"?>
<cars>
 <car>A</car>
 <car>B</car>
 <car>C</car>
 <car>D</car>
</cars>
File: Query.xquery
<cars>
{for $i in (1,2, doc("Data.xml")/cars/car/text(), 3, 4) return <car>{$i}</car>}
</cars>

Output:
<?xml version="1.0" encoding="UTF-8"?>
<cars>
   <car>1</car>
   <car>2</car>
   <car>A</car>
   <car>B</car>
   <car>C</car>
   <car>D</car>
   <car>3</car>
   <car>4</car>
</cars>



For stateCt

File: Data.xml

<!-- Order input docuCt -->
<order num="00299432" date="2006-09-15" cust="0221A">
  <item dept="A" num="1" quantity="1" color="navy"/>
  <item dept="B" num="2" quantity="1"/>
  <item dept="B" num="3" quantity="2"/>
  <item dept="C" num="4" quantity="1" color="white"/>
  <item dept="C" num="4" quantity="1" color="gray"/>
  <item dept="A" num="1" quantity="1" color="black"/>
</order>

File: Query.xquery
<title>Order Report</title>,
(for $item in doc("Data.xml")//item
 order by $item/@num
 return $item)

Output:
<?xml version="1.0" encoding="UTF-8"?>
<title>Order Report</title>
<item dept="A" num="1" quantity="1" color="navy"/>
<item dept="A" num="1" quantity="1" color="black"/>
<item dept="B" num="2" quantity="1"/>
<item dept="B" num="3" quantity="2"/>
<item dept="C" num="4" quantity="1" color="white"/>
<item dept="C" num="4" quantity="1" color="gray"/>



Multiple for clauses

File: Query.xquery
for $i in (1, 2)
for $j in ("a", "b")
return <oneEval>i is {$i} and j is {$j}</oneEval>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<oneEval>i is 1 and j is a</oneEval>
<oneEval>i is 1 and j is b</oneEval>
<oneEval>i is 2 and j is a</oneEval>
<oneEval>i is 2 and j is b</oneEval>



Multiple variable bindings in one for clause

File: Query.xquery
for $i in (1, 2), $j in ("a", "b")
return <oneEval>i is {$i} and j is {$j}</oneEval>

Output:
<?xml version="1.0" encoding="UTF-8"?>
<oneEval>i is 1 and j is a</oneEval>
<oneEval>i is 1 and j is b</oneEval>
<oneEval>i is 2 and j is a</oneEval>
<oneEval>i is 2 and j is b</oneEval>



Nested for each loop

File: Query.xquery
<cars>
{for $i in (1 to 5, 7, 8) return
 <group>{ for $a in (1 to ($i - 2)) return<car>{$a}</car>}
 </group>
 }
</cars>

Output:
<?xml version="1.0" encoding="UTF-8"?>
<cars>
   <group/>
   <group/>
   <group>
      <car>1</car>
   </group>
   <group>
      <car>1</car>
      <car>2</car>
   </group>
   <group>
      <car>1</car>
      <car>2</car>
      <car>3</car>
   </group>
   <group>
      <car>1</car>
      <car>2</car>
      <car>3</car>
      <car>4</car>
      <car>5</car>
   </group>
   <group>
      <car>1</car>
      <car>2</car>
      <car>3</car>
      <car>4</car>
      <car>5</car>
      <car>6</car>
   </group>
</cars>



Nested for loop

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
<ASaying>{
for $a in doc("Data.xml")/order/car[1]
  for $b in doc("Data.xml")/order/car[2]
  return  concat($a, " ", $b)
}
</ASaying>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<ASaying>
    0001
    name 1
   
    0002
    name 2
  </ASaying>



Use for loop

File: Data.xml
<?xml version="1.0"?>
<cars>
 <car>A</car>
 <car>B</car>
 <car>C</car>
 <car>D</car>
</cars>
File: Query.xquery
<cars>
{for $i in 1 to 5 return <car>{$i}</car>}
</cars>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<cars>
   <car>1</car>
   <car>2</car>
   <car>3</car>
   <car>4</car>
   <car>5</car>
</cars>