<!--

1. Get rid of the "might add or delete use cases" stuff from status
2. DONE: Use an updated Meltonesque status section
3. Static typing

--><spec id="spec-top" w3c-doctype="wd">
	<header id="spec-header">
        <title>XML Query (XQuery) 1.1 Use Cases</title>
        <w3c-designation>WD-xquery-11-use-cases</w3c-designation>
        <w3c-doctype>W3C Working Draft</w3c-doctype>
        <pubdate>
            <day>27</day>
            <month>March</month>
            <year>2008</year>
        </pubdate>
        <publoc>
            <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/2008/WD-xquery-11-use-cases-20080327/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://www.w3.org/TR/2008/WD-xquery-11-use-cases-20080327/</loc>
        </publoc>
        <altlocs>
            <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/2008/WD-xquery-11-use-cases-20080327/xquery-use-cases.xml" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XML</loc>
        </altlocs>
        <latestloc>
            <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/xquery-11-use-cases/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://www.w3.org/TR/xquery-11-use-cases/</loc>
        </latestloc>
        <prevlocs>
            <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/2005/WD-xquery-use-cases-20050915/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"/>
        </prevlocs>
        <authlist>
            <author>
                <name>Tim Kraska</name>
                <affiliation>FLWOR Foundation</affiliation>
                <email xmlns:xlink="http://www.w3.org/1999/xlink" href="tim@flworfound.org" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">tim@flworfound.org</email>
            </author>
        </authlist>

<!--* Common status section for QT specs.
    * Use is currently not required, but it simplifies things.
    * 
    * Revisions:
    * 2007-01-15 : CMSMcQ : made file, to simplify publication of Rec.
    * 2008-02-15 : JimMelton : cloned from MSM's REC-only material
                     to generalize for all stages
    *-->

    <status id="status">

<!-- ************************************************************************** -->
<!-- * All Status sections must start with the standard boilerplate paragraph * -->
<!-- *   This entity is defined in status-entities.dtd                        * -->
<!-- ************************************************************************** -->
      <p><emph>This section describes the status of this
         document at the time of its publication.
         Other documents may supersede this document.
         A list of current W3C publications and the latest
         revision of this technical report can be found in the
         <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">W3C technical reports index</loc>
         at http://www.w3.org/TR/.</emph></p>

<!-- ************************************************************************** -->
<!-- * QT publishes suites of documents, which must be described in the       * -->
<!--     Status section of each document within such a suite.                 * -->
<!-- *   This entity is defined in the host document.                         * -->
<!-- ************************************************************************** -->
      

<!-- ************************************************************************** -->
<!-- * There is a lot of detailed customization based on the document stage   * -->
<!-- *   This entity is defined in the host document.                         * -->
<!-- ************************************************************************** -->
      <p>This is a <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/2005/10/Process-20051014/tr.html#first-wd" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">First Public Working Draft</loc> as described in the <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/2004/02/Process-20040205/tr.html" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">Process Document</loc>. 
It has been developed by the W3C <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/XML/Query" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XML Query Working Group</loc>,
which is part of the <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/XML/Activity" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XML Activity</loc>.
The Working Group expects to eventually publish this document as a Working Group Note.</p>

<!-- ************************************************************************** -->
<!-- * CR documents must cite features at risk                                * -->
<!-- *   This entity is defined in the host document.                         * -->
<!-- ************************************************************************** -->
      

<!-- ************************************************************************** -->
<!-- * Every Status section must have a customized paragraph                  * -->
<!-- *   This entity is defined completely in the host document.              * -->
<!-- ************************************************************************** -->
      <p>This document provides
a number of use cases designed to evaluate XQuery 1.1, the requirements for which
are specified in <bibref ref="xquery-11-requirements"/>. 
Organizations and individuals should review this document to ascertain whether or not 
adequate coverage of the requirements is provided by these use cases. </p>
<p>The Working Group wishes to emphasize that this draft of this Use Cases
document does not address all of the requirements identified in
<bibref ref="xquery-11-requirements"/>; future drafts of this document
will provide use cases for additional requirements. 
Furthermore, the syntax used in this document to present solutions to the Use Cases
is conjectural and is based on work that has not yet advanced to First Public Working Draft
status.</p>

<!-- ************************************************************************** -->
<!-- * CR docs should, and PR docs must, have a pointer to an implementation  * -->
<!-- *   report.  We also want to point to the test suite.                    * -->
<!-- *   This entity is defined in the host document.                         * -->
<!-- ************************************************************************** -->
      

<!-- ************************************************************************** -->
<!-- * The Status section should point to a changelog                         * -->
<!-- *   This entity is defined in the host document.                         * -->
<!-- ************************************************************************** -->
      

