XQuery Sequence Functions: Manipulating and Analyzing Ordered Data
Learn how to effectively work with sequences in XQuery. This tutorial explores built-in XQuery functions for manipulating and analyzing ordered lists of data, covering techniques for counting, aggregating, filtering, sorting, and other sequence-based operations essential for efficient XML data processing.
Working with Sequences in XQuery
XQuery Sequence Functions
XQuery provides a set of built-in functions for working with sequences. Sequences are ordered lists of items. These functions are very helpful for manipulating and analyzing sequences of data extracted from XML documents. They allow for counting elements, calculating aggregates, filtering, sorting, and other operations on sequences.
Index | Function | Description |
---|---|---|
1 | count($seq as item()*) |
Returns the number of items in the sequence. |
2 | sum($seq as item()*) |
Returns the sum of numeric items in the sequence. |
3 | avg($seq as item()*) |
Returns the average of numeric items in the sequence. |
4 | min($seq as item()*) |
Returns the minimum value in the sequence. |
5 | max($seq as item()*) |
Returns the maximum value in the sequence. |
6 | distinct-values($seq as item()*) |
Returns a sequence containing only the unique values from the input sequence. |
7 | subsequence($seq as item()*, $start as xs:double, $length as xs:double) |
Returns a subsequence of the given sequence, starting at index `$start` and including `$length` items. |
8 | insert-before($seq as item()*, $pos as xs:integer, $inserts as item()*) |
Inserts items before a specified position in the sequence. |
9 | remove($seq as item()*, $pos as xs:integer) |
Removes the item at the specified position from the sequence. |
10 | reverse($seq as item()*) |
Reverses the order of items in the sequence. |
11 | index-of($seq as anyAtomicType()*, $target as anyAtomicType()) |
Returns the index (position) of the first occurrence of a target item in the sequence. |
12 | last() |
Returns the last item in the sequence (when used in a predicate). |
13 | position() |
Returns the position of the current item in the sequence (used in FLWOR expressions). |
Conclusion
XQuery's sequence functions provide a wide range of capabilities for manipulating and analyzing sequences of data. Mastering these functions is crucial for creating efficient and flexible XQuery expressions.