Sorting the Result

The 'O' in FLWOR stands for 'order by'. The previous XQuery expression did not contain explicit ordering instructions, and so the results were presented based on its order in the container. Over time, as the document set changes, the data will not maintain a constant order. Adding an explicit order by clause to the XQuery statement allows us to implement strict ordering:

dbxml> query '<html><body>
    <ul>
        {
        for $part in 
            (collection("parts.dbxml")/part[@number > 100 and @number < 105]) 
        order by xs:decimal($part/@number) descending 
        return 
            <li>{$part/description/string()}</li>
        }
    </ul></body></html>'

Query      - Starting query execution
Query      - parts.dbxml - R(@number,>,'100',<,'105') : [4] 103 104 105 106 
Query      - Finished eager query execution, time taken = 29.869ms
1 objects returned for eager expression '<html><body>
    <ul>
        {
        for $part in 
            (collection("parts.dbxml")/part[@number > 100 and @number < 105]) 
        order by xs:decimal($part/@number) descending
        return 
            <li>{$part/description/string()}</li>
        }
    </ul></body></html>'

dbxml> print
<html><body><ul>
<li>Description of 104</li>
<li>Description of 103</li>
<li>Description of 102</li>
<li>Description of 101</li>
</ul></body></html>

The following shows the previous HTML as displayed in a web browser:

The parts are now ordered in descending order, as expected.