For each node in certain level : for « XQuery « XML






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>

 








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 stateCt
10.for and doc() function