Define variable in for statement : variable « XQuery « XML






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>

 








Related examples in the same category

1.Compare value
2.Binding multiple variables in a quantified expression
3.Attempting to use a counter variable
4.Using a positional variable in a for clause
5.Attempting to use a positional variable with a where clause
6.Converting values with a lookup table