Reshaping the Result

XQuery is also useful when reshaping XML content. A common use for this feature is to restructure data into a display oriented dialect of XML, such as XHTML for presentation in a web browser.

Again, begin with the same value query seen earlier, modify it using XQuery and generate an XHTML version of the result suitable for display in a web browser:

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

Query      - Starting eager query execution
Query      - parts.dbxml - V(@number,>,'100') : [2899] 103 104 105 106 107 
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 ...
Query      - parts.dbxml - V(@number,<,'105') : [105] 2 3 4 5 6 7 8 9 10 11 
12 13 14 15 16 17 18 19 20 21 ...
Query      - parts.dbxml - n(V(@number,<,'105'),V(@number,>,'100')) : [4] 
103 104 105 106 
Query      - Finished eager query execution, time taken = 22.561ms
1 objects returned for eager expression '<html><body>
    <ul>
        {
        for $part in 
            (collection("parts.dbxml")/part[@number > 100 and @number < 105]) 
        return 
            <li>{$part/description/string()}</li>
        }
    </ul></body></html>'
    
dbxml> print
<html><body><ul>
<li>Description of 101</li>
<li>Description of 102</li>
<li>Description of 103</li>
<li>Description of 104</li>
</ul></body></html>

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

This XQuery introduces the XQuery FLWOR expression (For, Let, While, Order by, Return — sometimes written as FLWR or FLOWR). Note that XPath is still used in the query. Now, however, it is part of the overall FLWOR structure.

Note

Processing XML data in containers for display in dynamic web sites is best done using the language APIs most suitable to your web development rather than the command line tool we're using for examples.