<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE spec PUBLIC "-//W3C//DTD Specification V2.0//EN" "../schema/xmlspec.dtd" [
<!ENTITY date.day "23">
<!ENTITY date.DD "23">
<!ENTITY date.month "March">
<!ENTITY date.monthnum "03">
<!ENTITY date.year "2007">
<!ENTITY doc.date "&date.year;&date.monthnum;&date.DD;">
<!ENTITY w3c.tr "http://www.w3.org/TR">
<!ENTITY doc.shortname "xquery-use-cases">
<!ENTITY doc.w3c-designation "NOTE-&doc.shortname;">
<!ENTITY doc.publoc "&w3c.tr;/&date.year;/&doc.w3c-designation;-&doc.date;/">
<!ENTITY doc.latestloc "&w3c.tr;/&doc.shortname;/">
]>


<spec w3c-doctype="note">
    <header>
        <title>XML Query Use Cases</title>
        <version/>
        <w3c-designation>&doc.w3c-designation;-&doc.date;</w3c-designation>
        <w3c-doctype>W3C Working Group Note</w3c-doctype>
        <pubdate>
            <day>&date.day;</day>
            <month>&date.month;</month>
            <year>&date.year;</year>
        </pubdate>
        <publoc>
            <loc href="&doc.publoc;">&doc.publoc;</loc>
        </publoc>
<altlocs>
            <loc href="&doc.publoc;xquery-use-cases.xml">XML</loc>
</altlocs>
        <latestloc>
            <loc href="&doc.latestloc;">&doc.latestloc;</loc>
        </latestloc>
        <prevlocs>
            <loc href="http://www.w3.org/TR/2006/WD-xquery-use-cases-20060608/"/>
        </prevlocs>
        <authlist>
            <author>
                <name>Don Chamberlin</name>
                <affiliation>IBM Almaden Research Center</affiliation>
                <email href="mailto:chamberlin@almaden.ibm.com"
                    >chamberlin@almaden.ibm.com </email>
            </author>
            <author>
                <name>Peter Fankhauser</name>
                <affiliation>Infonyte GmbH</affiliation>
                <email href="mailto:fankhauser@infonyte.com"
                    >fankhauser@infonyte.com</email>
            </author>
            <author>
                <name>Daniela Florescu</name>
                <affiliation>Oracle corporation</affiliation>
                <email href="mailto:dana.florescu@oracle.com">dana.florescu@oracle.com</email>
            </author>
            <author>
                <name>Massimo Marchiori</name>
                <affiliation>University of Venice</affiliation>
                <email href="mailto:massimo@w3.org">massimo@w3.org</email>
            </author>
            <author>
                <name>Jonathan Robie</name>
                <affiliation>DataDirect Technologies</affiliation>
                <email href="mailto:jonathan.robie@datadirect.com"
                    >jonathan.robie@datadirect.com</email>
            </author>
        </authlist>
        <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 is the W3C
<loc href="http://www.w3.org/2005/10/Process-20051014/tr.html#tr-end">Working Group Note</loc>
of "XML Query (XQuery) Use Cases", produced by the W3C
<loc href="http://www.w3.org/XML/Query/">XML Query Working Group</loc>, part of the
<loc href="http://www.w3.org/XML/Activity">XML Activity</loc>.
This document is being published as a
<loc href="http://www.w3.org/2005/10/Process-20051014/tr.html#tr-end">Working Group Note</loc>
to persistently record the Use Cases that guided the development of
<loc href="http://www.w3.org/TR/xquery/">XQuery 1.0: An XML Query Language</loc>
and its associated specifications as W3C Recommendations. </p>

