Grouping

If you are used to SQL, then you might have been wondering what the equivalent to its DISTINCT and GROUP BY keywords is in XQuery FLOWR expressions. Well, SQL does not have one.

You can, however, get a fair amount of mileage from the distinct-values() function. Here is a query that groups videos according to who directed them:


              <movies>
               

                 {for $d in distinct-values(//director) return
               

                    <director name="{$d}">
               

                    { for $v in //video[director = $d] return
               

                       <title>{$v/title}</title>
               

                    }
               

                    </director>
               

                 }
               

              </movies>
               

            

This is not an ideal solution: apart from anything else, it depends heavily on the ability of the query processor to optimize the two nested loops to give good performance. But for the time being, this is all there is. This is an area where vendors are very likely to offer extensions to the language as defined by W3C.

Grouping was a notoriously weak point of XSLT 1.0, and the problem has been addressed with considerable success in the 2.0 version of the language. XQuery will likely follow suit.

XML Videos

Stylus Studio online XML video demonstrations illustrate how easy it is to learn advanced XML editing techniques. Now playing: Java Code Generation, Convert to XML, Grid View, and many other informative video demonstrations.

Using Stylus Studio's Database-to-XML Editor to Query and Update Relational Databases using SQL/XML

Watch and learn how easy it is to define relational-to-XML mappings for getting XML out of relational databases and updating databases from XML using industry-standard SQL/XML extensions and Stylus Studio's DB-to-XML data-source editor.

XSL-List Blog

XSL-List is a discussion forum on the Extensible Stylesheet Language (XSL), covering the XSL specification, XSL processors, tools, user questions, trends and more. Bookmark this RSS or ATOM feed now!

Stylus Most Wanted

 
Free Stylus Studio XML Training:
W3C Member