If you have used XSLT and XPath, you have probably come across the position()
function, which enables you to number the items in a sequence, or to test whether the current item is the first or the last. FLWOR expressions do not maintain an implicit context in this way. Instead, you can declare an auxiliary variable to hold the current position, like this:
There are various keywords in the order by
clause that give you finer control over how the sorting takes place. The most important is the collation
: unfortunately, though, the way collations work is likely to be very product-dependent. The basic idea is that if you are sorting the index at the back of a book, or the names in a phone directory, then you need to apply rather more intelligent rules than simply sorting on the numeric Unicode code value of each character. Upper-case and lower-case variants of letters may need to be treated the same way, and accents on letters have some quite subtle rules in many languages. The working group defining XQuery settled on the simple rule that every collating sequence you might want has a name (specifically, a URI rather like a namespace URI), and it is up to each vendor to decide what collations to provide and how to name them.