<p>Please submit comments about this document using 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 public comments 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 [XQRecUseCases] in the subject line of your report,
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>Publication as a <loc href="http://www.w3.org/2005/10/Process-20051014/tr.html#tr-end">Working
Group Note</loc> 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>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>. 
W3C maintains a
<loc rel="disclosure" href="http://www.w3.org/2004/01/pp-impl/18797/status#disclosures">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 XQuery.</p>
        </abstract>
        <langusage>
            <language id="EN">English</language>
        </langusage>
        <revisiondesc>
			<p>Robie, 8 May 2005: New status section. Added note on static typing.
			</p>

            <p>Robie, 31 July 2002: Minor changes to ensure that examples parse
                with the current syntax. All occurrences of date() changed to
                current-date(), and editorial notes on dates deleted since they
                now work properly in FandO. Deleted use case REF, since we no
                longer support the =&gt; operator, and people did not feel
                this use case was a good illustration of the problem domain.
                Replaced use case FNPARM with use case STRONG. Replaced filter()
                in the table of contents query with a recursive function call,
                since filter() no longer exists. Deleted queries Q3 and Q6,
                which jury rigged some primitive full text search capabilities
                to get the right answer, but didn't solve the real underlying
                problem. Added the new W3C patent policy language to the status
                section. Replaced 'precedes' with '&lt;&lt;', replaced
                follows with '&gt;&gt;'. </p>
            <p>Robie, 23 April 2002: Updated to reflect current language. Many
                use cases have been corrected based on testing and analysis done
                by Dana and me.</p>
            <p>Robie, 17 Dec 2001: Changed all examples to the current syntax.
                Changed functions to support the current Functions and Operators
                equivalents.</p>
            <p>Robie, 8 Jun 2001: Corrected many examples, converted all queries
                to the new XQuery syntax, and added use case FNPARM.</p>
            <p>Robie, 15 Feb 2001: First stand-alone Working Draft. This
                material previously appeared as a part of the W3C XML Query
                Requirements Working Draft, but was placed into a separate
                document to make it easier to incorporate solutions.</p>
        </revisiondesc>
    </header>
    <body>
        <div1 id="ucforxq">
            <head>Use Cases for XML Queries</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-requirements"/> document, without
            solutions in concrete query languages. Now it is being
            republished with solutions for <bibref
            ref="xquery"/>. These use cases are also being used by the
            <loc href="http://www.w3.org/XML/Group/xquery-test/">W3C
            XML Query Testing Task Force</loc>.</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>doc("http://bstore1.example.com/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>$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>//author</eg>
            <p>Previous versions of this document accessed implicit documents
                using the <code>input()</code> function, which no longer exists.
                The <code>input()</code> function had similar functionality to a
                predefined context item, except that it could be bound to a
                sequence of nodes, whereas the context item may only be bound to
                a single node. The use cases that used <code>input()</code> have
                been rewritten to use explicit file names.</p>
            <p>Several implementors have asked that we make the queries from
                these use cases available in a separate file 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>

            <div2 id="xmp">
                <head>Use Case "XMP": Experiences and Exemplars</head>
                <p>This use case contains several example queries that
                    illustrate requirements gathered from the database and
                    document communities.</p>
                <div3 id="xmp-dtd">
                    <head>Document Type Definitions (DTD) </head>
                    <p>Most of the example queries in this use case are based on
                        a bibliography document named
                        "http://bstore1.example.com/bib.xml" with the following
                        DTD: </p>
                    <eg role="schema"><![CDATA[<!ELEMENT bib  (book* )>
<!ELEMENT book  (title,  (author+ | editor+ ), publisher, price )>
<!ATTLIST book  year CDATA  #REQUIRED >
<!ELEMENT author  (last, first )>
<!ELEMENT editor  (last, first, affiliation )>
<!ELEMENT title  (#PCDATA )>
<!ELEMENT last  (#PCDATA )>
<!ELEMENT first  (#PCDATA )>
<!ELEMENT affiliation  (#PCDATA )>
<!ELEMENT publisher  (#PCDATA )>
<!ELEMENT price  (#PCDATA )>]]></eg>
                </div3>
                <div3 id="xmp-data">
                    <head>Sample Data</head>
                    <p>Here is the data found at "bstore1.example.com/bib.xml":</p>
                    <eg role="data"><![CDATA[<bib>
    <book year="1994">
        <title>TCP/IP Illustrated</title>
        <author><last>Stevens</last><first>W.</first></author>
        <publisher>Addison-Wesley</publisher>
        <price>65.95</price>
    </book>
 
    <book year="1992">
        <title>Advanced Programming in the Unix environment</title>
        <author><last>Stevens</last><first>W.</first></author>
        <publisher>Addison-Wesley</publisher>
        <price>65.95</price>
    </book>
 
    <book year="2000">
        <title>Data on the Web</title>
        <author><last>Abiteboul</last><first>Serge</first></author>
        <author><last>Buneman</last><first>Peter</first></author>
        <author><last>Suciu</last><first>Dan</first></author>
        <publisher>Morgan Kaufmann Publishers</publisher>
        <price>39.95</price>
    </book>
 
    <book year="1999">
        <title>The Economics of Technology and Content for Digital TV</title>
        <editor>
               <last>Gerbarg</last><first>Darcy</first>
                <affiliation>CITI</affiliation>
        </editor>
            <publisher>Kluwer Academic Publishers</publisher>
        <price>129.95</price>
    </book>
 
</bib>
                    ]]></eg>
                </div3>
                <div3 id="xmp-dtd-q5">
                    <head>DTD for Q5</head>
                    <p>Q5 also uses information on book reviews and prices from
                        a separate data source named
                        "http://bstore2.example.com/reviews.xml" with the
                        following DTD:</p>
                    <eg role="schema"><![CDATA[<!ELEMENT reviews (entry*)>
<!ELEMENT entry   (title, price, review)>
<!ELEMENT title   (#PCDATA)>
<!ELEMENT price   (#PCDATA)>
<!ELEMENT review  (#PCDATA)>
 ]]></eg>
                </div3>
                <div3 id="xmp-data-q5">
                    <head>Sample Data for Q5</head>
                    <p>Here are the contents of
                        "http://bstore2.example.com/reviews.xml":</p>
                    <eg role="data"><![CDATA[<reviews>
    <entry>
        <title>Data on the Web</title>
        <price>34.95</price>
        <review>
               A very good discussion of semi-structured database
               systems and XML.
        </review>
    </entry>
    <entry>
        <title>Advanced Programming in the Unix environment</title>
        <price>65.95</price>
        <review>
               A clear and detailed discussion of UNIX programming.
        </review>
    </entry>
    <entry>
        <title>TCP/IP Illustrated</title>
        <price>65.95</price>
        <review>
               One of the best books on TCP/IP.
        </review>
    </entry>
</reviews>]]></eg>
                </div3>
                <div3 id="xmp-dtd-q9">
                    <head>DTD for Q9</head>
                    <p>Q9 uses an input document named "books.xml", with the
                        following DTD:</p>
                    <eg role="schema"><![CDATA[<!ELEMENT chapter (title, section*)>
<!ELEMENT section (title, section*)>
<!ELEMENT title (#PCDATA)>
 ]]></eg>
                </div3>
                <div3 id="xmp-data-q9">
                    <head>Data for Q9</head>
                    <p>Here are the contents of books.xml:</p>
                    <eg role="data"><![CDATA[<chapter>
    <title>Data Model</title>
    <section>
        <title>Syntax For Data Model</title>
    </section>
    <section>
        <title>XML</title>
        <section>
            <title>Basic Syntax</title>
        </section>
        <section>
            <title>XML and Semistructured Data</title>
        </section>
    </section>
</chapter>]]></eg>
                </div3>
                <div3 id="xmp-dtd-q10">
                    <head>DTD for Q10</head>
                    <p>Q10 uses an input document named "prices.xml", with the
                        following DTD: </p>
                    <eg role="schema"><![CDATA[<!ELEMENT prices (book*)>
<!ELEMENT book (title, source, price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT source (#PCDATA)>
<!ELEMENT price (#PCDATA)>
                    ]]></eg>
                </div3>
                <div3 id="xmp-data-q10">
                    <head>Data for Q10</head>
                    <p>Here are the contents of prices.xml:</p>
                    <eg role="data"><![CDATA[<prices>
    <book>
        <title>Advanced Programming in the Unix environment</title>
        <source>bstore2.example.com</source>
        <price>65.95</price>
    </book>
    <book>
        <title>Advanced Programming in the Unix environment</title>
        <source>bstore1.example.com</source>
        <price>65.95</price>
    </book>
    <book>
        <title>TCP/IP Illustrated</title>
        <source>bstore2.example.com</source>
        <price>65.95</price>
    </book>
    <book>
        <title>TCP/IP Illustrated</title>
        <source>bstore1.example.com</source>
        <price>65.95</price>
    </book>
    <book>
        <title>Data on the Web</title>
        <source>bstore2.example.com</source>
        <price>34.95</price>
    </book>
    <book>
        <title>Data on the Web</title>
        <source>bstore1.example.com</source>
        <price>39.95</price>
    </book>
</prices>]]></eg>
                </div3>
                <div3 id="xmp-queries-results">
                    <head>Queries and Results</head>
                    <div4 id="xmp-queries-results-q1">
                        <head>Q1</head>
                        <p>List books published by Addison-Wesley after 1991,
                            including their year and title.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<bib>
 {
  for $b in doc("http://bstore1.example.com/bib.xml")/bib/book
  where $b/publisher = "Addison-Wesley" and $b/@year > 1991
  return
    <book year="{ $b/@year }">
     { $b/title }
    </book>
 }
</bib> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<bib>
    <book year="1994">
        <title>TCP/IP Illustrated</title>
    </book>
    <book year="1992">
        <title>Advanced Programming in the Unix environment</title>
    </book>
</bib>
                        ]]></eg>
                    </div4>
                    <div4 id="xmp-queries-results-q2">
                        <head>Q2</head>
                        <p>Create a flat list of all the title-author pairs,
                            with each pair enclosed in a "result" element.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<results>
  {
    for $b in doc("http://bstore1.example.com/bib.xml")/bib/book,
        $t in $b/title,
        $a in $b/author
    return
        <result>
            { $t }    
            { $a }
        </result>
  }
</results>
                        ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<results>
    <result>
        <title>TCP/IP Illustrated</title>
        <author>
            <last>Stevens</last>
            <first>W.</first>
        </author>
    </result>
    <result>
        <title>Advanced Programming in the Unix environment</title>
        <author>
            <last>Stevens</last>
            <first>W.</first>
        </author>
    </result>
    <result>
        <title>Data on the Web</title>
        <author>
            <last>Abiteboul</last>
            <first>Serge</first>
        </author>
    </result>
    <result>
        <title>Data on the Web</title>
        <author>
            <last>Buneman</last>
            <first>Peter</first>
        </author>
    </result>
    <result>
        <title>Data on the Web</title>
        <author>
            <last>Suciu</last>
            <first>Dan</first>
        </author>
    </result>
</results>]]></eg>
                    </div4>
                    <div4 id="xmp-queries-results-q3">
                        <head>Q3</head>
                        <p>For each book in the bibliography, list the title and
                            authors, grouped inside a "result" element.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<results>
{
    for $b in doc("http://bstore1.example.com/bib.xml")/bib/book
    return
        <result>
            { $b/title }
            { $b/author  }
        </result>
}
</results> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<results>
    <result>
        <title>TCP/IP Illustrated</title>
        <author>
            <last>Stevens</last>
            <first>W.</first>
        </author>
    </result>
    <result>
        <title>Advanced Programming in the Unix environment</title>
        <author>
            <last>Stevens</last>
            <first>W.</first>
        </author>
    </result>
    <result>
        <title>Data on the Web</title>
        <author>
            <last>Abiteboul</last>
            <first>Serge</first>
        </author>
        <author>
            <last>Buneman</last>
            <first>Peter</first>
        </author>
        <author>
            <last>Suciu</last>
            <first>Dan</first>
        </author>
    </result>
    <result>
        <title>The Economics of Technology and Content for Digital TV</title>
    </result>
</results>
                        ]]></eg>
                    </div4>
                    <div4 id="xmp-queries-results-q4">
                        <head>Q4</head>
                        <p>For each author in the bibliography, list the
                            author's name and the titles of all books by that
                            author, grouped inside a "result" element.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<results>
  {
    let $a := doc("http://bstore1.example.com/bib/bib.xml")//author
    for $last in distinct-values($a/last),
        $first in distinct-values($a[last=$last]/first)
    order by $last, $first
    return
        <result>
            <author>
               <last>{ $last }</last>
               <first>{ $first }</first>
            </author>
            {
                for $b in doc("http://bstore1.example.com/bib.xml")/bib/book
                where some $ba in $b/author 
                      satisfies ($ba/last = $last and $ba/first=$first)
                return $b/title
            }
        </result>
  }
</results> ]]></eg>
                        <p> The order in which values are returned by
                            distinct-values() is undefined. The
                            distinct-values() function returns atomic values,
                            extracting the names from the elements.</p>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<results>
    <result>
        <author>
            <last>Abiteboul</last>
            <first>Serge</first>
        </author>
        <title>Data on the Web</title>
    </result>
    <result>
        <author>
            <last>Buneman</last>
            <first>Peter</first>
        </author>
        <title>Data on the Web</title>
    </result>
    <result>
        <author>
            <last>Stevens</last>
            <first>W.</first>
        </author>
        <title>TCP/IP Illustrated</title>
        <title>Advanced Programming in the Unix environment</title>
    </result>
    <result>
        <author>
            <last>Suciu</last>
            <first>Dan</first>
        </author>
        <title>Data on the Web</title>
    </result>
</results>
                        ]]></eg>
                    </div4>
                    <div4 id="xmp-queries-results-q5">
                        <head>Q5</head>
                        <p>For each book found at both bstore1.example.com and
                            bstore2.example.com, list the title of the book and
                            its price from each source.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<books-with-prices>
  {
    for $b in doc("http://bstore1.example.com/bib.xml")//book,
        $a in doc("http://bstore2.example.com/reviews.xml")//entry
    where $b/title = $a/title
    return
        <book-with-prices>
            { $b/title }
            <price-bstore2>{ $a/price/text() }</price-bstore2>
            <price-bstore1>{ $b/price/text() }</price-bstore1>
        </book-with-prices>
  }
</books-with-prices>]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<books-with-prices>
    <book-with-prices>
        <title>TCP/IP Illustrated</title>
        <price-bstore2>65.95</price-bstore2>
        <price-bstore1>65.95</price-bstore1>
    </book-with-prices>
    <book-with-prices>
        <title>Advanced Programming in the Unix environment</title>
        <price-bstore2>65.95</price-bstore2>
        <price-bstore1>65.95</price-bstore1>
    </book-with-prices>
    <book-with-prices>
        <title>Data on the Web</title>
        <price-bstore2>34.95</price-bstore2>
        <price-bstore1>39.95</price-bstore1>
    </book-with-prices>
</books-with-prices> ]]></eg>
                    </div4>
                    <div4 id="xmp-queries-results-q6">
                        <head>Q6</head>
                        <p>For each book that has at least one author, list the
                            title and first two authors, and an empty "et-al"
                            element if the book has additional authors.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<bib>
  {
    for $b in doc("http://bstore1.example.com/bib.xml")//book
    where count($b/author) > 0
    return
        <book>
            { $b/title }
            {
                for $a in $b/author[position()<=2]  
                return $a
            }
            {
                if (count($b/author) > 2)
                 then <et-al/>
                 else ()
            }
        </book>
  }
</bib>]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<bib>
    <book>
        <title>TCP/IP Illustrated</title>
        <author>
            <last>Stevens</last>
            <first>W.</first>
        </author>
    </book>
    <book>
        <title>Advanced Programming in the Unix environment</title>
        <author>
            <last>Stevens</last>
            <first>W.</first>
        </author>
    </book>
    <book>
        <title>Data on the Web</title>
        <author>
            <last>Abiteboul</last>
            <first>Serge</first>
        </author>
        <author>
            <last>Buneman</last>
            <first>Peter</first>
        </author>
        <et-al/>
    </book>
</bib> ]]></eg>
                    </div4>
                    <div4 id="xmp-queries-results-q7">
                        <head>Q7</head>
                        <p>List the titles and years of all books published by
                            Addison-Wesley after 1991, in alphabetic order.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<bib>
  {
    for $b in doc("http://bstore1.example.com/bib.xml")//book
    where $b/publisher = "Addison-Wesley" and $b/@year > 1991
    order by $b/title
    return
        <book>
            { $b/@year }
            { $b/title }
        </book>
  }
</bib> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<bib>
    <book year="1992">
        <title>Advanced Programming in the Unix environment</title>
    </book>
    <book year="1994">
        <title>TCP/IP Illustrated</title>
    </book>
