XQuery Sequences
XQuery Sequences represent an ordered collection of elements where the elements can be of similar or of different types.
Creating a sequence
An XQuery sequence is created using parentheses with strings inside quotation marks or double quotes or numbers. XML elements can also be used as elements of a sequence.
Items of a sequence can be iterated one by one, using index or by value.
For example:
let $items := ('orange', <apple/>, <fruit type="juicy"/>, <vehicle type="car">sentro</vehicle>, 1,2,3,'a','b',"abc")
let $count := count($items)
return
<result>
<count>{$count}</count>
<items>
{
for $item in $items
return <item>{$item}</item>
}
</items>
</result>
Obtaining the following result:
<result>
<count>10</count>
<items>
<item>orange</item>
<item>
<apple/>
</item>
<item>
<fruit type="juicy"/>
</item>
<item>
<vehicle type="car">Sentro</vehicle>
</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>a</item>
<item>b</item>
<item>abc</item>
</items>
</result>
Viewing the Items of a Sequence
Items of a sequence can be iterated one by one, using index or by value.
By Index
let $items := (1,2,3,4,5,6)
let $count := count($items)
return
<result>
<count>{$count}</count>
<items>
{
for $item in $items[2]
return <item>{$item}</item>
}
</items>
</result>
Output:
<result>
<count>6</count>
<items>
<item>2</item>
</items>
</result>
By Value
let $items := (1,2,3,4,5,6)
let $count := count($items)
return
<result>
<count>{$count}</count>
<items>
{
for $item in $items[. = (1,2,3)]
return <item>{$item}</item>
}
</items>
</result>
Output:
<result>
<count>6</count>
<items>
<item>1</item>
<item>2</item>
<item>3</item>
</items>
</result>