A Final Example: Grouping

We conclude the chapter with an example that ties together most of the concepts that we've covered. Suppose we wish to display all articles for sale, grouped by seller. Listing 4.15 provides a query to do so.

Listing 4.15 Query to Display Articles for Sale, Grouped by Seller
 default element namespace = "http://www.example.com/auction" declare namespace x = "http://www.w3.org/1999/xhtml"  let $auction := $input/auction return  <x:html>   <x:head><x:title>Items by Seller</x:title></x:head>   <x:body>     <x:h1>Items by seller</x:h1>     {       for $id in distinct-values ($auction/articles/article/seller/@idref)       let $name := exactly-one($auction/users/user[@id = $id]/name)       order by $name       return         <x:p>{ $name }</x:p>         <x:table>{           for $article in one-or-more ($auction/articles/article[seller/@idref = $id])           return             <x:tr><x:td>{ $article/name }</x:td></x:tr>         }</x:table>     }   </x:body> </x:html> 

We use a path expression to find the idref of every seller of an article, and apply the function distinct-values to eliminate duplicates. For each identifier in this sequence, we find the name of the user with that identifier, order the sequence by the names , and display a paragraph containing the name of the user with that identifier and a table of all articles offered by that user.

Note that we iterate over identifiers extracted from the articles element, rather than from the users element; this guarantees that there will be at least one article displayed for each seller. This is just as well, because the schema for XHTML requires that a table element contain at least one row. Also, because of the integrity constraints on the data, every idref that appears in the article will correspond to exactly one user.

However, the static type inference rules are not clever enough to work this out; the types inferred for the articles and the name will both have zero-or-more occurrence indicators. This is fixed by applying the occurrence functions exactly-one and one-or-more at the appropriate points.

This example illustrates both the strengths and weaknesses of the XQuery static type system. The type system can catch many common errors at analysis time, including misspelled names in path expressions and constructors, and the use of an expression whose type is not compatible with the type required by the context in which the expression is used. Moreover, the type system guarantees that if a query does not raise a type error at analysis time, it will not raise a type error at evaluation time. Catching common errors at analysis time is especially important in applications that access large amounts of data, and it helps a programmer have confidence in the correctness of his program. The type system, however, is not always clever enough to infer the precise cardinality of an expression. This often occurs when a query joins or regroups data based on an unique key value. In these cases, the user must provide extra information to the type system by applying the occurrence functions.



XQuery from the Experts(c) A Guide to the W3C XML Query Language
Beginning ASP.NET Databases Using VB.NET
ISBN: N/A
EAN: 2147483647
Year: 2005
Pages: 102

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net