<!-- ************************************************************************** -->
<!-- * The Status section must tell readers where to send comments            * -->
<!-- *   This entity is defined in status-entities.dtd                        * -->
<!-- ************************************************************************** -->
      <p>Please report errors in this document using W3C's
         <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Bugs/Public/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">public Bugzilla system</loc>
         (instructions can be found at
         <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/XML/2005/04/qt-bugzilla" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://www.w3.org/XML/2005/04/qt-bugzilla</loc>).
         If access to that system is not feasible, you may send your comments
         to the W3C XSLT/XPath/XQuery public comments mailing list,
         <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:public-qt-comments@w3.org" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">public-qt-comments@w3.org</loc>.
         It will be very helpful if you include the string 
         “[XQuery11UC]”
         in the subject line of your report, whether made in Bugzilla or in email.
         Each Bugzilla entry and email message should contain only one error report.
         Archives of the comments and responses are available at
         <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://lists.w3.org/Archives/Public/public-qt-comments/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://lists.w3.org/Archives/Public/public-qt-comments/</loc>. </p>

<!-- ************************************************************************** -->
<!-- Status sections must state the stability (not stable, or REC) of the document -->
<!-- *   This entity is defined in the host document.                         * -->
<!-- ************************************************************************** -->
      <p>Publication as a Working Draft
does not imply endorsement by the W3C Membership. 
This is a draft document and may be updated, replaced or obsoleted
by other documents at any time. 
It is inappropriate to cite this document as other than work in progress.</p>

<!-- ************************************************************************** -->
<!-- * Finally, all Status sections must end with the appropriate IPR para    * -->
<!-- *   This entity is defined in status-entities.dtd                        * -->
<!-- ************************************************************************** -->
      
<p>This document was produced by a group operating under the
  <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Consortium/Patent-Policy-20040205/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">5 February 2004
  W3C Patent Policy</loc>.
  W3C maintains a 
  <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/2004/01/pp-impl/18797/status#disclosures" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"> public list of any patent disclosures</loc>
  made in connection with the deliverables of the group; 
  that page also includes instructions for disclosing a patent.
  An individual who has actual knowledge of a patent 
  which the individual believes contains
  <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">Essential Claim(s)</loc>
  must disclose the information in accordance with
  <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
  section 6 of the W3C Patent Policy</loc>. </p>


    </status>


<!--
        <status>

            <p>
                <emph>This section describes the status of this document at the time of its
                    publication. Other documents may supersede this document. A list of current W3C
                    publications and the latest revision of this technical report can be found in
                    the <loc href="http://www.w3.org/TR/">W3C technical reports index</loc> at
                    http://www.w3.org/TR/.</emph>
            </p>


            <p>This version of the Use Cases document corresponds to the XQuery 1.1 Working Draft
                released on XXXX March 2008. The queries in this document have been parsed using a
                parser generated from the same grammar used to create the documentation for the
                XQuery 1.1 Working Draft. </p>

            <p>This is a public W3C Working Draft for review by W3C Members and other interested
                parties. Publication as a Working Draft does not imply endorsement by the W3C
                Membership. This is a draft document and may be updated, replaced or obsoleted by
                other documents at any time. It is inappropriate to cite this document as other than
                work in progress.</p>

            <p>
                <phrase role="xquery">This document was produced by the <loc
                        href="http://www.w3.org/XML/Query">XML Query Working Group</loc> (WG) (part
                    of the <loc href="http://www.w3.org/XML/Activity">XML Activity</loc>).</phrase>
                <phrase role="xpath">This document was produced jointly by the <loc
                        href="http://www.w3.org/XML/Query">XML Query Working Group</loc> and the
                        <loc href="http://www.w3.org/Style/XSL">XSL Working Group</loc>, both of
                    which are part of the <loc href="http://www.w3.org/XML/Activity">XML
                    Activity</loc>.</phrase>
            </p>




            <p>Comments on this document are invited and should be made in W3C's <loc
                    href="http://www.w3.org/Bugs/Public/">public Bugzilla system</loc> (instructions
                can be found at <loc href="http://www.w3.org/XML/2005/04/qt-bugzilla"
                    >http://www.w3.org/XML/2005/04/qt-bugzilla</loc>). If access to that system is
                not feasible, you may send your comments to the W3C XSLT/XPath/XQuery mailing list,
                    <loc href="mailto:public-qt-comments@w3.org">public-qt-comments@w3.org</loc>. It
                will be very helpful if you include the string [XQueryUseCases] in the subject line
                of your comment, whether made in Bugzilla or in email. Each Bugzilla entry and email
                message should contain only one comment. Archives of the comments and responses are
                available at <loc href="http://lists.w3.org/Archives/Public/public-qt-comments/"
                    >http://lists.w3.org/Archives/Public/public-qt-comments/ </loc>.</p>

            <p>This document was produced by a group operating under the <loc
                    href="http://www.w3.org/Consortium/Patent-Policy-20040205/">5 February 2004 W3C
                    Patent Policy</loc>. This document is informative only. W3C maintains a <loc
                    role="disclosure" href="http://www.w3.org/2002/08/xmlquery-IPR-statements"
                    >public list of any patent disclosures</loc> made in connection with the
                deliverables of the group; that page also includes instructions for disclosing a
                patent. An individual who has actual knowledge of a patent which the individual
                believes contains <loc
                    href="http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential"
                    >Essential Claim(s)</loc> must disclose the information in accordance with <loc
                    href="http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure"
                    >section 6 of the W3C Patent Policy</loc>. </p>
        </status>
-->

        <abstract>
            <p>This document specifies usage scenarios for XML Query (XQuery) 1.1.</p>
        </abstract>
        <langusage>
            <language id="EN">English</language>
        </langusage>
        <revisiondesc>
            <p>TK, 30 January 2008: Addes first group-by and forseq UC.</p>
        </revisiondesc>
    </header>
    <body>
        <div1 id="ucforxq11">
            <head>Use Cases for XML Query 1.1</head>

            <p>The use cases listed below were created by the XML Query Working Group to illustrate
                important applications for an XML query language. Each use case is focused on a
                specific application area, and contains a Document Type Definition (DTD) and example
                input data. Each use case specifies a set of queries that might be applied to the
                input data, and the expected results for each query. Since the English description
                of each query is concise, the expected results form an important part of the
                definition of each query, specifying the expected output format. These use cases
                were originally published as part of the <bibref ref="xquery-11-requirements"/>
                document, without solutions in concrete query languages. Now it is being republished
                with solutions for <bibref ref="xquery"/>.</p>

            <p>The input environment for each use case is stated in its Document Type Definition
                (DTD) section. All of these use cases assume that input is provided in the form of
                one or more documents with specific names. For instance, the authors in a document
                may be accessed with expressions like this:</p>
            <eg xml:space="preserve">doc("bib.xml")//author</eg>
            <p>Some implementations of XQuery bind input to external variables. If the environment
                has bound the external variable $b to the same document used in the above query,
                this expression would return the same set of authors:</p>
            <eg xml:space="preserve">$b//author</eg>
            <p>Some implementations of XQuery predefine a single 'context item', which is available
                at the root level of a query, and which is used to resolve paths that begin with a
                leading slash. In such an implementation, if the context item is bound to document
                node of the same well-formed document used in the previous examples, this expression
                would return the same set of authors:</p>
            <eg xml:space="preserve">//author</eg>
            <p>Several implementors have asked us to make the queries from these use cases
                available in a separate file in order to make it easier for them to test their parsers. These
                queries may be found in <bibref ref="UseCaseQueries"/>. Also, the queries from the
                XQuery specification itself have been made available in <bibref ref="XQueryQueries"/>. </p>
            <p>To make output more readable, the output of queries has been formatted using
                whitespace which may not be returned by a query processor. This whitespace should
                not be considered normative for the correctness of results.</p>

            <p>These queries were tested with a dynamic implementation of XQuery. Some queries may
                require additional type declarations to be used with an implementation that
                implements the Static Typing feature. </p>

</div1>


<div1 id="groupby"> 
<head>Use Case "group by": Queries which require grouping</head>
<p>This use case contains several sample queries in which value based grouping is needed. Queries 1-6 are based on a simplified shop scenario with products, sale-records and different shop locations, whereas queries 7 and 8 are based on a bibliography document.</p>


<!--###################################### DATA products ############################ --> 
<div2 id="dtdproducts"><head>DTD for Q1-Q6</head><p>Most of the sample queries in this use case assume a simplified shop scenario described by three basic documents named products.xml, containing a list of products, stores.xml, containing a list of stores and sales-records.xml, including a list of sales. The product.xml uses the following Document Type Definitions (DTD):</p><eg role="schema" xml:space="preserve">&lt;!ELEMENT products (product*)&gt;
&lt;!ELEMENT product (name, category, price, cost)&gt;
&lt;!ELEMENT name (#PCDATA)&gt;
&lt;!ELEMENT category (#PCDATA)&gt;
&lt;!ELEMENT price (#PCDATA)&gt;
&lt;!ELEMENT cost (#PCDATA)&gt;
</eg>
<p>The sales-records.xml document follows this DTD:</p>
<eg role="schema" xml:space="preserve">
&lt;!ELEMENT sales (record*)&gt;
&lt;!ELEMENT record (product-name, store-number, qty)&gt;
&lt;!ELEMENT product-name (#PCDATA)&gt;
&lt;!ELEMENT store-number (#PCDATA)&gt;
&lt;!ELEMENT qty (#PCDATA)&gt;
</eg>
<p>The stores.xml document is valid to this DTD:</p>
<eg role="schema" xml:space="preserve">
&lt;!ELEMENT stores (store*)&gt;
&lt;!ELEMENT store (store-number, state)&gt;
&lt;!ELEMENT store-number (#PCDATA)&gt;
&lt;!ELEMENT state (#PCDATA)&gt;
</eg>
</div2>


<div2 id="dataproducts"><head>Sample Data</head><p>The content of products.xml is:</p><eg role="data" xml:space="preserve">&lt;products&gt;
  &lt;product&gt;
    &lt;name&gt;broiler&lt;/name&gt;
    &lt;category&gt;kitchen&lt;/category&gt;
    &lt;price&gt;100&lt;/price&gt;
    &lt;cost&gt;70&lt;/cost&gt;
  &lt;/product&gt;
  &lt;product&gt;
    &lt;name&gt;toaster&lt;/name&gt;
    &lt;category&gt;kitchen&lt;/category&gt;
    &lt;price&gt;30&lt;/price&gt;
    &lt;cost&gt;10&lt;/cost&gt;
  &lt;/product&gt;
  &lt;product&gt;
    &lt;name&gt;blender&lt;/name&gt;
    &lt;category&gt;kitchen&lt;/category&gt;
    &lt;price&gt;50&lt;/price&gt;
    &lt;cost&gt;25&lt;/cost&gt;
  &lt;/product&gt;
  &lt;product&gt;
    &lt;name&gt;socks&lt;/name&gt;
    &lt;category&gt;clothes&lt;/category&gt;
    &lt;price&gt;5&lt;/price&gt;
    &lt;cost&gt;2&lt;/cost&gt;
  &lt;/product&gt;
  &lt;product&gt;
    &lt;name&gt;shirt&lt;/name&gt;
    &lt;category&gt;clothes&lt;/category&gt;
    &lt;price&gt;10&lt;/price&gt;
    &lt;cost&gt;3&lt;/cost&gt;
  &lt;/product&gt;
&lt;/products&gt;
</eg>
<p>The content of sales-records.xml is:</p><eg role="data" xml:space="preserve">&lt;sales&gt;
  &lt;record&gt;
    &lt;product-name&gt;broiler&lt;/product-name&gt;
    &lt;store-number&gt;1&lt;/store-number&gt;
    &lt;qty&gt;20&lt;/qty&gt;
  &lt;/record&gt;
  &lt;record&gt;
    &lt;product-name&gt;toaster&lt;/product-name&gt;
    &lt;store-number&gt;2&lt;/store-number&gt;
    &lt;qty&gt;100&lt;/qty&gt;
  &lt;/record&gt;
  &lt;record&gt;
    &lt;product-name&gt;toaster&lt;/product-name&gt;
    &lt;store-number&gt;2&lt;/store-number&gt;
    &lt;qty&gt;50&lt;/qty&gt;
  &lt;/record&gt;
  &lt;record&gt;
    &lt;product-name&gt;toaster&lt;/product-name&gt;
    &lt;store-number&gt;3&lt;/store-number&gt;
    &lt;qty&gt;50&lt;/qty&gt;
  &lt;/record&gt;
  &lt;record&gt;
    &lt;product-name&gt;blender&lt;/product-name&gt;
    &lt;store-number&gt;3&lt;/store-number&gt;
    &lt;qty&gt;100&lt;/qty&gt;
  &lt;/record&gt;
  &lt;record&gt;
    &lt;product-name&gt;blender&lt;/product-name&gt;
    &lt;store-number&gt;3&lt;/store-number&gt;
    &lt;qty&gt;150&lt;/qty&gt;
  &lt;/record&gt;
  &lt;record&gt;
    &lt;product-name&gt;socks&lt;/product-name&gt;
    &lt;store-number&gt;1&lt;/store-number&gt;
    &lt;qty&gt;500&lt;/qty&gt;
  &lt;/record&gt;
  &lt;record&gt;
    &lt;product-name&gt;socks&lt;/product-name&gt;
    &lt;store-number&gt;2&lt;/store-number&gt;
    &lt;qty&gt;10&lt;/qty&gt;
  &lt;/record&gt;
  &lt;record&gt;
    &lt;product-name&gt;shirt&lt;/product-name&gt;
    &lt;store-number&gt;3&lt;/store-number&gt;
    &lt;qty&gt;10&lt;/qty&gt;
  &lt;/record&gt;
&lt;/sales&gt;
</eg>
<p>The content of stores.xml is:</p><eg role="data" xml:space="preserve">&lt;stores&gt;
  &lt;store&gt;
    &lt;store-number&gt;1&lt;/store-number&gt;
    &lt;state&gt;CA&lt;/state&gt;
  &lt;/store&gt;
  &lt;store&gt;
    &lt;store-number&gt;2&lt;/store-number&gt;
    &lt;state&gt;CA&lt;/state&gt;
  &lt;/store&gt;
  &lt;store&gt;
    &lt;store-number&gt;3&lt;/store-number&gt;
    &lt;state&gt;MA&lt;/state&gt;
  &lt;/store&gt;
&lt;/stores&gt;
</eg></div2>

<!--###################################### DATA books ############################ --> 
<div2 id="dtdbooks"><head>DTD for Q7-Q8</head><p>Q7 and Q8 use an input document named "books.xml", with the following DTD:</p>
<eg role="schema" xml:space="preserve">&lt;!ELEMENT bib ((book+))&gt;
&lt;!ELEMENT book ((author+, title))&gt;
&lt;!ELEMENT author (#PCDATA)&gt;
&lt;!ELEMENT title (#PCDATA)&gt;</eg>
</div2>
<div2 id="databooks"><head>Sample Data</head><p>Here are the contents of books.xml:</p><eg role="data" xml:space="preserve">&lt;bib&gt;
  &lt;book&gt;
    &lt;author&gt;Jim Melton&lt;/author&gt;
    &lt;author&gt;Alan Simon&lt;/author&gt;
    &lt;title&gt;SQL:1999&lt;/title&gt;
  &lt;/book&gt;
  &lt;book&gt;
    &lt;author&gt;Jim Melton&lt;/author&gt;
    &lt;title&gt;Advanced SQL:1999&lt;/title&gt;
  &lt;/book&gt;
  &lt;book&gt;
    &lt;author&gt;Alan Simon&lt;/author&gt;
    &lt;title&gt;Strategic Database Technology&lt;/title&gt;
  &lt;/book&gt;
  &lt;book&gt;
    &lt;author&gt;Jim Melton&lt;/author&gt;
    &lt;author&gt;Andrew Eisenberg&lt;/author&gt;
    &lt;title&gt;Understanding SQL and Java Together&lt;/title&gt;
  &lt;/book&gt;
  &lt;book&gt;
    &lt;author&gt;Jim Melton&lt;/author&gt;
    &lt;author&gt;Stephen Buxton&lt;/author&gt;
    &lt;title&gt;Querying XML&lt;/title&gt;
  &lt;/book&gt;
&lt;/bib&gt;
</eg></div2>



<!-- QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ --> 
<!--######################################    DATA groupby     ################################ -->
<!-- QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ --> 

<div2 id="groupby-queries-results"> 
<head>Queries and Results</head> 


 <!-- ############################ UC groupby_q1 ################################## --> 
<div3 id="groupby_q1"><head>Q1</head><p>Group sales by product, list name and total quantity of each product. </p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">&lt;sales-qty-by-product&gt;{
  for $sales in doc("sales-records.xml")/*/record
  let $pname := $sales/product-name
  group by $pname
  order by $pname
  return
    &lt;product name="{$pname}"&gt;{
      sum($sales/qty)
    }&lt;/product&gt;
}&lt;/sales-qty-by-product&gt; </eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;sales-qty-by-product&gt;
  &lt;product name="blender"&gt;250&lt;/product&gt;
  &lt;product name="broiler"&gt;20&lt;/product&gt;
  &lt;product name="shirt"&gt;10&lt;/product&gt;
  &lt;product name="socks"&gt;510&lt;/product&gt;
  &lt;product name="toaster"&gt;200&lt;/product&gt;
&lt;/sales-qty-by-product&gt;
</eg></div3>

 <!-- ############################ UC groupby_q2 ################################## --> 
<div3 id="groupby_q2"><head>Q2</head><p>Group sales by state (a property of the store) and category (a property
of the product). Order groups by the grouping keys. For each group,
show the total quantity sold. </p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">&lt;result&gt;{   
  for $sales in doc("sales-records.xml")/*/record
  let $state := doc("stores.xml")/*/store[store-number = $sales/store-number]/state
  let $category := doc("products.xml")/*/product[name = $sales/product-name]/category
  group by $state, $category
  order by $state, $category
  return
    &lt;group&gt;
      {$state, $category}
      &lt;total-qty&gt;{sum($sales/qty)}&lt;/total-qty&gt;
    &lt;/group&gt;
}&lt;/result&gt;</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;group&gt;
    &lt;state&gt;CA&lt;/state&gt;
    &lt;category&gt;clothes&lt;/category&gt;
    &lt;total-qty&gt;510&lt;/total-qty&gt;
  &lt;/group&gt;
  &lt;group&gt;
    &lt;state&gt;CA&lt;/state&gt;
    &lt;category&gt;kitchen&lt;/category&gt;
    &lt;total-qty&gt;170&lt;/total-qty&gt;
  &lt;/group&gt;
  &lt;group&gt;
    &lt;state&gt;MA&lt;/state&gt;
    &lt;category&gt;clothes&lt;/category&gt;
    &lt;total-qty&gt;10&lt;/total-qty&gt;
  &lt;/group&gt;
  &lt;group&gt;
    &lt;state&gt;MA&lt;/state&gt;
    &lt;category&gt;kitchen&lt;/category&gt;
    &lt;total-qty&gt;300&lt;/total-qty&gt;
  &lt;/group&gt;
&lt;/result&gt;
</eg></div3>

 <!-- ############################ UC groupby_q3 ################################## --> 
<div3 id="groupby_q3"><head>Q3</head><p>Group sales by state (a property of the store) and category (a property
of the product). Order groups by the grouping keys. For each group,
show the total revenue (defined as sales/qty * product/price). </p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">&lt;result&gt;{
  for $sales in doc("sales-records.xml")/*/record
  let $state := doc("stores.xml")/*/store[store-number = $sales/store-number]/state,
    $product := doc("products.xml")/*/product[name = $sales/product-name],
    $category := $product/category,
    $revenue := $sales/qty * $product/price
  group by $state, $category
  order by $state, $category
  return
    &lt;group&gt;
      {$state, $category}
      &lt;total-revenue&gt;{sum($revenue)}&lt;/total-revenue&gt;
    &lt;/group&gt; 
}&lt;/result&gt;</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;group&gt;
    &lt;state&gt;CA&lt;/state&gt;
    &lt;category&gt;clothes&lt;/category&gt;
    &lt;total-revenue&gt;2550&lt;/total-revenue&gt;
  &lt;/group&gt;
  &lt;group&gt;
    &lt;state&gt;CA&lt;/state&gt;
    &lt;category&gt;kitchen&lt;/category&gt;
    &lt;total-revenue&gt;6500&lt;/total-revenue&gt;
  &lt;/group&gt;
  &lt;group&gt;
    &lt;state&gt;MA&lt;/state&gt;
    &lt;category&gt;clothes&lt;/category&gt;
    &lt;total-revenue&gt;100&lt;/total-revenue&gt;
  &lt;/group&gt;
  &lt;group&gt;
    &lt;state&gt;MA&lt;/state&gt;
    &lt;category&gt;kitchen&lt;/category&gt;
    &lt;total-revenue&gt;14000&lt;/total-revenue&gt;
  &lt;/group&gt;
&lt;/result&gt;
</eg></div3>

 <!-- ############################ UC groupby_q4 ################################## --> 
<div3 id="groupby_q4"><head>Q4</head><p>Combine the input documents into a three-level hierarchy based on
state, category, and product. At the product level, show the total
quantity sold of each product. List items alphabetically at each
level of the hierarchy. </p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">&lt;result&gt;{
  for $store in doc("stores.xml")/*/store
  let $state := $store/state
  group by $state
  order by $state
  return
    &lt;state name="{$state}"&gt;{
      for $product in doc("products.xml")/*/product
      let $category := $product/category
      group by $category
      order by $category
      return
        &lt;category name="{$category}"&gt;{
          for $sales in doc("sales-records.xml")/*/record[store-number = $store/store-number
            and product-name = $product/name]
          let $pname := $sales/product-name
          group by $pname
          order by $pname
          return
            &lt;product name="{$pname}" total-qty="{sum($sales/qty)}" /&gt;
          }&lt;/category&gt;
    }&lt;/state&gt; 
}&lt;/result&gt;</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;state name="CA"&gt;
    &lt;category name="clothes"&gt;
      &lt;product name="socks" total-qty="510"/&gt;
    &lt;/category&gt;
    &lt;category name="kitchen"&gt;
      &lt;product name="broiler" total-qty="20"/&gt;
      &lt;product name="toaster" total-qty="150"/&gt;
    &lt;/category&gt;
  &lt;/state&gt;
  &lt;state name="MA"&gt;
    &lt;category name="clothes"&gt;
      &lt;product name="shirt" total-qty="10"/&gt;
    &lt;/category&gt;
    &lt;category name="kitchen"&gt;
      &lt;product name="blender" total-qty="250"/&gt;
      &lt;product name="toaster" total-qty="50"/&gt;
    &lt;/category&gt;
  &lt;/state&gt;
&lt;/result&gt;
</eg></div3>

 <!-- ############################ UC groupby_q5 ################################## --> 
<div3 id="groupby_q5"><head>Q5</head><p>List all stores in ascending order by store number. For each store, list
the products sold in that store, in descending order by quantity sold.

Illustrates ordering among and within groups. </p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">&lt;result&gt;{
  for $sales in doc("sales-records.xml")/*/record
  let $storeno := $sales/store-number
  group by $storeno
  order by $storeno
  return
    &lt;store number = "{$storeno}"&gt;{
      for $s in $sales
      order by xs:int($s/qty) descending
      return
        &lt;product name = "{$s/product-name}" qty = "{$s/qty}"/&gt;
    }&lt;/store&gt; 
}&lt;/result&gt;
</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;store number="1"&gt;
    &lt;product name="socks" qty="500"/&gt;
    &lt;product name="broiler" qty="20"/&gt;
  &lt;/store&gt;
  &lt;store number="2"&gt;
    &lt;product name="toaster" qty="100"/&gt;
    &lt;product name="toaster" qty="50"/&gt;
    &lt;product name="socks" qty="10"/&gt;
  &lt;/store&gt;
  &lt;store number="3"&gt;
    &lt;product name="blender" qty="150"/&gt;
    &lt;product name="blender" qty="100"/&gt;
    &lt;product name="toaster" qty="50"/&gt;
    &lt;product name="shirt" qty="10"/&gt;
  &lt;/store&gt;
&lt;/result&gt;
</eg></div3>

 <!-- ############################ UC groupby_q6 ################################## --> 
<div3 id="groupby_q6"><head>Q6</head><p>List all stores whose total profit is greater than 100000,
in descending order by total profit.
Note: total profit for a store is the sum over all sales
in that store, of the quantity sold times the difference
between price and cost for the item sold.

Illustrates cross-document computation, filtering of groups,
ordering by a non-grouping-key. </p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">&lt;result&gt;{
  for $sales in doc("sales-records.xml")/*/record
  let $storeno := $sales/store-number,
    $product := doc("products.xml")/*/product[name = $sales/product-name],
    $prd := $product,
    $profit := $sales/qty * ($prd/price - $prd/cost)
  group by $storeno
  let $total-store-profit := sum($profit)
  where $total-store-profit &gt; 100
  order by $total-store-profit descending
  return
    &lt;store number = "{$storeno}" total-profit = "{$total-store-profit}"/&gt; 
 }&lt;/result&gt;</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;store number="3" total-profit="7320"/&gt;
  &lt;store number="2" total-profit="3030"/&gt;
  &lt;store number="1" total-profit="2100"/&gt;
&lt;/result&gt;
</eg></div3>

 <!-- ############################ UC groupby_b1 ################################## --> 
<div3 id="groupby_b1"><head>Q7</head><p>Group books by author. Create a group for each individual author. A book with multiple authors should appear in the groups for each of its authors. Alphebetize the authors and the book titles within each author-group. </p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">&lt;result&gt;{
  for $book in doc("books.xml")/*/book
  for $author in $book/author
  group by $author
  order by $author
  return
  &lt;author name="{$author}"&gt;{ 
    for $b in $book
    order by $b/title
    return
      &lt;title&gt; {fn:data($b/title)} &lt;/title&gt;
  }&lt;/author&gt; 
}&lt;/result&gt;</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;author name="Alan Simon"&gt;
    &lt;title&gt;SQL:1999&lt;/title&gt;
    &lt;title&gt;Strategic Database Technology&lt;/title&gt;
  &lt;/author&gt;
  &lt;author name="Andrew Eisenberg"&gt;
    &lt;title&gt;Understanding SQL and Java Together&lt;/title&gt;
  &lt;/author&gt;
  &lt;author name="Jim Melton"&gt;
    &lt;title&gt;Advanced SQL:1999&lt;/title&gt;
    &lt;title&gt;Querying XML&lt;/title&gt;
    &lt;title&gt;SQL:1999&lt;/title&gt;
    &lt;title&gt;Understanding SQL and Java Together&lt;/title&gt;
  &lt;/author&gt;
  &lt;author name="Stephen Buxton"&gt;
    &lt;title&gt;Querying XML&lt;/title&gt;
  &lt;/author&gt;
&lt;/result&gt;
</eg></div3>

 <!-- ############################ UC groupby_b2 ################################## --> 
<div3 id="groupby_b2"><head>Q8</head><p>Group books by author. Create a group for each distinct ordered list of authors. Each book should be grouped with other books that have the same ordered list of authors. Alphebetize the book titles within each group. </p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">&lt;result&gt;{
  for $book in doc("books.xml")/*/book
  let $author-list := fn:string-join($book/author, ', ')
  group by $author-list
  order by $author-list
  return
    &lt;author-list names="{$author-list}"&gt;{ 
      for $b in $book
      order by $b/title
      return
        &lt;title&gt; {fn:data($b/title)} &lt;/title&gt;
    }&lt;/author-list&gt; 
}&lt;/result&gt;</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;author-list names="Alan Simon"&gt;
    &lt;title&gt;Strategic Database Technology&lt;/title&gt;
  &lt;/author-list&gt;
  &lt;author-list names="Jim Melton"&gt;
    &lt;title&gt;Advanced SQL:1999&lt;/title&gt;
  &lt;/author-list&gt;
  &lt;author-list names="Jim Melton, Alan Simon"&gt;
    &lt;title&gt;SQL:1999&lt;/title&gt;
  &lt;/author-list&gt;
  &lt;author-list names="Jim Melton, Andrew Eisenberg"&gt;
    &lt;title&gt;Understanding SQL and Java Together&lt;/title&gt;
  &lt;/author-list&gt;
  &lt;author-list names="Jim Melton, Stephen Buxton"&gt;
    &lt;title&gt;Querying XML&lt;/title&gt;
  &lt;/author-list&gt;
&lt;/result&gt;
</eg></div3>
</div2></div1>


<!-- EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE --> 
<!--######################################    groupby     ################################ -->
<!-- EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE -->



<!--################################################################################### --> 
<!--######################################    FORSEQ     ################################ --> 
<!--################################################################################### --> 


<div1 id="forseq"> 
<head>Use Case "forseq": Queries which require windowing</head>
<p>This use case covers queries that require windowing or positional grouping, which can be seen as a special form of windowing. Windowing means that the queries require selecting subsequences based on certain characterisics of an underlying sequence. 
</p>


<!--###################################### DATA arrange_rows ############################ --> 
<div2 id="dtdarrange_rows"><head>DTD for Q1</head><p>Q1 uses an input document named arrange_rows.xml, with the following DTD:</p><eg role="schema" xml:space="preserve">&lt;!ELEMENT doc ((data+))&gt;
&lt;!ELEMENT data (#PCDATA)&gt;
</eg></div2><div2 id="dataarrange_rows"><head>Sample Data</head><p>The content of arrange_rows.xml is:</p><eg role="data" xml:space="preserve">&lt;doc&gt;
  &lt;data&gt;Green&lt;/data&gt;
  &lt;data&gt;Pink&lt;/data&gt;
  &lt;data&gt;Lilac&lt;/data&gt;
  &lt;data&gt;Turquoise&lt;/data&gt;
  &lt;data&gt;Peach&lt;/data&gt;
  &lt;data&gt;Opal&lt;/data&gt;
  &lt;data&gt;Champagne&lt;/data&gt;
&lt;/doc&gt;
</eg></div2>

<!--###################################### DATA head_para ############################ --> 
<div2 id="dtdhead_para"><head>DTD for Q2</head><p>Q2 uses an input document named head_para.xml, with the following DTD:</p><eg role="schema" xml:space="preserve">
&lt;!ELEMENT body ((h2, p*)*)&gt;
&lt;!ELEMENT p (#PCDATA)&gt;
&lt;!ELEMENT h2 (#PCDATA)&gt;
</eg></div2><div2 id="datahead_para"><head>Sample Data</head><p>The content of head_para.xml is:</p><eg role="data" xml:space="preserve">&lt;body&gt;
  &lt;h2&gt;heading1&lt;/h2&gt;
  &lt;p&gt;para1&lt;/p&gt;
  &lt;p&gt;para2&lt;/p&gt;
  &lt;h2&gt;heading2&lt;/h2&gt;
  &lt;p&gt;para3&lt;/p&gt;
  &lt;p&gt;para4&lt;/p&gt;
  &lt;p&gt;para5&lt;/p&gt;
&lt;/body&gt;
</eg></div2>



<!--###################################### DATA term_def_list ############################ --> 
<div2 id="dtdterm_def_list"><head>DTD for Q3</head><p>Q3 uses an input document named term_def_list.xml, with the following DTD:</p>
<eg role="schema" xml:space="preserve">&lt;!ELEMENT doc ((dt+, dd+)*)&gt;
&lt;!ELEMENT dt (#PCDATA)&gt;
&lt;!ELEMENT dd (#PCDATA)&gt;
</eg></div2><div2 id="dataterm_def_list"><head>Sample Data</head><p>The content of term_def_list.xml is:</p><eg role="data" xml:space="preserve">&lt;doc&gt;
  &lt;dt&gt;XML&lt;/dt&gt;
  &lt;dd&gt;Extensible Markup Language&lt;/dd&gt;
  &lt;dt&gt;XSLT&lt;/dt&gt;
  &lt;dt&gt;XSL Transformations&lt;/dt&gt;
  &lt;dd&gt;A language for transforming XML&lt;/dd&gt;
  &lt;dd&gt;A specification produced by W3C&lt;/dd&gt;
&lt;/doc&gt;
</eg></div2>


<!--###################################### DATA delayed_events_day1 ############################ --> 
<div2 id="dtddelayed_events_day1"><head>DTD for Q4-Q6</head><p>Q4 - Q6 use an input document named temp_events.xml, with the following DTD:</p><eg role="schema" xml:space="preserve">&lt;!ELEMENT stream ((event+))&gt;
&lt;!ELEMENT event EMPTY&gt;
&lt;!ATTLIST event
	temp CDATA #REQUIRED
	time CDATA #REQUIRED
&gt;
</eg></div2><div2 id="datadelayed_events_day1"><head>Sample Data</head><p>The content of temp_events.xml is:</p><eg role="data" xml:space="preserve">&lt;stream&gt;
  &lt;event temp="10" time="1"/&gt;
  &lt;event temp="8" time="2"/&gt;
  &lt;event temp="6" time="3"/&gt;
  &lt;event temp="13" time="4"/&gt;
  &lt;event temp="33" time="5"/&gt;
  &lt;event temp="10" time="6"/&gt;
  &lt;event temp="10" time="7"/&gt;
&lt;/stream&gt;
</eg></div2>

<!--###################################### DATA day1 ############################ --> 
<div2 id="dtdday1"><head>DTD for Q7-Q13</head><p>Q7 - Q13 use an input document named person_events.xml, with the following DTD:</p><eg role="schema" xml:space="preserve">
&lt;!ELEMENT stream (event+)&gt;
&lt;!ELEMENT event ((person, direction)?)&gt;
&lt;!ATTLIST event time CDATA #REQUIRED&gt;
&lt;!ELEMENT person (#PCDATA)&gt;
&lt;!ELEMENT direction (#PCDATA)&gt;
</eg></div2><div2 id="dataday1"><head>Sample Data</head><p>The content of person_events.xml is:</p><eg role="data" xml:space="preserve">&lt;stream&gt;
  &lt;event time="2006-01-01T01:00:00-00:00"/&gt;
  &lt;event time="2006-01-01T10:30:00-00:00"&gt;
    &lt;person&gt;Anton&lt;/person&gt;
    &lt;direction&gt;in&lt;/direction&gt;
  &lt;/event&gt;
  &lt;event time="2006-01-01T11:00:00-00:00"&gt;
    &lt;person&gt;Barbara&lt;/person&gt;
    &lt;direction&gt;in&lt;/direction&gt;
  &lt;/event&gt;
  &lt;event time="2006-01-01T11:15:00-00:00"&gt;
    &lt;person&gt;Clara&lt;/person&gt;
    &lt;direction&gt;in&lt;/direction&gt;
  &lt;/event&gt;
  &lt;event time="2006-01-01T12:15:00-00:00"&gt;
    &lt;person&gt;Clara&lt;/person&gt;
    &lt;direction&gt;out&lt;/direction&gt;
  &lt;/event&gt;
  &lt;event time="2006-01-01T14:00:00-00:00"&gt;
    &lt;person&gt;Barbara&lt;/person&gt;
    &lt;direction&gt;out&lt;/direction&gt;
  &lt;/event&gt;
  &lt;event time="2006-01-01T15:00:00-00:00"&gt;
    &lt;person&gt;Anton&lt;/person&gt;
    &lt;direction&gt;out&lt;/direction&gt;
  &lt;/event&gt;
  &lt;event time="2006-01-01T23:00:00-00:00"/&gt;
  &lt;event time="2006-01-02T01:00:00-00:00"/&gt;
  &lt;event time="2006-01-02T11:00:00-00:00"&gt;
    &lt;person&gt;Anton&lt;/person&gt;
    &lt;direction&gt;in&lt;/direction&gt;
  &lt;/event&gt;
  &lt;event time="2006-01-02T12:00:00-00:00"&gt;
    &lt;person&gt;Clara&lt;/person&gt;
    &lt;direction&gt;in&lt;/direction&gt;
  &lt;/event&gt;
  &lt;event time="2006-01-02T16:00:00-00:00"&gt;
    &lt;person&gt;Anton&lt;/person&gt;
    &lt;direction&gt;out&lt;/direction&gt;
  &lt;/event&gt;
  &lt;event time="2006-01-02T16:15:00-00:00"&gt;
    &lt;person&gt;Clara&lt;/person&gt;
    &lt;direction&gt;out&lt;/direction&gt;
  &lt;/event&gt;
  &lt;event time="2006-01-02T23:00:00-00:00"/&gt;
&lt;/stream&gt;
</eg></div2>

<!--###################################### DATA rss ############################ --> 
<div2 id="dtdrss"><head>DTD for Q14-Q16</head><p>Q14 - Q16 use as input document a slightly modified RSS document named rss.xml, with the following DTD :</p><eg role="schema" xml:space="preserve">&lt;!ELEMENT rss (channel*)&gt;
&lt;!ATTLIST rss version CDATA #REQUIRED&gt;
&lt;!ELEMENT channel ((title, link, description, language, item*))&gt;
&lt;!ELEMENT description (#PCDATA)&gt;
&lt;!ELEMENT language (#PCDATA)&gt;
&lt;!ELEMENT link (#PCDATA)&gt;
&lt;!ELEMENT item ((title, category, author, pubDate))&gt;
&lt;!ELEMENT pubDate (#PCDATA)&gt;
&lt;!ELEMENT category (#PCDATA)&gt;
&lt;!ELEMENT author (#PCDATA)&gt;
&lt;!ELEMENT title (#PCDATA)&gt;
</eg></div2><div2 id="datarss"><head>Sample Data</head><p>The main difference to RSS 2.0 how dates are expressed in the data. The content of rss.xml is:</p><eg role="data" xml:space="preserve">&lt;rss version="2.0"&gt;
  &lt;channel&gt;
    &lt;title&gt;DBIS RSS&lt;/title&gt;
    &lt;link&gt;http://www.dbis.ethz.ch&lt;/link&gt;
    &lt;description&gt;The forseq dummy RSS.&lt;/description&gt;
    &lt;language&gt;en-us&lt;/language&gt;
    &lt;item&gt;
      &lt;title&gt;Why use cases are important Part 1.&lt;/title&gt;
      &lt;category&gt;Workshop&lt;/category&gt;
      &lt;author&gt;rokas@e-mail.de&lt;/author&gt;
      &lt;pubDate&gt;2003-06-03T09:00:00&lt;/pubDate&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;title&gt;Why use cases are important Part 2.&lt;/title&gt;
      &lt;category&gt;Workshop&lt;/category&gt;
      &lt;author&gt;rokas@e-mail.de&lt;/author&gt;
      &lt;pubDate&gt;2003-06-03T09:00:00&lt;/pubDate&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;title&gt;Why use cases are important Part 3.&lt;/title&gt;
      &lt;category&gt;Workshop&lt;/category&gt;
      &lt;author&gt;rokas@e-mail.de&lt;/author&gt;
      &lt;pubDate&gt;2003-06-03T10:00:00&lt;/pubDate&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;title&gt;Extending XQuery with Window Functions&lt;/title&gt;
      &lt;category&gt;Talk&lt;/category&gt;
      &lt;author&gt;tim@e-mail.de&lt;/author&gt;
      &lt;pubDate&gt;2003-06-03T11:00:00&lt;/pubDate&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;title&gt;XQueryP: A new programming language is born&lt;/title&gt;
      &lt;category&gt;Talk&lt;/category&gt;
      &lt;author&gt;david@e-mail.de&lt;/author&gt;
      &lt;pubDate&gt;2003-06-03T12:00:00&lt;/pubDate&gt;
    &lt;/item&gt;
    &lt;item&gt;
      &lt;title&gt;Why use cases are annoying to write.&lt;/title&gt;
      &lt;category&gt;Talk&lt;/category&gt;
      &lt;author&gt;rokas@e-mail.de&lt;/author&gt;
      &lt;pubDate&gt;2003-06-04T10:00:00&lt;/pubDate&gt;
    &lt;/item&gt;
  &lt;/channel&gt;
&lt;/rss&gt;
</eg></div2>

<!--###################################### DATA cXML ############################ --> 
<div2 id="dtdcXML"><head>DTD for Q17-Q19</head><p>Q17-Q19 use an input document named cxml.xml. The structure of this document is inspired by the Commerce XML Resource standard (cXML). The document contains a sequence of events, whereas an event corresponds to a simplified message of the cXML standard or an timeclock event. The DTD is as follows:</p>
<eg role="schema" xml:space="preserve">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!ELEMENT sequence ((time, OrderRequest, ConfirmationRequest,ShipNotice)*)&gt;
&lt;!ELEMENT time EMPTY&gt;
&lt;!ATTLIST time timedate CDATA #REQUIRED&gt;
&lt;!ELEMENT ShipNotice EMPTY&gt;
&lt;!ATTLIST ShipNotice date CDATA #REQUIRED	orderID CDATA #REQUIRED&gt;
&lt;!ELEMENT OrderRequest ((Item+))&gt;
&lt;!ATTLIST OrderRequest
	billTo CDATA #REQUIRED
	date CDATA #REQUIRED
	orderID CDATA #REQUIRED
	shipTo CDATA #IMPLIED
	total CDATA #REQUIRED
	type CDATA #REQUIRED
&gt;
&lt;!ELEMENT Item EMPTY&gt;
&lt;!ATTLIST Item
	partID CDATA #REQUIRED
	quantity CDATA #REQUIRED
	unitPrice CDATA #REQUIRED
&gt;
&lt;!ELEMENT ConfirmationRequest EMPTY&gt;
&lt;!ATTLIST ConfirmationRequest
	confirmID CDATA #REQUIRED
	date CDATA #REQUIRED
	orderID CDATA #REQUIRED
	status CDATA #REQUIRED
&gt;
</eg>
</div2>
<div2 id="datacXML"><head>Sample Data</head><p>The content of cxml.xml is:</p><eg role="data" xml:space="preserve">&lt;sequence&gt;
  &lt;time date="2006-01-01T00:00:00-00:00"/&gt;
  &lt;OrderRequest billTo="ACME1" date="2006-01-01T10:00:00-00:00"
    orderID="OID01" shipTo="ACME1" total="1100" type="new"&gt;
    &lt;Item partID="ID1" quantity="10" unitPrice="100"/&gt;
    &lt;Item partID="ID2" quantity="10" unitPrice="10"/&gt;
  &lt;/OrderRequest&gt;
  &lt;OrderRequest billTo="ACME2" date="2006-01-01T11:00:00-00:00"
    orderID="OID02" total="100" type="new"&gt;
    &lt;Item partID="ID2" quantity="10" unitPrice="10"/&gt;
  &lt;/OrderRequest&gt;
  &lt;ConfirmationRequest confirmID="C1" date="2006-01-01T18:00:00-00:00"
    orderID="OID02" status="reject"/&gt;
  &lt;time date="2006-01-02T00:00:00-00:00"/&gt;
  &lt;ConfirmationRequest confirmID="C1" date="2006-01-02T08:00:00-00:00"
    orderID="OID01" status="accept"/&gt;
  &lt;OrderRequest billTo="ACME1" date="2006-01-02T14:00:00-00:00"
    orderID="OID03" shipTo="ACME1" total="10000" type="new"&gt;
    &lt;Item partID="ID3" quantity="100" unitPrice="100"/&gt;
  &lt;/OrderRequest&gt;
  &lt;ConfirmationRequest confirmID="C1" date="2006-01-02T16:00:00-00:00"
    orderID="OID03" status="accept"/&gt;
  &lt;time date="2006-01-03T00:00:00-00:00"/&gt;
  &lt;time date="2006-01-04T00:00:00-00:00"/&gt;
  &lt;time date="2006-01-05T00:00:00-00:00"/&gt;
  &lt;ShipNotice date="2006-01-05T08:00:00-00:00" orderID="OID01"/&gt;
  &lt;ShipNotice date="2006-01-05T09:00:00-00:00" orderID="OID03"/&gt;
  &lt;time date="2006-01-06T00:00:00-00:00"/&gt;
  &lt;OrderRequest billTo="ACME2" date="2006-01-06T08:00:00-00:00"
    orderID="OID04" total="100" type="new"&gt;
    &lt;Item partID="ID2" quantity="10" unitPrice="10"/&gt;
  &lt;/OrderRequest&gt;
  &lt;time date="2006-01-07T00:00:00-00:00"/&gt;
&lt;/sequence&gt;
</eg></div2>


<!-- QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ --> 
<!--######################################    DATA forseq     ################################ -->
<!-- QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ --> 

<div2 id="forseq-queries-results"> 
<head>Queries and Results</head> 


 <!-- ############################ UC forseq_arrange_rows ################################## --> 
<div3 id="forseq_arrange_rows"><head>Q1</head><p>Arrange a sequence of items as a table with three columns (using as many rows as necessary). </p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">declare variable $seq := fn:doc("arrange_rows.xml");

&lt;table&gt;{
  forseq $w in $seq/doc/* tumbling window 
    start position $x when fn:true() 
    end position $y when $y - $x = 2
  return
    &lt;tr&gt;{
      for $i in $w
      return
        &lt;td&gt;{data($i)}&lt;/td&gt;
    }&lt;/tr&gt;
}&lt;/table&gt;
</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;table&gt;
  &lt;tr&gt;
    &lt;td&gt;Green&lt;/td&gt;
    &lt;td&gt;Pink&lt;/td&gt;
    &lt;td&gt;Lilac&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Turquoise&lt;/td&gt;
    &lt;td&gt;Peach&lt;/td&gt;
    &lt;td&gt;Opal&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Champagne&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
</eg></div3>

 <!-- ############################ UC forseq_head_para ################################## --> 
<div3 id="forseq_head_para"><head>Q2</head><p>Convert a structure with implicit sections to a structure with explicit sections.</p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">declare variable $seq := fn:doc("head_para.xml");

&lt;chapter&gt;{
  forseq $w in $seq/body/* tumbling window 
    start prevItem $s when $s[self::h2]  
    end curItem $e when $e[self::h2] 
  return
    &lt;section title="{data($s)}"&gt;{
       for $x in $w 
       return
         &lt;para&gt;{data($x)}&lt;/para&gt;
  }&lt;/section&gt;
}&lt;/chapter&gt;</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;chapter&gt;
  &lt;section title="heading1"&gt;
    &lt;para&gt;para1&lt;/para&gt;
    &lt;para&gt;para2&lt;/para&gt;
    &lt;para&gt;heading2&lt;/para&gt;
  &lt;/section&gt;
  &lt;section title="heading2"&gt;
    &lt;para&gt;para3&lt;/para&gt;
    &lt;para&gt;para4&lt;/para&gt;
    &lt;para&gt;para5&lt;/para&gt;
  &lt;/section&gt;
&lt;/chapter&gt;
</eg></div3>

 <!-- ############################ UC forseq_term_def_list ################################## --> 
<div3 id="forseq_term_def_list"><head>Q3</head><p>Within a glossary in HTML, a defined term &lt;dt&gt; can be followed by a definition &lt;dd&gt;. The task is to group these together within a &lt;term&gt; element, where a group can
consist of one or more &lt;dt&gt; elements followed by one or more &lt;dd&gt; elements.</p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">declare variable $seq := fn:doc("term_def_list.xml");

&lt;doc&gt;{
forseq $w in $seq/doc/* tumbling window 
  start curItem $x when $x[self::dt]
  end curItem $y, nextItem $z when $y[self::dd] and $z[self::dt]
return
  &lt;term&gt;{
    $w
  }&lt;/term&gt;
}&lt;/doc&gt;
</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;doc&gt;
  &lt;term&gt;
    &lt;dt&gt;XML&lt;/dt&gt;
    &lt;dd&gt;Extensible Markup Language&lt;/dd&gt;
  &lt;/term&gt;
  &lt;term&gt;
    &lt;dt&gt;XSLT&lt;/dt&gt;
    &lt;dt&gt;XSL Transformations&lt;/dt&gt;
    &lt;dd&gt;A language for transforming XML&lt;/dd&gt;
    &lt;dd&gt;A specification produced by W3C&lt;/dd&gt;
  &lt;/term&gt;
&lt;/doc&gt;
</eg></div3>

 <!-- ############################ UC forseq_1moving_avarage_time ################################## --> 
<div3 id="forseq_1moving_avarage_time"><head>Q4</head><p>Calculate the moving average of temperature values for the 3 last seconds.</p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">declare variable $timesequence := fn:doc("temp_events.xml");
let $MAX_DIFF := 2

forseq $w in $timesequence/stream/event sliding window
  start curItem $s_curr, prevItem $s_prev, position $s_pos 
    when ($s_curr/@time ne $s_prev/@time) or (empty($s_prev))
  force end nextItem $e_next 
    when $e_next/@time - $s_curr/@time gt $MAX_DIFF
return 
  avg( $w/@temp )

</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">8 9 17 18</eg></div3>

 <!-- ############################ UC forseq_2exponential_smoothing_1 ################################## --> 
<div3 id="forseq_2exponential_smoothing_1"><head>Q5</head><p>Single Exponential Smoothing (3 last values and smoothing-factor 0.2)</p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">declare variable $timesequence := fn:doc("temp_events.xml");
let $SMOOTH_CONST := 0.2

forseq $w in $timesequence/stream/event sliding window
  start position $s_pos when true()
  force end position $e_pos when $e_pos - $s_pos eq 2
return 
  round-half-to-even($SMOOTH_CONST * data($w[3]/@temp) + (1 - $SMOOTH_CONST) *
    ( $SMOOTH_CONST * data($w[2]/@temp) + 
      (1 - $SMOOTH_CONST) * data($w[1]/@temp) ), 2)
</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">8.88 8.68 12.32 15.24 23.92</eg></div3>

 <!-- ############################ UC forseq_3outlier_detection ################################## --> 
<div3 id="forseq_3outlier_detection"><head>Q6</head><p>Detect outliers (current value is two times higher (lower) than the previous one) in a sequence of temp values. </p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">declare variable $seq := fn:doc("temp_events.xml");

forseq $w in $seq/stream/event sliding window
  start curItem $s_curr when fn:true()
  force end nextItem $next when $next/@time &gt; $s_curr/@time + 3
return
  let $avg := fn:avg($w/@temp)
  where $avg * 2 lt xs:double($next/@temp) or $avg div 2 gt xs:double($next/@temp)
  return &lt;alarm&gt;Outlier detected. Event id:{data($next/@time)}&lt;/alarm&gt; 

</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;alarm&gt;Outlier detected. Event id:5&lt;/alarm&gt;
</eg></div3>

 <!-- ############################ UC forseq_1SEQ_A1h ################################## --> 
<div3 id="forseq_1SEQ_A1h"><head>Q7</head><p>Notify when Barbara enters the building within 1 hour after Anton </p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">declare variable $seq := fn:doc("person_events.xml");

&lt;result&gt;{
  forseq $w in $seq/stream/event tumbling window 
    start curItem $s when $s/person eq "Anton" and $s/direction eq "in"
    force end nextItem $n, curItem $e when  xs:dateTime($n/@time) - xs:dateTime($s/@time) gt 
      xs:dayTimeDuration("PT1H") or  $e/person eq "Barbara" and $e/direction eq "in"
  where $e/person eq "Barbara" and $e/direction eq "in"
  return 
    &lt;warning time="{ $e/@time }"&gt;Barbara: Anton arrived 1h ago&lt;/warning&gt;
}&lt;/result&gt;
</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;warning time="2006-01-01T11:00:00-00:00"&gt;Barbara: Anton arrived 1h ago&lt;/warning&gt;
&lt;/result&gt;
</eg></div3>

 <!-- ############################ UC forseq_2SEQ_TimePerPerson ################################## --> 
<div3 id="forseq_2SEQ_TimePerPerson"><head>Q8</head><p>Measure the working time of each person</p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">declare variable $seq := fn:doc("person_events.xml");

&lt;result&gt;{
forseq $w in $seq/stream/event sliding window
  start curItem $s when $s/direction eq "in"
  force end curItem $e when $s/person eq $e/person and
    $e/direction eq "out"
return
  &lt;working-time&gt; 
      {$s/person}
      &lt;time&gt;{ xs:dateTime($e/@time) - xs:dateTime($s/@time)}&lt;/time&gt;
  &lt;/working-time&gt;
}&lt;/result&gt;</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;working-time&gt;
    &lt;person&gt;Clara&lt;/person&gt;
    &lt;time&gt;PT1H&lt;/time&gt;
  &lt;/working-time&gt;
  &lt;working-time&gt;
    &lt;person&gt;Barbara&lt;/person&gt;
    &lt;time&gt;PT3H&lt;/time&gt;
  &lt;/working-time&gt;
  &lt;working-time&gt;
    &lt;person&gt;Anton&lt;/person&gt;
    &lt;time&gt;PT4H30M&lt;/time&gt;
  &lt;/working-time&gt;
  &lt;working-time&gt;
    &lt;person&gt;Clara&lt;/person&gt;
    &lt;time&gt;PT10M&lt;/time&gt;
  &lt;/working-time&gt;
  &lt;working-time&gt;
    &lt;person&gt;Clara&lt;/person&gt;
    &lt;time&gt;PT5M&lt;/time&gt;
  &lt;/working-time&gt;
  &lt;working-time&gt;
    &lt;person&gt;Clara&lt;/person&gt;
    &lt;time&gt;PT15M&lt;/time&gt;
  &lt;/working-time&gt;
  &lt;working-time&gt;
    &lt;person&gt;Anton&lt;/person&gt;
    &lt;time&gt;PT5H&lt;/time&gt;
  &lt;/working-time&gt;
  &lt;working-time&gt;
    &lt;person&gt;Clara&lt;/person&gt;
    &lt;time&gt;PT2H15M&lt;/time&gt;
  &lt;/working-time&gt;
&lt;/result&gt;
</eg></div3>

 <!-- ############################ UC forseq_2SEQ_TimePerPerson_overall ################################## --> 
<div3 id="forseq_2SEQ_TimePerPerson_overall"><head>Q9</head><p>Measure the overall working time for each person.</p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">declare variable $seq := fn:doc("person_events.xml");

&lt;result&gt;{
forseq $w in $seq/stream/event sliding window
  start curItem $s when $s/direction eq "in"
  force end curItem $e when $s/person eq $e/person and
    $e/direction eq "out"
let $person := $s/person
let $workingTime := xs:dateTime($e/@time) - xs:dateTime($s/@time)
group by $person
return
  &lt;working-time&gt;
    {$person}
    &lt;time&gt;{ sum($workingTime) }&lt;/time&gt;
  &lt;/working-time&gt;
    
}&lt;/result&gt;</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;working-time&gt;
    &lt;person&gt;Barbara&lt;/person&gt;
    &lt;time&gt;PT3H&lt;/time&gt;
  &lt;/working-time&gt;
  &lt;working-time&gt;
    &lt;person&gt;Anton&lt;/person&gt;
    &lt;time&gt;PT9H30M&lt;/time&gt;
  &lt;/working-time&gt;
  &lt;working-time&gt;
    &lt;person&gt;Clara&lt;/person&gt;
    &lt;time&gt;PT3H45M&lt;/time&gt;
  &lt;/working-time&gt;
&lt;/result&gt;
</eg></div3>

 <!-- ############################ UC forseq_3Seq_Not_A ################################## --> 
<div3 id="forseq_3Seq_Not_A"><head>Q10</head><p>Display a warning if Barbara does not come to work.</p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">declare variable $seq := fn:doc("person_events.xml");

&lt;result&gt;{
  forseq $w in $seq/stream/event[direction eq "in"] tumbling window 
    start curItem $s when fn:true() 
    end nextItem $e when xs:date( xs:dateTime($s/@time) ) ne xs:date( xs:dateTime($e/@time) ) 
  let $date := xs:date(xs:dateTime($s/@time))
  where not($w[person eq "Barbara"])
  return &lt;alert date="{ $date }"&gt;Barbara did not come to work&lt;/alert&gt;
}&lt;/result&gt;
</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;alert date="2006-01-02Z"&gt;Barbara did not come to work&lt;/alert&gt;
&lt;/result&gt;
</eg></div3>

 <!-- ############################ UC forseq_4SEQ_yA ################################## --> 
<div3 id="forseq_4SEQ_yA"><head>Q11</head><p>Identify every person who arrives at least 15 minutes earlier than Clara. 
</p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">declare variable $seq := fn:doc("person_events.xml");

&lt;result&gt;{
  forseq $w in $seq/stream/event tumbling window 
    start when true()
    force end curItem $x when  $x/person eq "Clara" and $x/direction eq "in"
  return 
    &lt;result time="{ $x/@time }"&gt;{
      distinct-values(for $y in $w 
        where (xs:dateTime($y/@time) + xs:dayTimeDuration("PT15M") ) lt xs:dateTime($x/@time)
        return $y/person)
    }&lt;/result&gt;
}&lt;/result&gt;
</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;result time="2006-01-01T11:15:00-00:00"&gt;Anton&lt;/result&gt;
  &lt;result time="2006-01-02T11:00:00-00:00"&gt;Clara Barbara Anton&lt;/result&gt;
  &lt;result time="2006-01-02T11:15:00-00:00"/&gt;
  &lt;result time="2006-01-02T11:30:00-00:00"/&gt;
  &lt;result time="2006-01-02T14:00:00-00:00"&gt;Clara&lt;/result&gt;
&lt;/result&gt;
</eg></div3>

 <!-- ############################ UC forseq_5NOSEQ_BothIn ################################## --> 
<div3 id="forseq_5NOSEQ_BothIn"><head>Q12</head><p>Notify when both Anton and Barbara enter the office within 30 minutes
of one another.</p>
<p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">
declare variable $seq := fn:doc("person_events.xml");

&lt;result&gt;{
	forseq $w in $seq/stream/event[direction eq "in"] tumbling window 
		start curItem $x when $x/person = ("Barbara", "Anton")
		end nextItem $y when xs:dateTime($y/@time) - xs:dateTime($x/@time) gt xs:dayTimeDuration("PT30M")
	where $w[person eq "Anton"] and $w[person eq "Barbara"]
	return 
		&lt;alert date="{ xs:dateTime($y/@time) }"&gt;Anton and Barbara just arrived&lt;/alert&gt;
}&lt;/result&gt;</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;alert date="2006-01-01T11:15:00Z"&gt;Anton and Barbara just arrived&lt;/alert&gt;
&lt;/result&gt;
</eg></div3>

 <!-- ############################ UC forseq_6AGG_Enter_3X_1 ################################## --> 
<div3 id="forseq_6AGG_Enter_3X_1"><head>Q13</head><p>Inform when a person entered the building at least 3 times within 1 hour</p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">declare variable $seq := fn:doc("person_events.xml");

&lt;result&gt;{
  forseq $w in $seq/stream/event sliding window 
    start curItem $s when true() 
    end nextItem $e when xs:dateTime($e/@time) - xs:dateTime($s/@time) gt 
      xs:dayTimeDuration("PT1H")
  where count($w[person eq $s/person and direction eq "in"]) ge 3
  return 
    &lt;alert time="{ $e/@time }"&gt;{fn:data($s/person)} is suspicious&lt;/alert&gt;
}&lt;/result&gt;</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;alert time="2006-01-02T14:00:00-00:00"&gt;Clara is suspicious&lt;/alert&gt;
&lt;/result&gt;
</eg></div3>

 <!-- ############################ UC forseq_annoying_authors ################################## --> 
<div3 id="forseq_annoying_authors"><head>Q14</head><p>Find all annoying authors who have posted three consecutive items in the RSS feed.</p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">declare variable $rssfeed := fn:doc("rss.xml");

&lt;result&gt;{
  forseq $w in $rssfeed/rss/channel/item tumbling window
    start curItem $first when fn:true()
    end nextItem $lookAhead when $first/author ne $lookAhead/author
  where count($w) ge 3
  return &lt;annoying-author&gt;{
      $w[1]/author
    }&lt;/annoying-author&gt;
}&lt;/result&gt;</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;annoying-author&gt;
    &lt;author&gt;rokas@e-mail.de&lt;/author&gt;
  &lt;/annoying-author&gt;
&lt;/result&gt;
</eg></div3>

 <!-- ############################ UC forseq_interesting_topics ################################## --> 
<div3 id="forseq_interesting_topics"><head>Q15</head><p>Every day provide a list of interesting topics in the RSS feed. In our example interesting means, that the title of the item contains the specific word XQuery.</p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">declare variable $rssfeed := fn:doc("rss.xml");

&lt;result&gt;{
  forseq $w in $rssfeed/rss/channel/item tumbling window
    start curItem $s_curr when true()
    end nextItem $e_next when 
      fn:day-from-dateTime(xs:dateTime($e_next/pubDate)) ne 
      fn:day-from-dateTime(xs:dateTime($s_curr/pubDate))
  return 
    &lt;item&gt;
        &lt;date&gt;{xs:date(xs:dateTime($s_curr/pubDate))}&lt;/date&gt;
        {  for $item in $w 
		   where fn:contains( xs:string($item/title), 'XQuery')
		   return $item/title	}
      &lt;/item&gt;
}&lt;/result&gt;
</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;item&gt;
    &lt;date&gt;2003-06-03&lt;/date&gt;
    &lt;title&gt;Extending XQuery with Window Functions&lt;/title&gt;
    &lt;title&gt;XQueryP: A new programming language is born&lt;/title&gt;
  &lt;/item&gt;
  &lt;item&gt;
    &lt;date&gt;2003-06-04&lt;/date&gt;
  &lt;/item&gt;
&lt;/result&gt;
</eg></div3>

 <!-- ############################ UC forseq_summary_by_authors ################################## --> 
<div3 id="forseq_summary_by_authors"><head>Q16</head><p>Every day, provide a summary of the RSS feed grouped by author.</p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">declare variable $rssfeed := fn:doc("rss.xml");

&lt;result&gt;{
  forseq $w in $rssfeed/rss/channel/item tumbling window
    start curItem $s_curr when true()
    end nextItem $e_next when 
      fn:day-from-dateTime(xs:dateTime($e_next/pubDate)) ne 
      fn:day-from-dateTime(xs:dateTime($s_curr/pubDate))
  return 
    &lt;item&gt;
      &lt;date&gt;{xs:date(xs:dateTime($s_curr/pubDate))}&lt;/date&gt;
       {  for $a in fn:distinct-values($w/author) 
           return 
             &lt;author name="{$a}"&gt;
               &lt;titles&gt;
                 { $w[author eq $a]/title }
               &lt;/titles&gt;
			&lt;/author&gt;
	    }
	  &lt;/item&gt;
}&lt;/result&gt;</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;item&gt;
    &lt;date&gt;2003-06-03&lt;/date&gt;
    &lt;author name="rokas@e-mail.de"&gt;
      &lt;titles&gt;
        &lt;title&gt;Why use cases are important Part 1.&lt;/title&gt;
        &lt;title&gt;Why use cases are important Part 2.&lt;/title&gt;
        &lt;title&gt;Why use cases are important Part 3.&lt;/title&gt;
      &lt;/titles&gt;
    &lt;/author&gt;
    &lt;author name="tim@e-mail.de"&gt;
      &lt;titles&gt;
        &lt;title&gt;Extending XQuery with Window Functions&lt;/title&gt;
      &lt;/titles&gt;
    &lt;/author&gt;
    &lt;author name="david@e-mail.de"&gt;
      &lt;titles&gt;
        &lt;title&gt;XQueryP: A new programming language is born&lt;/title&gt;
      &lt;/titles&gt;
    &lt;/author&gt;
  &lt;/item&gt;
  &lt;item&gt;
    &lt;date&gt;2003-06-04&lt;/date&gt;
    &lt;author name="rokas@e-mail.de"&gt;
      &lt;titles&gt;
        &lt;title&gt;Why use cases are annoying to write.&lt;/title&gt;
      &lt;/titles&gt;
    &lt;/author&gt;
  &lt;/item&gt;
&lt;/result&gt;
</eg></div3>

 <!-- ############################ UC forseq_Q2_most_valuable_customer ################################## --> 
<div3 id="forseq_Q2_most_valuable_customer"><head>Q17</head><p>At the end of a day,
list the most valuable customers.</p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">declare variable $seq := fn:doc("cxml.xml");

&lt;result&gt;{
forseq $w in $seq/sequence/* sliding window 
  start curItem $cur, prevItem $prev when day-from-date(xs:dateTime($cur/@date)) ne 
    day-from-date(xs:dateTime($prev/@date)) or empty($prev)
  end when newstart
return
  &lt;mostValuableCustomer endOfDay="{xs:dateTime($cur/@date)}"&gt;{
    let $companies :=	for $x in distinct-values($w/@billTo ) return 
			&lt;amount company="{$x}"&gt;{sum($w[@billTo eq $x]/@total)}&lt;/amount&gt;
    let $max := max($companies)	
    for $company in $companies
    where $company eq xs:untypedAtomic($max)
    return $company
  }&lt;/mostValuableCustomer&gt;
}&lt;/result&gt;</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;mostValuableCustomer endOfDay="2006-01-01T00:00:00Z"&gt;
    &lt;amount company="ACME1"&gt;1100&lt;/amount&gt;
  &lt;/mostValuableCustomer&gt;
  &lt;mostValuableCustomer endOfDay="2006-01-02T00:00:00Z"&gt;
    &lt;amount company="ACME1"&gt;10000&lt;/amount&gt;
  &lt;/mostValuableCustomer&gt;
  &lt;mostValuableCustomer endOfDay="2006-01-03T00:00:00Z"/&gt;
  &lt;mostValuableCustomer endOfDay="2006-01-04T00:00:00Z"/&gt;
  &lt;mostValuableCustomer endOfDay="2006-01-05T00:00:00Z"/&gt;
  &lt;mostValuableCustomer endOfDay="2006-01-06T00:00:00Z"&gt;
    &lt;amount company="ACME2"&gt;100&lt;/amount&gt;
  &lt;/mostValuableCustomer&gt;
  &lt;mostValuableCustomer endOfDay="2006-01-07T00:00:00Z"/&gt;
&lt;/result&gt;
</eg></div3>

 <!-- ############################ UC forseq_Q3_transaction_time ################################## --> 
<div3 id="forseq_Q3_transaction_time"><head>Q18</head><p>Calculate the time needed to process an order from the orderRequest up to the shipping.</p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">declare variable $seq := fn:doc("cxml.xml");

&lt;result&gt;{
  forseq $w in $seq/sequence/* sliding window 
    start curItem $s when $s[self::OrderRequest]
    end curItem $e when $e/@orderID eq  $s/@orderID and
      $e[self::ConfirmationRequest] and $e/@status eq "reject") 
      or $e[self::ShipNotice]
  where $e[self::ShipNotice]
  return 
    &lt;timeToShip orderID="{ $s/@orderID}"&gt;{xs:dateTime($e/@date) - xs:dateTime($s/@date) }&lt;/timeToShip&gt; 
}&lt;/result&gt;</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;timeToShip orderID="OID01"&gt;P3DT22H&lt;/timeToShip&gt;
  &lt;timeToShip orderID="OID03"&gt;P2DT19H&lt;/timeToShip&gt;
&lt;/result&gt;
</eg></div3>

 <!-- ############################ UC forseq_Q4_ship_together ################################## --> 
<div3 id="forseq_Q4_ship_together"><head>Q19</head><p>Calculate at the moment of the shipping notification if an open request exists that can be shipped to the same address.</p><p><emph>Solution in XQuery:</emph></p><eg role="xquery" xml:space="preserve">declare variable $seq := fn:doc("cxml.xml");

&lt;result&gt;{
  forseq $w in $seq/sequence/* sliding window 
    start prevItem $wSPrev when $wSPrev[self::OrderRequest]
    end nextItem $wENext when $wENext/@orderID eq  $wSPrev/@orderID and
	  ( $wENext[self::ConfirmationRequest] and $wENext/@status eq "reject") 
	    or $wENext[self::ShipNotice]
  where $wENext[self::ShipNotice]
  return 
    &lt;bundleWith orderId="{$wSPrev/@orderID}"&gt;{
        forseq $bundle in $w sliding window 
          start curItem $bSCur when $bSCur[self::OrderRequest] and 
            $bSCur/@shipTo eq $wSPrev/@shipTo
          end curItem $bECur, nextItem $bENext when $bECur/@orderID eq  $bSCur/@orderID and
            ($bECur[self::ConfirmationRequest] and $bECur/@status eq "reject") or 
            $bECur[self::ShipNotice]
          where empty($bENext)
          return $bSCur
    }&lt;/bundleWith&gt;
}&lt;/result&gt;</eg><p><emph>Expected Result:</emph></p><eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;bundleWith orderId="OID01"&gt;
    &lt;OrderRequest billTo="ACME1" date="2006-01-02T14:00:00-00:00"
      orderID="OID03" shipTo="ACME1" total="10000" type="new"&gt;
      &lt;Item partID="ID3" quantity="100" unitPrice="100"/&gt;
    &lt;/OrderRequest&gt;
  &lt;/bundleWith&gt;
  &lt;bundleWith orderId="OID03"/&gt;
&lt;/result&gt;
</eg></div3>
</div2></div1>


<!-- EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE --> 
<!--######################################    forseq     ################################ -->
<!-- EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE -->




    </body>
    <back>

<!-- Original appendix provided by Tim:
        <div1 id="acknowledgements">
            <head>Acknowledgements</head>
            <p>The editors thank the members of the XML Query Working Group, which produced the
                material in this document.</p>
            <p>The use cases in this paper were contributed by the following individuals:</p>
            <table border="1">
                <tbody>
                    <tr>
                        <td>Use Case "groupby"</td>
                        <td>Don Chamberlin</td>
                    </tr>
                    <tr>
                        <td>Use Case "forseq"</td>
                        <td>Peter M. Fischer, Donald Kossmann, Tim Kraska, Rokas Tamosevicius</td>
                    </tr>
                </tbody>
            </table>
            <p>Use case "forseq" has been previously published in <bibref ref="Fischer2006"/>.</p>
        </div1>
-->

        <inform-div1 id="acknowledgements">
            <head>Acknowledgements</head>
            <p>The Working Group thanks the following individuals for their contributions:</p>
            <table border="1">
                <tbody>
                    <tr>
                        <td rowspan="1" colspan="1">Peter M. Fischer, Donald Kossmann, Rokas Tamosevicius</td>
                        <td rowspan="1" colspan="1">Use Case "forseq"</td>
                    </tr>
                </tbody>
            </table>
            <p>Use case "forseq" has been previously published in <bibref ref="Fischer2006"/>.</p>
        </inform-div1>

        <inform-div1 id="ChangeLog">
            <head>Change Log</head>
            <div2 id="id-2008-01-30">
                <head>30 January 2008</head>
                <ulist>
                    <item>
                        <p>Added group-by UC.</p>
                    </item>
                    <item>
                        <p>Added forseq UC.</p>
                    </item>
                </ulist>
            </div2>

        </inform-div1>
        <inform-div1 id="references">
            <head>References</head>
            <p>The following references are some of the works considered by the WG in deriving its
                use cases. </p>
            <blist>
                <bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="Fischer2006" key="Forseq UC" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
                    <titleref href="http://www.dbis.ethz.ch/research/publications/XQWindowsUseCases.pdf" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">Windows for XQuery - Use Cases</titleref>, Peter M. Fischer, Donald
                    Kossmann, Tim Kraska and Rokas Tamosevicius, 2006, Technical Report, ETH Zurich</bibl>
                <bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="xmlschema-0" key="XMLSchema0" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"/>
                <bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="xmlschema-1" key="XMLSchema1" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"/>
                <bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="xmlschema-2" key="XMLSchema2" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"/>
                <bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="xquery" key="XQuery" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"/>
                <bibl xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/xquery-11-requirements/" id="xquery-11-requirements" key="XQuery 1.1 Requirements" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
                      <loc href="http://www.w3.org/TR/xquery-11-requirements/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XML Query (XQuery) 1.1 Requirements</loc>,
                      W3C Working Draft, 28 March 2008.</bibl>
                <bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="xslt" key="XSLT" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"/>
                <bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="UseCaseQueries" key="Use Case Sample Queries" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
                    <titleref href="http://www.w3.org/TR/2008/WD-xquery-11-use-cases-20080327/xquery-use-case-queries.txt" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">Queries from this
                        document</titleref>, presented in a single file</bibl>
                <bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="XQueryQueries" key="XQuery Sample Queries" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
                    <titleref href="http://www.w3.org/TR/2008/WD-xquery-11-use-cases-20080327/xquery-wd-queries.txt" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">Queries from the XQuery
                        document</titleref>, presented in a single file.</bibl>
            </blist>
        </inform-div1>
    </back>
</spec>