for and doc() function : for « XQuery « XML






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>

 








Related examples in the same category

1.Multiple for clauses
2.Multiple variable bindings in one for clause
3.Nested for loop
4.for loop and node text
5.Use for loop
6.for each loop and range
7.for each loop
8.Nested for each loop
9.For each node in certain level
10.For stateCt