Grouping in XQuery

by Erik Wilde

One of the really convenient features introduced in XSLT 2.0 is Grouping. It is a typical second-generation change in a programming language: Not essential for the language itself (grouping can be done by hand using techniques such as the Muenchian Method), but required by many users and thus a useful addition to the language.

XQuery so far lacked support for grouping, with the same results as in XSLT 1.0: Grouping is cumbersome to implement and potentially slow when not implemented right. The recently published first XQuery 1.1 draft makes only few changes to XQuery 1.0, among them a Group By clause for FLWOR expressions. This should make the language quite a bit more useful, because grouping is such a frequently needed feature.

The other notable addition to XQuery 1.1 is the Window clause for FLWOR expressions. Windows come in two flavors, either tumbling or sliding windows. Windows clauses are somewhat similar to grouping, because they allow specific iterations over the binding sequence (not by grouping the items into new items, though, but by grouping them into windows, which always contain consecutive items). Windows are not that hard to understand when you look at the tumbling window examples and sliding window examples provided in the draft.

So far, XQuery 1.1 looks like a really small and useful set of changes to XQuery 1.0, which is good. Let's just hope it does not share the fate of XSLT 1.1, which was abandoned and never really made it into implementations. Are there any other changes which should be part of XQuery 1.1? The XQuery 1.1 Requirements list quite a few, including functions for date formatting, numeric formatting, and error processing and recovery. It will be interesting to see how these requirements will be addressed in future versions of the XQuery 1.1 draft.


3 Comments

Matthias Brantner
2008-07-16 02:24:11
Nice summary. The GroupBy clause and an error processing/recover mechanism (i.e. try-catch) are already implemented in the Zorba XQuery Processor - just in case somebody already wants to play around with it. Some example snippets can be found at http://www.zorba-xquery.com/doc/zorba-0.9.2/zorba/html
Dan McCreary
2008-07-16 09:29:08
The lack of numeric formatting is a big annoyance but most people in the eXist community have just created an XQuery wrapper function that calls XSLT 2.0 format-number function.
Jonathan Robie
2008-07-16 10:11:19
Nice summary - and I'm glad you like XQuery 1.1 so far.


You ask how many of the requirements will be addressed in future version sof the XQuery 1.1 draft. An easy way to answer that question is to look at each requirement to see if it is a MUST requirement (which we have to address or give a really compelling reason for not addressing), a SHOULD requirement (which we have to address or give a good reason for not addressing, or a MAY requirement (which we might or might not address). We purposely use that kind of language to help the user community know what to expect.


We'll continue to try to keep things simple, and extend the current design naturally.


Jonathan