</bib>     ]]></eg>
                    </div4>
                    <div4 id="xmp-queries-results-q8">
                        <head>Q8</head>
                        <p>Find books in which the name of some element ends
                            with the string "or" and the same element contains
                            the string "Suciu" somewhere in its content. For
                            each such book, return the title and the qualifying
                            element.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[for $b in doc("http://bstore1.example.com/bib.xml")//book
let $e := $b/*[contains(string(.), "Suciu") 
               and ends-with(local-name(.), "or")]
where exists($e)
return
    <book>
        { $b/title }
        { $e }
    </book> ]]></eg>
                        <p>In the above solution, string(), local-name() and
                            ends-with() are functions defined in the Functions
                            and Operators document.</p>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<book>
        <title>Data on the Web</title>
        <author>
            <last>Suciu</last>
            <first>Dan</first>
        </author>
 </book>]]></eg>
                    </div4>
                    <div4 id="xmp-queries-results-q9">
                        <head>Q9</head>
                        <p>In the document "books.xml", find all section or
                            chapter titles that contain the word "XML",
                            regardless of the level of nesting.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<results>
  {
    for $t in doc("books.xml")//(chapter | section)/title
    where contains($t/text(), "XML")
    return $t
  }
</results> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<results>
    <title>XML</title>
    <title>XML and Semistructured Data</title>
</results> ]]></eg>
                    </div4>
                    <div4 id="xmp-queries-results-q10">
                        <head>Q10</head>
                        <p>In the document "prices.xml", find the minimum price
                            for each book, in the form of a "minprice" element
                            with the book title as its title attribute.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<results>
  {
    let $doc := doc("prices.xml")
    for $t in distinct-values($doc//book/title)
    let $p := $doc//book[title = $t]/price
    return
      <minprice title="{ $t }">
        <price>{ min($p) }</price>
      </minprice>
  }
</results> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<results>
    <minprice title="Advanced Programming in the Unix environment">
        <price>65.95</price>
    </minprice>
    <minprice title="TCP/IP Illustrated">
        <price>65.95</price>
    </minprice>
    <minprice title="Data on the Web">
        <price>34.95</price>
    </minprice>
</results> ]]></eg>
                    </div4>
                    <div4 id="xmp-queries-results-q11">
                        <head>Q11</head>
                        <p> For each book with an author, return the book with
                            its title and authors. For each book with an editor,
                            return a reference with the book title and the
                            editor's affiliation.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<bib>
{
        for $b in doc("http://bstore1.example.com/bib.xml")//book[author]
        return
            <book>
                { $b/title }
                { $b/author }
            </book>
}
{
        for $b in doc("http://bstore1.example.com/bib.xml")//book[editor]
        return
          <reference>
            { $b/title }
            {$b/editor/affiliation}
          </reference>
}
</bib>  ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<bib>
    <book>
        <title>TCP/IP Illustrated</title>
        <author>
            <last>Stevens</last>
            <first>W.</first>
        </author>
    </book>
    <book>
        <title>Advanced Programming in the Unix environment</title>
        <author>
            <last>Stevens</last>
            <first>W.</first>
        </author>
    </book>
    <book>
        <title>Data on the Web</title>
        <author>
            <last>Abiteboul</last>
            <first>Serge</first>
        </author>
        <author>
            <last>Buneman</last>
            <first>Peter</first>
        </author>
        <author>
            <last>Suciu</last>
            <first>Dan</first>
        </author>
    </book>
    <reference>
        <title>The Economics of Technology and Content for Digital TV</title>
        <affiliation>CITI</affiliation>
    </reference>
</bib> ]]></eg>
                    </div4>
                    <div4 id="xmp-queries-results-q12">
                        <head>Q12</head>
                        <p>Find pairs of books that have different titles but
                            the same set of authors (possibly in a different
                            order).</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<bib>
{
    for $book1 in doc("http://bstore1.example.com/bib.xml")//book,
        $book2 in doc("http://bstore1.example.com/bib.xml")//book
    let $aut1 := for $a in $book1/author 
                 order by $a/last, $a/first
                 return $a
    let $aut2 := for $a in $book2/author 
                 order by $a/last, $a/first
                 return $a
    where $book1 << $book2
    and not($book1/title = $book2/title)
    and deep-equal($aut1, $aut2) 
    return
        <book-pair>
            { $book1/title }
            { $book2/title }
        </book-pair>
}
</bib> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<bib>
    <book-pair>
        <title>TCP/IP Illustrated</title>
        <title>Advanced Programming in the Unix environment</title>
    </book-pair>
</bib> ]]></eg>
                        <p>The above solution uses a function, deep-equal(),
                            which compares sequences. Two sequences are equal if
                            all items in corresponding positions in the two
                            sequences are equal - if the sequences are node
                            sequences, the values of the nodes are used for
                            comparison.</p>
                    </div4>
                </div3>
            </div2>
            <div2 id="tree">
                <head>Use Case "TREE": Queries that preserve hierarchy</head>
                <p>Some XML document-types have a very flexible structure in
                    which text is mixed with elements and many elements are
                    optional. These document-types show a wide variation in
                    structure from one document to another. In documents of
                    these types, the ways in which elements are ordered and
                    nested are usually quite important. </p>
                <div3 id="tree-description">
                    <head>Description</head>
                    <p>An XML query language should have the ability to extract
                        elements from documents while preserving their original
                        hierarchy. This Use Case illustrates this requirement by
                        means of a flexible document type named Book.</p>
                </div3>
                <div3 id="tree-dtd">
                    <head>Document Type Definition (DTD)</head>
                    <p>This use case is based on an input document named
                        "book.xml". The DTD for this schema is found in a file
                        called "book.dtd": </p>
                    <eg role="schema"><![CDATA[  <!ELEMENT book (title, author+, section+)>
  <!ELEMENT title (#PCDATA)>
  <!ELEMENT author (#PCDATA)>
  <!ELEMENT section (title, (p | figure | section)* )>
  <!ATTLIST section
      id         ID    #IMPLIED
      difficulty CDATA #IMPLIED>
  <!ELEMENT p (#PCDATA)>
  <!ELEMENT figure (title, image)>
  <!ATTLIST figure
       width   CDATA   #REQUIRED
       height  CDATA   #REQUIRED >
  <!ELEMENT image EMPTY>
  <!ATTLIST image
       source  CDATA   #REQUIRED >  ]]></eg>
                </div3>
                <div3 id="tree-data">
                    <head>Sample Data</head>
                    <p>The queries in this use case are based on the following
                        sample data.</p>
                    <eg role="data"><![CDATA[<?xml version="1.0"?>
<!DOCTYPE book SYSTEM "book.dtd">
<book>
  <title>Data on the Web</title>
  <author>Serge Abiteboul</author>
  <author>Peter Buneman</author>
  <author>Dan Suciu</author>
  <section id="intro" difficulty="easy" >
    <title>Introduction</title>
    <p>Text ... </p>
    <section>
      <title>Audience</title>
      <p>Text ... </p>
    </section>
    <section>
      <title>Web Data and the Two Cultures</title>
      <p>Text ... </p>
      <figure height="400" width="400">
        <title>Traditional client/server architecture</title>
        <image source="csarch.gif"/>
      </figure>
      <p>Text ... </p>
    </section>
  </section>
  <section id="syntax" difficulty="medium" >
    <title>A Syntax For Data</title>
    <p>Text ... </p>
    <figure height="200" width="500">
      <title>Graph representations of structures</title>
      <image source="graphs.gif"/>
    </figure>
    <p>Text ... </p>
    <section>
      <title>Base Types</title>
      <p>Text ... </p>
    </section>
    <section>
      <title>Representing Relational Databases</title>
      <p>Text ... </p>
      <figure height="250" width="400">
        <title>Examples of Relations</title>
        <image source="relations.gif"/>
      </figure>
    </section>
    <section>
      <title>Representing Object Databases</title>
      <p>Text ... </p>
    </section>       
  </section>
</book> ]]></eg>
                </div3>
                <div3 id="tree-queries-results">
                    <head>Queries and Results</head>
                    <div4 id="tree-queries-results-q1">
                        <head>Q1</head>
                        <p>Prepare a (nested) table of contents for Book1,
                            listing all the sections and their titles. Preserve
                            the original attributes of each
                            &lt;section&gt; element, if any.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[declare function local:toc($book-or-section as element()) as element()*
{
    for $section in $book-or-section/section
    return
      <section>
         { $section/@* , $section/title , local:toc($section) }			
      </section>
};

<toc>
   {
     for $s in doc("book.xml")/book return local:toc($s)
   }
</toc> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<toc>
    <section id="intro" difficulty="easy">
        <title>Introduction</title>
        <section>
            <title>Audience</title>
        </section>
        <section>
            <title>Web Data and the Two Cultures</title>
        </section>
    </section>
    <section id="syntax" difficulty="medium">
        <title>A Syntax For Data</title>
        <section>
            <title>Base Types</title>
        </section>
        <section>
            <title>Representing Relational Databases</title>
        </section>
        <section>
            <title>Representing Object Databases</title>
        </section>
    </section>
</toc> ]]></eg>
                    </div4>
                    <div4 id="tree-queries-results-q2">
                        <head>Q2</head>
                        <p> Prepare a (flat) figure list for Book1, listing all
                            the figures and their titles. Preserve the original
                            attributes of each &lt;figure&gt; element,
                            if any.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<figlist>
  {
    for $f in doc("book.xml")//figure
    return
        <figure>
            { $f/@* }
            { $f/title }
        </figure>
  }
</figlist> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<figlist>
    <figure  height="400" width="400">
        <title>Traditional client/server architecture</title>
    </figure>
    <figure  height="200" width="500">
        <title>Graph representations of structures</title>
    </figure>
    <figure  height="250" width ="400">
        <title>Examples of Relations</title>
    </figure>
</figlist> ]]></eg>
                    </div4>
                    <div4 id="tree-queries-results-q3">
                        <head>Q3</head>
                        <p>How many sections are in Book1, and how many figures? </p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<section_count>{ count(doc("book.xml")//section) }</section_count>, 
<figure_count>{ count(doc("book.xml")//figure) }</figure_count> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<section_count>7</section_count>
<figure_count>3</figure_count> ]]></eg>
                    </div4>
                    <div4 id="tree-queries-results-q4">
                        <head>Q4</head>
                        <p>How many top-level sections are in Book1? </p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<top_section_count>
 { 
   count(doc("book.xml")/book/section) 
 }
</top_section_count>]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"
                            ><![CDATA[<top_section_count>2</top_section_count> ]]></eg>
                    </div4>
                    <div4 id="tree-queries-results-q5">
                        <head>Q5</head>
                        <p>Make a flat list of the section elements in Book1. In
                            place of its original attributes, each section
                            element should have two attributes, containing the
                            title of the section and the number of figures
                            immediately contained in the section. </p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<section_list>
  {
    for $s in doc("book.xml")//section
    let $f := $s/figure
    return
        <section title="{ $s/title/text() }" figcount="{ count($f) }"/>
  }
</section_list> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<section_list>
    <section title="Introduction" figcount="0"/>
    <section title="Audience" figcount="0"/>
    <section title="Web Data and the Two Cultures" figcount="1"/>
    <section title="A Syntax For Data" figcount="1"/>
    <section title="Base Types" figcount="0"/>
    <section title="Representing Relational Databases" figcount="1"/>
    <section title="Representing Object Databases" figcount="0"/>
</section_list>]]></eg>
                    </div4>
                    <div4 id="tree-queries-results-q6">
                        <head>Q6</head>
                        <p>Make a nested list of the section elements in Book1,
                            preserving their original attributes and hierarchy.
                            Inside each section element, include the title of
                            the section and an element that includes the number
                            of figures immediately contained in the section. </p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[declare function local:section-summary($book-or-section as element()*)
  as element()*
{
  for $section in $book-or-section
  return
    <section>
       { $section/@* }
       { $section/title }	
       <figcount>		
         { count($section/figure) }
       </figcount>		
       { local:section-summary($section/section) }			
    </section>
};

<toc>
  {
    for $s in doc("book.xml")/book/section
    return local:section-summary($s)
  }
</toc> ]]></eg>
                        <ednote>
                            <edtext>This solution was provided by Michael
                                Wenger, a student at the University of
                                W&#x00FC;rzburg.</edtext>
                        </ednote>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<toc>
    <section id="intro" difficulty="easy">
        <title>Introduction</title>
        <figcount>0</figcount>
        <section>
            <title>Audience</title>
            <figcount>0</figcount>
        </section>
        <section>
            <title>Web Data and the Two Cultures</title>
            <figcount>1</figcount>
        </section>
    </section>
    <section id="syntax" difficulty="medium">
        <title>A Syntax For Data</title>
        <figcount>1</figcount>
        <section>
            <title>Base Types</title>
            <figcount>0</figcount>
        </section>
        <section>
            <title>Representing Relational Databases</title>
            <figcount>1</figcount>
        </section>
        <section>
            <title>Representing Object Databases</title>
            <figcount>0</figcount>
        </section>
    </section>
</toc> ]]></eg>
                    </div4>
                </div3>
            </div2>
            <div2 id="seq">
                <head>Use Case "SEQ" - Queries based on Sequence</head>
                <p>This use case illustrates queries based on the sequence in
                    which elements appear in a document. </p>
                <div3 id="seq-description">
                    <head>Description</head>
                    <p>Although sequence is not significant in most traditional
                        database systems or object systems, it can be quite
                        significant in structured documents. This use case
                        presents a series of queries based on a medical
                    report.</p>
                </div3>
                <div3 id="seq-dtd">
                    <head>Document Type Definition (DTD)</head>
                    <p>This use case is based on a medical report using the HL7
                        Patient Record Architecture. We simplify the DTD in this
                        example, using only what is needed to understand the
                        queries.</p>
                    <eg role="schema"><![CDATA[
<!DOCTYPE report [
  <!ELEMENT report (section*)>
  <!ELEMENT section (section.title, section.content)>
  <!ELEMENT section.title  (#PCDATA )>
  <!ELEMENT section.content  (#PCDATA | anesthesia | prep 
                            | incision | action | observation )*>
  <!ELEMENT anesthesia (#PCDATA)>
  <!ELEMENT prep ( (#PCDATA | action)* )>
  <!ELEMENT incision ( (#PCDATA | geography | instrument)* )>
  <!ELEMENT action ( (#PCDATA | instrument )* )>
  <!ELEMENT observation (#PCDATA)>
  <!ELEMENT geography (#PCDATA)>
  <!ELEMENT instrument (#PCDATA)>
]> ]]></eg>
                </div3>
                <div3 id="seq-data">
                    <head>Sample Data</head>
                    <p>The queries in this use case are based on the following
                        sample data.</p>
                    <eg role="data"><![CDATA[<report>
  <section>
    <section.title>Procedure</section.title>
     <section.content>
      The patient was taken to the operating room where she was placed
      in supine position and
      <anesthesia>induced under general anesthesia.</anesthesia>
      <prep> 
        <action>A Foley catheter was placed to decompress the bladder</action>
        and the abdomen was then prepped and draped in sterile fashion.
      </prep>  
      <incision>
        A curvilinear incision was made
        <geography>in the midline immediately infraumbilical</geography>
        and the subcutaneous tissue was divided
        <instrument>using electrocautery.</instrument>
      </incision>
      The fascia was identified and
      <action>#2 0 Maxon stay sutures were placed on each side of the midline.
      </action>
      <incision>
        The fascia was divided using
        <instrument>electrocautery</instrument>
        and the peritoneum was entered.
      </incision>
      <observation>The small bowel was identified.</observation>
      and
      <action>
        the
        <instrument>Hasson trocar</instrument>
        was placed under direct visualization.
      </action>
      <action>
        The
        <instrument>trocar</instrument>
        was secured to the fascia using the stay sutures.
      </action>
     </section.content>
  </section>
</report>]]></eg>
                </div3>
                <div3 id="seq-queries-results">
                    <head>Queries and Results</head>
                    <div4 id="seq-queries-results-q1">
                        <head>Q1</head>
                        <p>In the Procedure section of Report1, what Instruments
                            were used in the second Incision? </p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[for $s in doc("report1.xml")//section[section.title = "Procedure"]
return ($s//incision)[2]/instrument]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"
                            ><![CDATA[<instrument>electrocautery</instrument>]]></eg>
                    </div4>
                    <div4 id="seq-queries-results-q2">
                        <head>Q2</head>
                        <p>In the Procedure section of Report1, what are the
                            first two Instruments to be used? </p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[for $s in doc("report1.xml")//section[section.title = "Procedure"]
return ($s//instrument)[position()<=2]]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<instrument>using electrocautery.</instrument>
<instrument>electrocautery</instrument> ]]></eg>
                    </div4>
                    <div4 id="seq-queries-results-q3">
                        <head>Q3</head>
                        <p> In Report1, what Instruments were used in the first
                            two Actions after the second Incision? </p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[let $i2 := (doc("report1.xml")//incision)[2]
for $a in (doc("report1.xml")//action)[. >> $i2][position()<=2]
return $a//instrument ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<instrument>Hasson trocar</instrument>
<instrument>trocar</instrument> ]]></eg>
                    </div4>
                    <div4 id="seq-queries-results-q4">
                        <head>Q4</head>
                        <p>In Report1, find "Procedure" sections where no
                            Anesthesia element occurs before the first Incision</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[for $p in doc("report1.xml")//section[section.title = "Procedure"]
where not(some $a in $p//anesthesia satisfies
        $a << ($p//incision)[1] )
return $p ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[

                        ]]></eg>
                        <p>(No sections satisfy Q4, thankfully.)</p>
                    </div4>
                    <div4 id="seq-queries-results-q5">
                        <head>Q5</head>
                        <p>In Report1, what happened between the first Incision
                            and the second Incision? </p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[declare function local:precedes($a as node(), $b as node()) as xs:boolean 
{
    $a << $b
      and
    empty($a//node() intersect $b)
};


declare function local:follows($a as node(), $b as node()) as xs:boolean 
{
    $a >> $b
      and
    empty($b//node() intersect $a)
};

<critical_sequence>
 {
  let $proc := doc("report1.xml")//section[section.title="Procedure"][1]
  for $n in $proc//node()
  where local:follows($n, ($proc//incision)[1])
    and local:precedes($n, ($proc//incision)[2])
  return $n
 }
</critical_sequence> ]]></eg>
                        <p>Here is another solution that is perhaps more
                            efficient and less readable:</p>
                        <eg role="xquery"><![CDATA[<critical_sequence>
 {
  let $proc := doc("report1.xml")//section[section.title="Procedure"][1],
      $i1 :=  ($proc//incision)[1],
      $i2 :=  ($proc//incision)[2]
  for $n in $proc//node() except $i1//node()
  where $n >> $i1 and $n << $i2
  return $n 
 }
</critical_sequence> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<critical_sequence>
      The fascia was identified and
      <action>#2 0 Maxon stay sutures were placed on each side of the midline.
      </action>#2 0 Maxon stay sutures were placed on each side of the midline.
</critical_sequence> ]]></eg>
                        <p>In the above output, the contents of the critical
                            sequence element include a text node, an action
                            element, and the text node containing the content of
                            the action element. But the serialization we are
                            using already shows all descendants of a given node.
                            If $c is bound to a sequence of nodes, the following
                            expression eliminates members of the sequence that
                            are descendants of another node already found in the
                            sequence:</p>
                        <eg role="xquery">$c except $c//node()</eg>
                        <p>In the following solution, the between() function
                            takes a sequence of nodes, a starting node, and an
                            ending node, and returns the nodes between them:</p>
                        <eg role="xquery"><![CDATA[
declare function local:between($seq as node()*, $start as node(), $end as node())
 as item()*
{
  let $nodes :=
    for $n in $seq except $start//node()
    where $n >> $start and $n << $end
    return $n
  return $nodes except $nodes//node()
};

<critical_sequence>
 {
  let $proc := doc("report1.xml")//section[section.title="Procedure"][1],
      $first :=  ($proc//incision)[1],
      $second:=  ($proc//incision)[2]
  return local:between($proc//node(), $first, $second)
 }
</critical_sequence>
                        ]]></eg>
                        <p>Here is the output from the above query:</p>
                        <eg role="result"><![CDATA[<critical_sequence>
      The fascia was identified and
      <action>#2 0 Maxon stay sutures were placed on each side of the midline.
      </action>
</critical_sequence> ]]></eg>
                    </div4>
                </div3>
            </div2>
            <div2 id="rdb">
                <head>Use Case "R" - Access to Relational Data</head>
                <p>One important use of an XML query language will be to access
                    data stored in relational databases. This use case describes
                    one possible way in which this access might be accomplished.</p>
                <div3 id="rdb-description">
                    <head>Description</head>
                    <p>A relational database system might present a view in
                        which each table (relation) takes the form of an XML
                        document. One way to represent a database table as an
                        XML document is to allow the document element to
                        represent the table itself, and each row (tuple) inside
                        the table to be represented by a nested element. Inside
                        the tuple-elements, each column is in turn represented
                        by a nested element. Columns that allow null values are
                        represented by optional elements, and a missing element
                        denotes a null value.</p>
                    <p>As an example, consider a relational database used by an
                        online auction. The auction maintains a USERS table
                        containing information on registered users, each
                        identified by a unique userid, who can either offer
                        items for sale or bid on items. An ITEMS table lists
                        items currently or recently for sale, with the userid of
                        the user who offered each item. A BIDS table contains
                        all bids on record, keyed by the userid of the bidder
                        and the item number of the item to which the bid
                        applies.</p>
                    <p>The three tables used by the online auction are below,
                        with their column-names indicated in parentheses.</p>
                    <eg> USERS ( USERID, NAME, RATING ) ITEMS ( ITEMNO,
                        DESCRIPTION, OFFERED_BY, START_DATE, END_DATE,
                        RESERVE_PRICE ) BIDS ( USERID, ITEMNO, BID, BID_DATE
                    )</eg>
                </div3>
                <div3 id="rdb-dtd">
                    <head>Document Type Definition (DTD)</head>
                    <p>This use case is based on three separate input documents
                        named users.xml, items.xml, and bids.xml. Each of the
                        documents represents one of the tables in the relational
                        database described above, using the following DTDs:</p>
                    <eg> &lt;!DOCTYPE users [ &lt;!ELEMENT users
                        (user_tuple*)&gt; &lt;!ELEMENT user_tuple
                        (userid, name, rating?)&gt; &lt;!ELEMENT userid
                        (#PCDATA)&gt; &lt;!ELEMENT name
                        (#PCDATA)&gt; &lt;!ELEMENT rating
                        (#PCDATA)&gt; ]&gt; &lt;!DOCTYPE items [
                        &lt;!ELEMENT items (item_tuple*)&gt;
                        &lt;!ELEMENT item_tuple (itemno, description,
                        offered_by, start_date?, end_date?, reserve_price?
                        )&gt; &lt;!ELEMENT itemno (#PCDATA)&gt;
                        &lt;!ELEMENT description (#PCDATA)&gt;
                        &lt;!ELEMENT offered_by (#PCDATA)&gt;
                        &lt;!ELEMENT start_date (#PCDATA)&gt;
                        &lt;!ELEMENT end_date (#PCDATA)&gt;
                        &lt;!ELEMENT reserve_price (#PCDATA)&gt;
                        ]&gt; &lt;!DOCTYPE bids [ &lt;!ELEMENT bids
                        (bid_tuple*)&gt; &lt;!ELEMENT bid_tuple (userid,
                        itemno, bid, bid_date)&gt; &lt;!ELEMENT userid
                        (#PCDATA)&gt; &lt;!ELEMENT itemno
                        (#PCDATA)&gt; &lt;!ELEMENT bid (#PCDATA)&gt;
                        &lt;!ELEMENT bid_date (#PCDATA)&gt;
                    ]&gt;</eg>
                </div3>
                <div3 id="rdb-data">
                    <head>Sample Data</head>
                    <p>Here is an abbreviated set of data showing the XML format
                        of the instances:</p>
                    <eg role="data"><![CDATA[<items>
  <item_tuple>
    <itemno>1001</itemno>
    <description>Red Bicycle</description>
    <offered_by>U01</offered_by>
    <start_date>1999-01-05</start_date>
    <end_date>1999-01-20</end_date>
    <reserve_price>40</reserve_price>
  </item_tuple>
  <!-- !!! Snip !!! -->

<users>
  <user_tuple>
    <userid>U01</userid>
    <name>Tom Jones</name>
    <rating>B</rating>
  </user_tuple>
  <!-- !!! Snip !!! -->

<bids>
  <bid_tuple> 
    <userid>U02</userid> 
    <itemno>1001</itemno> 
    <bid>35</bid> 
    <bid_date>1999-01-07</bid_date> 
    </bid_tuple> 
  <bid_tuple> 
  <!-- !!! Snip !!! -->
                    ]]></eg>
                    <p>The entire data set is represented by the following
                        table:</p>
                    <table border="1">
                        <caption>USERS</caption>
                        <thead>
                            <tr>
                                <td>USERID</td>
                                <td>NAME</td>
                                <td>RATING</td>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>U01</td>
                                <td>Tom Jones</td>
                                <td>B</td>
                            </tr>
                            <tr>
                                <td>U02</td>
                                <td>Mary Doe</td>
                                <td>A</td>
                            </tr>
                            <tr>
                                <td>U03</td>
                                <td>Dee Linquent</td>
                                <td>D</td>
                            </tr>
                            <tr>
                                <td>U04</td>
                                <td>Roger Smith</td>
                                <td>C</td>
                            </tr>
                            <tr>
                                <td>U05</td>
                                <td>Jack Sprat</td>
                                <td>B</td>
                            </tr>
                            <tr>
                                <td>U06</td>
                                <td>Rip Van Winkle</td>
                                <td>B</td>
                            </tr>
                        </tbody>
                    </table>
                    <table border="1">
                        <caption>ITEMS</caption>
                        <thead>
                            <tr>
                                <td>ITEMNO</td>
                                <td>DESCRIPTION</td>
                                <td>OFFERED_BY</td>
                                <td>START_DATE</td>
                                <td>END_DATE</td>
                                <td>RESERVE_PRICE</td>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>1001</td>
                                <td>Red Bicycle</td>
                                <td>U01</td>
                                <td>1999-01-05</td>
                                <td>1999-01-20</td>
                                <td>40</td>
                            </tr>
                            <tr>
                                <td>1002</td>
                                <td>Motorcycle</td>
                                <td>U02</td>
                                <td>1999-02-11</td>
                                <td>1999-03-15</td>
                                <td>500</td>
                            </tr>
                            <tr>
                                <td>1003</td>
                                <td>Old Bicycle</td>
                                <td>U02</td>
                                <td>1999-01-10</td>
                                <td>1999-02-20</td>
                                <td>25</td>
                            </tr>
                            <tr>
                                <td>1004</td>
                                <td>Tricycle</td>
                                <td>U01</td>
                                <td>1999-02-25</td>
                                <td>1999-03-08</td>
                                <td>15</td>
                            </tr>
                            <tr>
                                <td>1005</td>
                                <td>Tennis Racket</td>
                                <td>U03</td>
                                <td>1999-03-19</td>
                                <td>1999-04-30</td>
                                <td>20</td>
                            </tr>
                            <tr>
                                <td>1006</td>
                                <td>Helicopter</td>
                                <td>U03</td>
                                <td>1999-05-05</td>
                                <td>1999-05-25</td>
                                <td>50000</td>
                            </tr>
                            <tr>
                                <td>1007</td>
                                <td>Racing Bicycle</td>
                                <td>U04</td>
                                <td>1999-01-20</td>
                                <td>1999-02-20</td>
                                <td>200</td>
                            </tr>
                            <tr>
                                <td>1008</td>
                                <td>Broken Bicycle</td>
                                <td>U01</td>
                                <td>1999-02-05</td>
                                <td>1999-03-06</td>
                                <td>25</td>
                            </tr>
                        </tbody>
                    </table>
                    <table border="1">
                        <caption>BIDS</caption>
                        <thead>
                            <tr>
                                <td>USERID</td>
                                <td>ITEMNO</td>
                                <td>BID</td>
                                <td>BID_DATE</td>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>U02</td>
                                <td>1001</td>
                                <td>35</td>
                                <td>1999-01-07</td>
                            </tr>
                            <tr>
                                <td>U04</td>
                                <td>1001</td>
                                <td>40</td>
                                <td>1999-01-08</td>
                            </tr>
                            <tr>
                                <td>U02</td>
                                <td>1001</td>
                                <td>45</td>
                                <td>1999-01-11</td>
                            </tr>
                            <tr>
                                <td>U04</td>
                                <td>1001</td>
                                <td>50</td>
                                <td>1999-01-13</td>
                            </tr>
                            <tr>
                                <td>U02</td>
                                <td>1001</td>
                                <td>55</td>
                                <td>1999-01-15</td>
                            </tr>
                            <tr>
                                <td>U01</td>
                                <td>1002</td>
                                <td>400</td>
                                <td>1999-02-14</td>
                            </tr>
                            <tr>
                                <td>U02</td>
                                <td>1002</td>
                                <td>600</td>
                                <td>1999-02-16</td>
                            </tr>
                            <tr>
                                <td>U03</td>
                                <td>1002</td>
                                <td>800</td>
                                <td>1999-02-17</td>
                            </tr>
                            <tr>
                                <td>U04</td>
                                <td>1002</td>
                                <td>1000</td>
                                <td>1999-02-25</td>
                            </tr>
                            <tr>
                                <td>U02</td>
                                <td>1002</td>
                                <td>1200</td>
                                <td>1999-03-02</td>
                            </tr>
                            <tr>
                                <td>U04</td>
                                <td>1003</td>
                                <td>15</td>
                                <td>1999-01-22</td>
                            </tr>
                            <tr>
                                <td>U05</td>
                                <td>1003</td>
                                <td>20</td>
                                <td>1999-02-03</td>
                            </tr>
                            <tr>
                                <td>U01</td>
                                <td>1004</td>
                                <td>40</td>
                                <td>1999-03-05</td>
                            </tr>
                            <tr>
                                <td>U03</td>
                                <td>1007</td>
                                <td>175</td>
                                <td>1999-01-25</td>
                            </tr>
                            <tr>
                                <td>U05</td>
                                <td>1007</td>
                                <td>200</td>
                                <td>1999-02-08</td>
                            </tr>
                            <tr>
                                <td>U04</td>
                                <td>1007</td>
                                <td>225</td>
                                <td>1999-02-12</td>
                            </tr>
                        </tbody>
                    </table>
                </div3>
                <div3 id="rdb-queries-results">
                    <head>Queries and Results</head>
                    <div4 id="rdb-queries-results-q1">
                        <head>Q1</head>
                        <p> List the item number and description of all bicycles
                            that currently have an auction in progress, ordered
                            by item number.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  {
    for $i in doc("items.xml")//item_tuple
    where $i/start_date <= current-date()
      and $i/end_date >= current-date() 
      and contains($i/description, "Bicycle")
    order by $i/itemno
    return
        <item_tuple>
            { $i/itemno }
            { $i/description }
        </item_tuple>
  }
</result>]]></eg>
                        <note>
                            <p>This solution assumes that the current date is
                                1999-01-31.</p>
                        </note>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[
<result>
    <item_tuple>
        <itemno>1003</itemno>
        <description>Old Bicycle</description>
    </item_tuple>
    <item_tuple>
        <itemno>1007</itemno>
        <description>Racing Bicycle</description>
    </item_tuple>
</result>]]></eg>
                        <p>The above query returns an element named
                                <code>item_tuple</code>, but its definition does
                            not match the definition of item_tuple in the DTD.
                        </p>
                    </div4>
                    <div4 id="rdb-queries-results-q2">
                        <head>Q2</head>
                        <p> For all bicycles, list the item number, description,
                            and highest bid (if any), ordered by item number.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  {
    for $i in doc("items.xml")//item_tuple
    let $b := doc("bids.xml")//bid_tuple[itemno = $i/itemno]
    where contains($i/description, "Bicycle")
    order by $i/itemno
    return
        <item_tuple>
            { $i/itemno }
            { $i/description }
            <high_bid>{ max($b/bid) }</high_bid>
        </item_tuple>
  }
</result> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<result>
  <item_tuple>
    <itemno>1001</itemno>
    <description>Red Bicycle</description>
    <high_bid>55</high_bid>
  </item_tuple>
  <item_tuple>
    <itemno>1003</itemno>
    <description>Old Bicycle</description>
    <high_bid>20</high_bid>
  </item_tuple>
  <item_tuple>
    <itemno>1007</itemno>
    <description>Racing Bicycle</description>
    <high_bid>225</high_bid>
  </item_tuple>
  <item_tuple>
    <itemno>1008</itemno>
    <description>Broken Bicycle</description>
    <high_bid></high_bid>
  </item_tuple>
</result> ]]></eg>
                    </div4>
                    <div4 id="rdb-queries-results-q3">
                        <head>Q3</head>
                        <p> Find cases where a user with a rating worse
                            (alphabetically, greater) than "C" is offering an
                            item with a reserve price of more than 1000.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  {
    for $u in doc("users.xml")//user_tuple
    for $i in doc("items.xml")//item_tuple
    where $u/rating > "C" 
       and $i/reserve_price > 1000 
       and $i/offered_by = $u/userid
    return
        <warning>
            { $u/name }
            { $u/rating }
            { $i/description }
            { $i/reserve_price }
        </warning>
  }
</result>]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<result>
    <warning>
        <name>Dee Linquent</name>
        <rating>D</rating>
        <description>Helicopter</description>
        <reserve_price>50000</reserve_price>
    </warning>
</result>]]></eg>
                    </div4>
                    <div4 id="rdb-queries-results-q4">
                        <head>Q4</head>
                        <p> List item numbers and descriptions of items that
                            have no bids.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  {
    for $i in doc("items.xml")//item_tuple
    where empty(doc("bids.xml")//bid_tuple[itemno = $i/itemno])
    return
        <no_bid_item>
            { $i/itemno }
            { $i/description }
        </no_bid_item>
  }
</result> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[
<result>
    <no_bid_item>
        <itemno>1005</itemno>
        <description>Tennis Racket</description>
    </no_bid_item>
    <no_bid_item>
        <itemno>1006</itemno>
        <description>Helicopter</description>
    </no_bid_item>
    <no_bid_item>
        <itemno>1008</itemno>
        <description>Broken Bicycle</description>
    </no_bid_item>
</result>]]></eg>
                    </div4>
                    <div4 id="rdb-queries-results-q5">
                        <head>Q5</head>
                        <p>For bicycle(s) offered by Tom Jones that have
                            received a bid, list the item number, description,
                            highest bid, and name of the highest bidder, ordered
                            by item number.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  {
    for $seller in doc("users.xml")//user_tuple,
        $buyer in  doc("users.xml")//user_tuple,
        $item in  doc("items.xml")//item_tuple,
        $highbid in  doc("bids.xml")//bid_tuple
    where $seller/name = "Tom Jones"
      and $seller/userid  = $item/offered_by
      and contains($item/description , "Bicycle")
      and $item/itemno  = $highbid/itemno
      and $highbid/userid  = $buyer/userid
      and $highbid/bid = max(
                              doc("bids.xml")//bid_tuple
                                [itemno = $item/itemno]/bid
                         )
    order by ($item/itemno)
    return
        <jones_bike>
            { $item/itemno }
            { $item/description }
            <high_bid>{ $highbid/bid }</high_bid>
            <high_bidder>{ $buyer/name }</high_bidder>
        </jones_bike>
  }
</result> ]]></eg>
                        <p>The above query does several joins, and requires the
                            results in a particular order. If there were no
                                <code>order by</code> clause, results would be
                            reported in document order. If you do not care about
                            the order, you can use the <code>unordered</code>
                            function to inform the query processor that the
                            order of the lists in the for clause is not
                            significant, which means that the tuples can be
                            generated in any order. This can enable better
                            optimization. </p>
                        <p>
                            <emph>Unordered Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  {
   unordered (
    for $seller in doc("users.xml")//user_tuple,
        $buyer in doc("users.xml")//user_tuple,
        $item in doc("items.xml")//item_tuple,
        $highbid in  doc("bids.xml")//bid_tuple
    where $seller/name = "Tom Jones"
      and $seller/userid  = $item/offered_by
      and contains($item/description , "Bicycle")
      and $item/itemno  = $highbid/itemno
      and $highbid/userid  = $buyer/userid
      and $highbid/bid = max(
                              doc("bids.xml")//bid_tuple
                                [itemno = $item/itemno]/bid
                         )
    return
        <jones_bike>
            { $item/itemno }
            { $item/description }
            <high_bid>{ $highbid/bid }</high_bid>
            <high_bidder>{ $buyer/name }</high_bidder>
        </jones_bike>
    )
  }
</result> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[
<result>
    <jones_bike>
        <itemno>1001</itemno>
        <description>Red Bicycle</description>
        <high_bid>
            <bid>55</bid>
        </high_bid>
        <high_bidder>
            <name>Mary Doe</name>
        </high_bidder>
    </jones_bike>
</result>]]></eg>
                    </div4>
                    <div4 id="rdb-queries-results-q6">
                        <head>Q6</head>
                        <p>For each item whose highest bid is more than twice
                            its reserve price, list the item number,
                            description, reserve price, and highest bid.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  {
    for $item in doc("items.xml")//item_tuple
    let $b := doc("bids.xml")//bid_tuple[itemno = $item/itemno]
    let $z := max($b/bid)
    where $item/reserve_price * 2 < $z
    return
        <successful_item>
            { $item/itemno }
            { $item/description }
            { $item/reserve_price }
            <high_bid>{$z }</high_bid>
         </successful_item>
  }
</result> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[
<result>
    <successful_item>
        <itemno>1002</itemno>
        <description>Motorcycle</description>
        <reserve_price>500</reserve_price>
        <high_bid>1200</high_bid>
    </successful_item>
    <successful_item>
        <itemno>1004</itemno>
        <description>Tricycle</description>
        <reserve_price>15</reserve_price>
        <high_bid>40</high_bid>
    </successful_item>
</result> ]]></eg>
                    </div4>
                    <div4 id="rdb-queries-results-q7">
                        <head>Q7</head>
                        <p>Find the highest bid ever made for a bicycle or
                            tricycle.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[let $allbikes := doc("items.xml")//item_tuple
                    [contains(description, "Bicycle") 
                     or contains(description, "Tricycle")]
let $bikebids := doc("bids.xml")//bid_tuple[itemno = $allbikes/itemno]
return
    <high_bid>
      { 
        max($bikebids/bid) 
      }
    </high_bid> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"
                        ><![CDATA[<high_bid>225</high_bid>]]></eg>
                    </div4>
                    <div4 id="rdb-queries-results-q8">
                        <head>Q8</head>
                        <p>How many items were actioned (auction ended) in March
                            1999?</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[let $item := doc("items.xml")//item_tuple
  [end_date >= xs:date("1999-03-01") and end_date <= xs:date("1999-03-31")]
return
    <item_count>
      { 
        count($item) 
      }
    </item_count>]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"
                        ><![CDATA[<item_count>3</item_count>]]></eg>
                    </div4>
                    <div4 id="rdb-queries-results-q9">
                        <head>Q9</head>
                        <p> List the number of items auctioned each month in
                            1999 for which data is available, ordered by month.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  {
    let $end_dates := doc("items.xml")//item_tuple/end_date
    for $m in distinct-values(for $e in $end_dates 
                              return month-from-date($e))
    let $item := doc("items.xml")
        //item_tuple[year-from-date(end_date) = 1999 
                     and month-from-date(end_date) = $m]
    order by $m
    return
        <monthly_result>
            <month>{ $m }</month>
            <item_count>{ count($item) }</item_count>
        </monthly_result>
  }
</result>]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<result>
    <monthly_result>
        <month>1</month>
        <item_count>1</item_count>
    </monthly_result>
    <monthly_result>
        <month>2</month>
        <item_count>2</item_count>
    </monthly_result>
    <monthly_result>
        <month>3</month>
        <item_count>3</item_count>
    </monthly_result>
    <monthly_result>
        <month>4</month>
        <item_count>1</item_count>
    </monthly_result>
    <monthly_result>
        <month>5</month>
        <item_count>1</item_count>
    </monthly_result>
</result> ]]></eg>
                    </div4>
                    <div4 id="rdb-queries-results-q10">
                        <head>Q10</head>
                        <p>For each item that has received a bid, list the item
                            number, the highest bid, and the name of the highest
                            bidder, ordered by item number.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
 {
    for $highbid in doc("bids.xml")//bid_tuple,
        $user in doc("users.xml")//user_tuple
    where $user/userid = $highbid/userid 
      and $highbid/bid = max(doc("bids.xml")//bid_tuple[itemno=$highbid/itemno]/bid)
    order by $highbid/itemno
    return
        <high_bid>
            { $highbid/itemno }
            { $highbid/bid }
            <bidder>{ $user/name/text() }</bidder>
        </high_bid>
  }
</result> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[
<result>
    <high_bid>
        <itemno>1001</itemno>
        <bid>55</bid>
        <bidder>Mary Doe</bidder>
    </high_bid>
    <high_bid>
        <itemno>1002</itemno>
        <bid>1200</bid>
        <bidder>Mary Doe</bidder>
    </high_bid>
    <high_bid>
        <itemno>1003</itemno>
        <bid>20</bid>
        <bidder>Jack Sprat</bidder>
    </high_bid>
    <high_bid>
        <itemno>1004</itemno>
        <bid>40</bid>
        <bidder>Tom Jones</bidder>
    </high_bid>
    <high_bid>
        <itemno>1007</itemno>
        <bid>225</bid>
        <bidder>Roger Smith</bidder>
    </high_bid>
</result> ]]></eg>
                    </div4>
                    <div4 id="rdb-queries-results-q11">
                        <head>Q11</head>
                        <p>List the item number and description of the item(s)
                            that received the highest bid ever recorded, and the
                            amount of that bid.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[let $highbid := max(doc("bids.xml")//bid_tuple/bid)
return
    <result>
     {
        for $item in doc("items.xml")//item_tuple,
            $b in doc("bids.xml")//bid_tuple[itemno = $item/itemno]
        where $b/bid = $highbid
        return
            <expensive_item>
                { $item/itemno }
                { $item/description }
                <high_bid>{ $highbid }</high_bid>
            </expensive_item>
     }
    </result> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[
<result>
    <expensive_item>
        <itemno>1002</itemno>
        <description>Motorcycle</description>
        <high_bid>1200</high_bid>
    </expensive_item>
</result> ]]></eg>
                    </div4>
                    <div4 id="rdb-queries-results-q12">
                        <head>Q12</head>
                        <p>List the item number and description of the item(s)
                            that received the largest number of bids, and the
                            number of bids it (or they) received.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[declare function local:bid_summary()
  as element()*
{
    for $i in distinct-values(doc("bids.xml")//itemno)
    let $b := doc("bids.xml")//bid_tuple[itemno = $i]
    return
        <bid_count>
            <itemno>{ $i }</itemno>
            <nbids>{ count($b) }</nbids>
        </bid_count>
};

<result>
 {
    let $bid_counts := local:bid_summary(),
        $maxbids := max($bid_counts/nbids),
        $maxitemnos := $bid_counts[nbids = $maxbids]
    for $item in doc("items.xml")//item_tuple,
        $bc in $bid_counts
    where $bc/nbids =  $maxbids and $item/itemno = $bc/itemno
    return
        <popular_item>
            { $item/itemno }
            { $item/description }
            <bid_count>{ $bc/nbids/text() }</bid_count>
        </popular_item>
 }
</result> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<result>
    <popular_item>
        <itemno>1001</itemno>
        <description>Red Bicycle</description>
        <bid_count>5</bid_count>
    </popular_item>
    <popular_item>
        <itemno>1002</itemno>
        <description>Motorcycle</description>
        <bid_count>5</bid_count>
    </popular_item>
</result> ]]></eg>
                    </div4>
                    <div4 id="rdb-queries-results-q13">
                        <head>Q13</head>
                        <p>For each user who has placed a bid, give the userid,
                            name, number of bids, and average bid, in order by
                            userid.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
 {
    for $uid in distinct-values(doc("bids.xml")//userid),
        $u in doc("users.xml")//user_tuple[userid = $uid]
    let $b := doc("bids.xml")//bid_tuple[userid = $uid]
    order by $u/userid
    return
        <bidder>
            { $u/userid }
            { $u/name }
            <bidcount>{ count($b) }</bidcount>
            <avgbid>{ avg($b/bid) }</avgbid>
        </bidder>
  }
</result> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<result>
    <bidder>
        <userid>U01</userid>
        <name>Tom Jones</name>
        <bidcount>2</bidcount>
        <avgbid>220</avgbid>
    </bidder>
    <bidder>
        <userid>U02</userid>
        <name>Mary Doe</name>
        <bidcount>5</bidcount>
        <avgbid>387</avgbid>
    </bidder>
    <bidder>
        <userid>U03</userid>
        <name>Dee Linquent</name>
        <bidcount>2</bidcount>
        <avgbid>487.5</avgbid>
    </bidder>
    <bidder>
        <userid>U04</userid>
        <name>Roger Smith</name>
        <bidcount>5</bidcount>
        <avgbid>266</avgbid>
    </bidder>
    <bidder>
        <userid>U05</userid>
        <name>Jack Sprat</name>
        <bidcount>2</bidcount>
        <avgbid>110</avgbid>
    </bidder>
</result> ]]></eg>
                    </div4>
                    <div4 id="rdb-queries-results-q14">
                        <head>Q14</head>
                        <p>List item numbers and average bids for items that
                            have received three or more bids, in descending
                            order by average bid.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
 {
    for $i in distinct-values(doc("bids.xml")//itemno)
    let $b := doc("bids.xml")//bid_tuple[itemno = $i]
    let $avgbid := avg($b/bid)
    where count($b) >= 3
    order by $avgbid descending
    return
        <popular_item>
            <itemno>{ $i }</itemno>
            <avgbid>{ $avgbid }</avgbid>
        </popular_item>
  }
</result> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[
<result>
    <popular_item>
        <itemno>1002</itemno>
        <avgbid>800</avgbid>
    </popular_item>
    <popular_item>
        <itemno>1007</itemno>
        <avgbid>200</avgbid>
    </popular_item>
    <popular_item>
        <itemno>1001</itemno>
        <avgbid>45</avgbid>
    </popular_item>
</result>]]></eg>
                    </div4>
                    <div4 id="rdb-queries-results-q15">
                        <head>Q15</head>
                        <p>List names of users who have placed multiple bids of
                            at least $100 each.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  {
    for $u in doc("users.xml")//user_tuple
    let $b := doc("bids.xml")//bid_tuple[userid=$u/userid and bid>=100]
    where count($b) > 1
    return
        <big_spender>{ $u/name/text() }</big_spender>
  }
</result>
                        ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[
<result>
    <big_spender>Mary Doe</big_spender>
    <big_spender>Dee Linquent</big_spender>
    <big_spender>Roger Smith</big_spender>
</result>]]></eg>
                    </div4>
                    <div4 id="rdb-queries-results-q16">
                        <head>Q16</head>
                        <p>List all registered users in order by userid; for
                            each user, include the userid, name, and an
                            indication of whether the user is active (has at
                            least one bid on record) or inactive (has no bid on
                            record).</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  {
    for $u in doc("users.xml")//user_tuple
    let $b := doc("bids.xml")//bid_tuple[userid = $u/userid]
    order by $u/userid
    return
        <user>
            { $u/userid }
            { $u/name }
            {
                if (empty($b))
                  then <status>inactive</status>
                  else <status>active</status>
            }
        </user>
  }
</result>
                        ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[
<result>
    <user>
        <userid>U01</userid>
        <name>Tom Jones</name>
        <status>active</status>
    </user>
    <user>
        <userid>U02</userid>
        <name>Mary Doe</name>
        <status>active</status>
    </user>
    <user>
        <userid>U03</userid>
        <name>Dee Linquent</name>
        <status>active</status>
    </user>
    <user>
        <userid>U04</userid>
        <name>Roger Smith</name>
        <status>active</status>
    </user>
    <user>
        <userid>U05</userid>
        <name>Jack Sprat</name>
        <status>active</status>
    </user>
    <user>
        <userid>U06</userid>
        <name>Rip Van Winkle</name>
        <status>inactive</status>
    </user>
</result>]]></eg>
                    </div4>
                    <div4 id="rdb-queries-results-q17">
                        <head>Q17</head>
                        <p>List the names of users, if any, who have bid on
                            every item.</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<frequent_bidder>
  {
    for $u in doc("users.xml")//user_tuple
    where 
      every $item in doc("items.xml")//item_tuple satisfies 
        some $b in doc("bids.xml")//bid_tuple satisfies 
          ($item/itemno = $b/itemno and $u/userid = $b/userid)
    return
        $u/name
  }
</frequent_bidder>]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[<frequent_bidder /> ]]></eg>
                        <p>(No users satisfy Q17.)</p>
                    </div4>
                    <div4 id="rdb-queries-results-q18">
                        <head>Q18</head>
                        <p>List all users in alphabetic order by name. For each
                            user, include descriptions of all the items (if any)
                            that were bid on by that user, in alphabetic order. </p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  {
    for $u in doc("users.xml")//user_tuple
    order by $u/name
    return
        <user>
            { $u/name }
            {
                for $b in distinct-values(doc("bids.xml")//bid_tuple
                                             [userid = $u/userid]/itemno)
                for $i in doc("items.xml")//item_tuple[itemno = $b]
                let $descr := $i/description/text()
                order by $descr
                return
                    <bid_on_item>{ $descr }</bid_on_item>
            }
        </user>
  }
</result>
                        ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result"><![CDATA[
<result>
    <user>
        <name>Dee Linquent</name>
        <bid_on_item>Motorcycle</bid_on_item>
        <bid_on_item>Racing Bicycle</bid_on_item>
    </user>
    <user>
        <name>Jack Sprat</name>
        <bid_on_item>Old Bicycle</bid_on_item>
        <bid_on_item>Racing Bicycle</bid_on_item>
    </user>
    <user>
        <name>Mary Doe</name>
        <bid_on_item>Motorcycle</bid_on_item>
        <bid_on_item>Red Bicycle</bid_on_item>
    </user>
    <user>
        <name>Rip Van Winkle</name>
    </user>
    <user>
        <name>Roger Smith</name>
        <bid_on_item>Motorcycle</bid_on_item>
        <bid_on_item>Old Bicycle</bid_on_item>
        <bid_on_item>Racing Bicycle</bid_on_item>
        <bid_on_item>Red Bicycle</bid_on_item>
    </user>
    <user>
        <name>Tom Jones</name>
        <bid_on_item>Motorcycle</bid_on_item>
        <bid_on_item>Tricycle</bid_on_item>
    </user>
</result>]]></eg>
                    </div4>
                </div3>
            </div2>
            <div2 id="sgml">
                <head>Use Case "SGML": Standard Generalized Markup Language</head>
                <div3 id="sgml-description">
                    <head>Description</head>
                    <p>The example document and queries in this Use Case were
                        first created for a 1992 conference on Standard
                        Generalized Markup Language (SGML). For our use, the
                        Document Type Definition (DTD) and example document have
                        been translated from SGML to XML. </p>
                </div3>
                <div3 id="sgml-dtd">
                    <head>Document Type Definition (DTD)</head>
                    <p>This use case is based on data conforming to the DTD
                        shown below.</p>
                    <eg role="schema"><![CDATA[ <!NOTATION cgm PUBLIC "Computer Graphics Metafile">
 <!NOTATION ccitt PUBLIC "CCITT group 4 raster">

 <!ENTITY % text "(#PCDATA | emph)*">
 <!ENTITY infoflow SYSTEM "infoflow.ccitt" NDATA ccitt>
 <!ENTITY tagexamp SYSTEM "tagexamp.cgm"   NDATA cgm>

 <!ELEMENT report (title, chapter+)>
 <!ELEMENT title %text;>
 <!ELEMENT chapter (title, intro?, section*)>
 <!ATTLIST chapter
               shorttitle CDATA #IMPLIED>
 <!ELEMENT intro (para | graphic)+>
 <!ELEMENT section (title, intro?, topic*)>
 <!ATTLIST section
               shorttitle CDATA #IMPLIED
               sectid ID #IMPLIED>
 <!ELEMENT topic (title, (para | graphic)+)>
 <!ATTLIST topic
               shorttitle CDATA #IMPLIED
               topicid ID #IMPLIED>
 <!ELEMENT para (#PCDATA | emph | xref)*>
 <!ATTLIST para
               security (u | c | s | ts) "u">
 <!ELEMENT emph %text;>
 <!ELEMENT graphic EMPTY>
 <!ATTLIST graphic
               graphname ENTITY #REQUIRED>
 <!ELEMENT xref EMPTY>
 <!ATTLIST xref
               xrefid IDREF #IMPLIED>
                    ]]></eg>
                </div3>
                <div3 id="sgml-data">
                    <head>Sample Data</head>
                    <p>The queries in this use case are based on the following
                        sample data, which is found in the file "sgml.xml". Line
                        numbers have been added to the data to allow the results
                        of queries to be conveniently specified.</p>
                    <eg role="data"><![CDATA[
 0: <!DOCTYPE report SYSTEM "report.dtd">
 1: <report>
 2: <title>Getting started with SGML</title>
 3: <chapter>
 4: <title>The business challenge</title>
 5: <intro>
 6: <para>With the ever-changing and growing global market, companies and
 7: large organizations are searching for ways to become more viable and
 8: competitive. Downsizing and other cost-cutting measures demand more
 9: efficient use of corporate resources. One very important resource is
10: an organization's information.</para>
11: <para>As part of the move toward integrated information management,
12: whole industries are developing and implementing standards for
13: exchanging technical information. This report describes how one such
14: standard, the Standard Generalized Markup Language (SGML), works as
15: part of an overall information management strategy.</para>
16: <graphic graphname="infoflow"/></intro></chapter>
17: <chapter>
18: <title>Getting to know SGML</title>
19: <intro>
20: <para>While SGML is a fairly recent technology, the use of
21: <emph>markup</emph> in computer-generated documents has existed for a
22: while.</para></intro>
23: <section shorttitle="What is markup?">
24: <title>What is markup, or everything you always wanted to know about
25: document preparation but were afraid to ask?</title>
26: <intro>
27: <para>Markup is everything in a document that is not content. The
28: traditional meaning of markup is the manual <emph>marking</emph> up
29: of typewritten text to give instructions for a typesetter or
30: compositor about how to fit the text on a page and what typefaces to
31: use. This kind of markup is known as <emph>procedural markup</emph>.</para></intro>
32: <topic topicid="top1">
33: <title>Procedural markup</title>
34: <para>Most electronic publishing systems today use some form of
35: procedural markup. Procedural markup codes are good for one
36: presentation of the information.</para></topic>
37: <topic topicid="top2">
38: <title>Generic markup</title>
39: <para>Generic markup (also known as descriptive markup) describes the
40: <emph>purpose</emph> of the text in a document. A basic concept of
41: generic markup is that the content of a document must be separate from
42: the style. Generic markup allows for multiple presentations of the
43: information.</para></topic>
44: <topic topicid="top3">
45: <title>Drawbacks of procedural markup</title>
46: <para>Industries involved in technical documentation increasingly
47: prefer generic over procedural markup schemes. When a company changes
48: software or hardware systems, enormous data translation tasks arise,
49: often resulting in errors.</para></topic></section>
50: <section shorttitle="What is SGML?">
51: <title>What <emph>is</emph> SGML in the grand scheme of the universe, anyway?</title>
52: <intro>
53: <para>SGML defines a strict markup scheme with a syntax for defining
54: document data elements and an overall framework for marking up
55: documents.</para>
56: <para>SGML can describe and create documents that are not dependent on
57: any hardware, software, formatter, or operating system. Since SGML documents
58: conform to an international standard, they are portable.</para></intro></section>
59: <section shorttitle="How does SGML work?">
60: <title>How is SGML and would you recommend it to your grandmother?</title>
61: <intro>
62: <para>You can break a typical document into three layers: structure,
63: content, and style. SGML works by separating these three aspects and
64: deals mainly with the relationship between structure and content.</para></intro>
65: <topic topicid="top4">
66: <title>Structure</title>
67: <para>At the heart of an SGML application is a file called the DTD, or
68: Document Type Definition. The DTD sets up the structure of a document,
69: much like a database schema describes the types of information it
70: handles.</para>
71: <para>A database schema also defines the relationships between the
72: various types of data. Similarly, a DTD specifies <emph>rules</emph>
73: to help ensure documents have a consistent, logical structure.</para></topic>
74: <topic topicid="top5">
75: <title>Content</title>
76: <para>Content is the information itself. The method for identifying
77: the information and its meaning within this framework is called
78: <emph>tagging</emph>. Tagging must
79: conform to the rules established in the DTD (see <xref xrefid="top4"/>).</para>
80: <graphic graphname="tagexamp"/></topic>
81: <topic topicid="top6">
82: <title>Style</title>
83: <para>SGML does not standardize style or other processing methods for
84: information stored in SGML.</para></topic></section></chapter>
85: <chapter>
86: <title>Resources</title>
87: <section>
88: <title>Conferences, tutorials, and training</title>
89: <intro>
90: <para>The Graphic Communications Association has been
91: instrumental in the development of SGML. GCA provides conferences,
92: tutorials, newsletters, and publication sales for both members and
93: non-members.</para>
94: <para security="c">Exiled members of the former Soviet Union's secret
95: police, the KGB, have infiltrated the upper ranks of the GCA and are
96: planning the Final Revolution as soon as DSSSL is completed.</para>
97: </intro>
98: </section>
99: </chapter>
100:</report>
                    ]]></eg>
                </div3>
                <div3 id="sgml-queries-results">
                    <head>Queries and Results</head>
                    <div4 id="sgml-queries-results-q1">
                        <head>Q1</head>
                        <p>Locate all paragraphs in the report (all "para"
                            elements occurring anywhere within the "report"
                            element).</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  { 
    doc("sgml.xml")//report//para 
  }
</result>]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <p>Elements whose start-tags are on lines 6, 11, 20, 27,
                            34, 39, 46, 53, 56, 62, 67, 71, 76, 83, 90, 94 </p>
                    </div4>
                    <div4 id="sgml-queries-results-q2">
                        <head>Q2</head>
                        <p>Locate all paragraph elements in an introduction (all
                            "para" elements directly contained within an "intro"
                            element). </p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  { 
    doc("sgml.xml")//intro/para 
  }
</result>]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <p>Elements whose start-tags are on lines 6, 11, 20, 27,
                            53, 56, 62, 90, 94 </p>
                    </div4>
                    <div4 id="sgml-queries-results-q3">
                        <head>Q3</head>
                        <p>Locate all paragraphs in the introduction of a
                            section that is in a chapter that has no
                            introduction (all "para" elements directly contained
                            within an "intro" element directly contained in a
                            "section" element directly contained in a "chapter"
                            element. The "chapter" element must not directly
                            contain an "intro" element). </p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  {
    for $c in doc("sgml.xml")//chapter
    where empty($c/intro)
    return $c/section/intro/para
  }
</result> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <p>Elements whose start-tags are on lines 90, 94 </p>
                    </div4>
                    <div4 id="sgml-queries-results-q4">
                        <head>Q4</head>
                        <p>Locate the second paragraph in the third section in
                            the second chapter (the second "para" element
                            occurring in the third "section" element occurring
                            in the second "chapter" element occurring in the
                            "report"). </p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  {
    (((doc("sgml.xml")//chapter)[2]//section)[3]//para)[2]
  }
</result> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <p>Element whose start-tag is on line 67 </p>
                    </div4>
                    <div4 id="sgml-queries-results-q5">
                        <head>Q5</head>
                        <p>Locate all classified paragraphs (all "para" elements
                            whose "security" attribute has the value "c").</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  {
    doc("sgml.xml")//para[@security = "c"]
  }
</result> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <p>Element whose start-tag is on line 94 </p>
                    </div4>
                    <div4 id="sgml-queries-results-q6">
                        <head>Q6</head>
                        <p>List the short titles of all sections (the values of
                            the "shorttitle" attributes of all "section"
                            elements, expressing each short title as the value
                            of a new element.) </p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  {
    for $s in doc("sgml.xml")//section/@shorttitle
    return <stitle>{ $s }</stitle>
  }
</result> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <p>Attribute values in start-tags on lines 23, 50, 59
                        </p>
                    </div4>
                    <div4 id="sgml-queries-results-q7">
                        <head>Q7</head>
                        <p> Locate the initial letter of the initial paragraph
                            of all introductions (the first character in the
                            content [character content as well as element
                            content] of the first "para" element contained in an
                            "intro" element). </p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  {
    for $i in doc("sgml.xml")//intro/para[1]
    return
        <first_letter>{ substring(string($i), 1, 1) }</first_letter>
  }
</result> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <p>Character after start-tag on lines 6, 20, 27, 53, 62,
                            90</p>
                    </div4>
                    <div4 id="sgml-queries-results-q8a">
                        <head>Q8a</head>
                        <p>Locate all sections with a title that has "is SGML"
                            in it. The string may occur anywhere in the
                            descendants of the title element, and markup
                            boundaries are ignored. </p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
   {
     doc("sgml.xml")//section[.//title[contains(., "is SGML")]]
   }
</result> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <p>Elements whose start-tags are on lines 50, 59 </p>
                    </div4>
                    <div4 id="sgml-queries-results-q8b">
                        <head>Q8b</head>
                        <p>Same as (Q8a), but the string "is SGML" cannot be
                            interrupted by sub-elements, and must appear in a
                            single text node. </p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
   {
     doc("sgml.xml")//section[.//title/text()[contains(., "is SGML")]]
   }
</result> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <p>Element whose start-tag is on line 59</p>
                    </div4>
                    <div4 id="sgml-queries-results-q9">
                        <head>Q9</head>
                        <p> Locate all the topics referenced by a
                            cross-reference anywhere in the report (all the
                            "topic" elements whose "topicid" attribute value is
                            the same as an "xrefid" attribute value of any
                            "xref" element). </p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  {
    for $id in doc("sgml.xml")//xref/@xrefid
    return doc("sgml.xml")//topic[@topicid = $id]
  }
</result> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <p>Element whose start-tag is on line 65 </p>
                    </div4>
                    <div4 id="sgml-queries-results-q10">
                        <head>Q10</head>
                        <p>Locate the closest title preceding the
                            cross-reference ("xref") element whose "xrefid"
                            attribute is "top4" (the "title" element that would
                            be touched last before this "xref" element when
                            touching each element in document order).</p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"><![CDATA[<result>
  {
    let $x := doc("sgml.xml")//xref[@xrefid = "top4"],
        $t := doc("sgml.xml")//title[. << $x]
    return $t[last()]
  }
</result> ]]></eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <p>Given xref on line 79, element whose start-tag is on
                            line 75 </p>
                    </div4>
                </div3>
            </div2>
            <div2 id="text">
                <head>Use Case "STRING": String Search</head>
                <div3 id="text-description">
                    <head>Description</head>
                    <p>This use case is based on company profiles and a set of
                        news documents which contain data for PR, mergers and
                        acquisitions, etc. Given a company, the use case
                        illustrates several different queries for searching text
                        in news documents and different ways of providing query
                        results by matching the information from the company
                        profile and the content of the news items. </p>
                    <p>In this use case, the <code>contains</code> function is
                        used to test whether a string occurs within a node or a
                        string. Obviously, using full-text functions would
                        provide more powerful searching, but the current
                        Functions and Operators draft does not have full-text
                        functions.</p>
                </div3>
                <div3 id="text-dtd">
                    <head>Document Type Definition (DTD)</head>
                    <p>This use case uses data that corresponds to the following
                        DTDs: </p>
                    <eg role="schema"><![CDATA[<!ELEMENT company (name, ticker_symbol?, description?,
          business_code, partners?, competitors?)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT ticker_symbol (#PCDATA)>
<!ELEMENT description (#PCDATA)>
<!ELEMENT business_code (#PCDATA)>
<!ELEMENT partners (partner+)>
<!ELEMENT partner (#PCDATA)>
<!ELEMENT competitors (competitor+)>
<!ELEMENT competitor (#PCDATA)>
                    ]]></eg>
                    <p/>
                    <eg role="schema"><![CDATA[<!ELEMENT news (news_item*)>
<!ELEMENT news_item (title, content, date, author?, news_agent)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT content (par | figure)+ >
<!ELEMENT date (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT news_agent (#PCDATA)>
<!ELEMENT par (#PCDATA | quote | footnote)*>
<!ELEMENT quote (#PCDATA)>
<!ELEMENT footnote (#PCDATA)>
<!ELEMENT figure (title, image)>
<!ELEMENT image EMPTY>
<!ATTLIST image
    source  CDATA  #REQUIRED >
                    ]]></eg>
                </div3>
                <div3 id="text-data">
                    <head>Sample Data</head>
                    <p>The queries in this use case are based on the following
                        input data, which is found in the file "string.xml".</p>
                    <eg role="data"><![CDATA[<?xml version="1.0" encoding="ISO-8859-1"?>
<news>
<news_item>
   <title> Gorilla Corporation acquires YouNameItWeIntegrateIt.com </title>
   <content>
      <par> Today, Gorilla Corporation announced that it will purchase
          YouNameItWeIntegrateIt.com. The shares of
          YouNameItWeIntegrateIt.com dropped $3.00 as a result of this
          announcement.
      </par>

      <par> As a result of this acquisition, the CEO of
          YouNameItWeIntegrateIt.com Bill Smarts resigned. He did not
          announce what he will do next.  Sources close to
          YouNameItWeIntegrateIt.com hint that Bill Smarts might be
          taking a position in Foobar Corporation.
      </par>

      <par>YouNameItWeIntegrateIt.com is a leading systems integrator
          that enables <quote>brick and mortar</quote> companies to
          have a presence on the web.
      </par>

   </content>
   <date>1-20-2000</date>
   <author>Mark Davis</author>
   <news_agent>News Online</news_agent>
</news_item>

<news_item>
   <title>Foobar Corporation releases its new line of Foo products
   today</title>
   <content>
      <par> Foobar Corporation releases the 20.9 version of its Foo
            products.  The new version of Foo products solve known
            performance problems which existed in 20.8 line and
            increases the speed of Foo based products tenfold. It also
            allows wireless clients to be connected to the Foobar
            servers.
      </par>
      <par> The President of Foobar Corporation announced that they
            were proud to release 20.9 version of Foo products and
            they will upgrade existing customers <footnote>where
            service agreements exist</footnote>
            promptly. TheAppCompany Inc. immediately announced that it
            will release the new version of its products to utilize
            the 20.9 architecture within the next three months.
      </par>
      <figure>
          <title>Presidents of Foobar Corporation and TheAppCompany
          Inc. Shake Hands</title> <image source="handshake.jpg"/>
      </figure>
   </content>
   <date>1-20-2000</date>
   <news_agent>Foobar Corporation</news_agent>
</news_item>

<news_item> <title>Foobar Corporation is suing Gorilla Corporation for
   patent infringement </title>
   <content>
      <par> In surprising developments today, Foobar Corporation
         announced that it is suing Gorilla Corporation for patent
         infringement. The patents that were mentioned as part of the
         lawsuit are considered to be the basis of Foobar
         Corporation's <quote>Wireless Foo</quote> line of products.
      </par>
      <par>The tension between Foobar and Gorilla Corporations has
         been increasing ever since the Gorilla Corporation acquired
         more than 40 engineers who have left Foobar Corporation,
         TheAppCompany Inc. and YouNameItWeIntegrateIt.com over the
         past 3 months. The engineers who have left the Foobar
         corporation and its partners were rumored to be working on
         the next generation of server products and applications which
         will directly compete with Foobar's Foo 20.9 servers. Most of
         the engineers have relocated to Hawaii where the Gorilla
         Corporation's server development is located.
      </par>
   </content>
   <date>1-20-2000</date>
   <news_agent>Reliable News Corporation</news_agent>
</news_item>
</news>
                    ]]></eg>
                    <p>In addition, the following data, listing the partners and
                        competitors of companies, is found in the file
                        "company-data.xml".</p>
                    <eg role="data"><![CDATA[<?xml version="1.0" encoding="ISO-8859-1"?> 
<!DOCTYPE company SYSTEM "company.dtd">
<company>
   <name>Foobar Corporation</name>
   <ticker_symbol>FOO</ticker_symbol>

   <description>Foobar Corporation is a maker of Foo(TM) and
        Foobar(TM) products and a leading software company with a 300
        Billion dollar revenue in 1999. It is located in Alaska.
   </description>

   <business_code>Software</business_code>
   <partners>
        <partner>YouNameItWeIntegrateIt.com</partner>
        <partner>TheAppCompany Inc.</partner>
   </partners>
   <competitors>
        <competitor>Gorilla Corporation</competitor>
   </competitors>
</company>
                    ]]></eg>
                </div3>
                <div3 id="text-queries-results">
                    <head>Queries and Results</head>
                    <div4 id="text-queries-results-q1">
                        <head>Q1</head>
                        <p>Find the titles of all news items where the string
                            "Foobar Corporation" appears in the title. </p>
                        <p>
                            <emph>Solution in XQuery:</emph>
                        </p>
                        <eg role="xquery"
                            ><![CDATA[doc("string.xml")//news_item/title[contains(., "Foobar Corporation")] ]]></eg>
                        <p>
                            <emph>Expected Results</emph>
                        </p>
                        <eg role="result"><![CDATA[<title>Foobar Corporation releases its new line of Foo
products today</title>
<title>Foobar Corporation is suing Gorilla Corporation for patent
infringement </title> ]]></eg>
                    </div4>
                    <div4 id="text-queries-results-q2">
                        <head>Q2</head>
                        <p>Find news items where the Foobar Corporation and one
                            or more of its partners are mentioned in the same
                            paragraph and/or title. List each news item by its
                            title and date. </p>
                        <p>
                            <emph>Solution in XQuery:</emph>