<!--

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

--><spec w3c-doctype="wd">
    <header>
        <title>XML Query Use Cases</title>
        <version/>
        <w3c-designation>WD-xquery-use-cases-20060608</w3c-designation>
        <w3c-doctype>W3C Working Draft</w3c-doctype>
        <pubdate>
            <day>8</day>
            <month>June</month>
            <year>2006</year>
        </pubdate>
        <publoc>
            <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/2006/WD-xquery-use-cases-20060608/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://www.w3.org/TR/2006/WD-xquery-use-cases-20060608/</loc>
        </publoc>
<altlocs>
            <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/2006/WD-xquery-use-cases-20060608/xquery-use-cases.xml" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XML</loc>
            <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/2006/WD-xquery-use-cases-20060608/diff-from-20050915.html" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">Recent revisions</loc>
</altlocs>
        <latestloc>
            <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/xquery-use-cases/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://www.w3.org/TR/xquery-use-cases/</loc>
        </latestloc>
        <prevlocs>
            <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/2005/WD-xquery-use-cases-20050915/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"/>
        </prevlocs>
        <authlist>
            <author>
                <name>Don Chamberlin</name>
                <affiliation>IBM Almaden Research Center</affiliation>
                <email xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:chamberlin@almaden.ibm.com" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">chamberlin@almaden.ibm.com </email>
            </author>
            <author>
                <name>Peter Fankhauser</name>
                <affiliation>Infonyte GmbH</affiliation>
                <email xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:fankhauser@infonyte.com" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">fankhauser@infonyte.com</email>
            </author>
            <author>
                <name>Daniela Florescu</name>
                <affiliation>Oracle corporation</affiliation>
                <email xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:dana.florescu@oracle.com" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">dana.florescu@oracle.com</email>
            </author>
            <author>
                <name>Massimo Marchiori</name>
                <affiliation>University of Venice</affiliation>
                <email xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:massimo@w3.org" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">massimo@w3.org</email>
            </author>
            <author>
                <name>Jonathan Robie</name>
                <affiliation>DataDirect Technologies</affiliation>
                <email xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:jonathan.robie@datadirect.com" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">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 xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">W3C technical reports 
index</loc> at http://www.w3.org/TR/.</emph></p>


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

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

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



 
<p>Comments on this document are invited and should be made in W3C's <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Bugs/Public/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">public Bugzilla system</loc> 
(instructions can be found at <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/XML/2005/04/qt-bugzilla" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://www.w3.org/XML/2005/04/qt-bugzilla</loc>). 
If access to that system is not feasible, you may send your comments to the 
W3C XSLT/XPath/XQuery mailing list, <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:public-qt-comments@w3.org" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">public-qt-comments@w3.org</loc>. It 
will be very helpful if you include the string
[XQueryUseCases]

in the subject line of your comment, whether made in Bugzilla or in email. 
Each Bugzilla entry and email message should contain only one 
comment.  Archives of the comments and responses are available at <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://lists.w3.org/Archives/Public/public-qt-comments/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://lists.w3.org/Archives/Public/public-qt-comments/ 
</loc>.</p>

<p>This document was produced by a group operating under the
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Consortium/Patent-Policy-20040205/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">5 February
2004 W3C Patent Policy</loc>. This document is informative only. W3C
maintains a
<loc xmlns:xlink="http://www.w3.org/1999/xlink" role="disclosure" href="http://www.w3.org/2002/08/xmlquery-IPR-statements" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">public
list of any patent disclosures</loc> made in connection
with the deliverables of the group; that page also includes
instructions for disclosing a patent. An individual who has actual
knowledge of a patent which the individual believes contains
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">Essential
Claim(s)</loc> must disclose the information in accordance with
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">section
6 of the W3C Patent Policy</loc>. </p>
        </status>
        <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 xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/XML/Group/xquery-test/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">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 xml:space="preserve">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 xml:space="preserve">$b//author</eg>
            <p>Some implementations of XQuery predefine a single 'context item',
                which is available at the root level of a query, and which is
                used to resolve paths that begin with a leading slash. In such
                an implementation, if the context item is bound to document node
                of the same well-formed document used in the previous examples,
                this expression would return the same set of authors:</p>
            <eg xml:space="preserve">//author</eg>
            <p>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" xml:space="preserve">&lt;!ELEMENT bib  (book* )&gt;
&lt;!ELEMENT book  (title,  (author+ | editor+ ), publisher, price )&gt;
&lt;!ATTLIST book  year CDATA  #REQUIRED &gt;
&lt;!ELEMENT author  (last, first )&gt;
&lt;!ELEMENT editor  (last, first, affiliation )&gt;
&lt;!ELEMENT title  (#PCDATA )&gt;
&lt;!ELEMENT last  (#PCDATA )&gt;
&lt;!ELEMENT first  (#PCDATA )&gt;
&lt;!ELEMENT affiliation  (#PCDATA )&gt;
&lt;!ELEMENT publisher  (#PCDATA )&gt;
&lt;!ELEMENT price  (#PCDATA )&gt;</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" xml:space="preserve">&lt;bib&gt;
    &lt;book year="1994"&gt;
        &lt;title&gt;TCP/IP Illustrated&lt;/title&gt;
        &lt;author&gt;&lt;last&gt;Stevens&lt;/last&gt;&lt;first&gt;W.&lt;/first&gt;&lt;/author&gt;
        &lt;publisher&gt;Addison-Wesley&lt;/publisher&gt;
        &lt;price&gt;65.95&lt;/price&gt;
    &lt;/book&gt;
 
    &lt;book year="1992"&gt;
        &lt;title&gt;Advanced Programming in the Unix environment&lt;/title&gt;
        &lt;author&gt;&lt;last&gt;Stevens&lt;/last&gt;&lt;first&gt;W.&lt;/first&gt;&lt;/author&gt;
        &lt;publisher&gt;Addison-Wesley&lt;/publisher&gt;
        &lt;price&gt;65.95&lt;/price&gt;
    &lt;/book&gt;
 
    &lt;book year="2000"&gt;
        &lt;title&gt;Data on the Web&lt;/title&gt;
        &lt;author&gt;&lt;last&gt;Abiteboul&lt;/last&gt;&lt;first&gt;Serge&lt;/first&gt;&lt;/author&gt;
        &lt;author&gt;&lt;last&gt;Buneman&lt;/last&gt;&lt;first&gt;Peter&lt;/first&gt;&lt;/author&gt;
        &lt;author&gt;&lt;last&gt;Suciu&lt;/last&gt;&lt;first&gt;Dan&lt;/first&gt;&lt;/author&gt;
        &lt;publisher&gt;Morgan Kaufmann Publishers&lt;/publisher&gt;
        &lt;price&gt;39.95&lt;/price&gt;
    &lt;/book&gt;
 
    &lt;book year="1999"&gt;
        &lt;title&gt;The Economics of Technology and Content for Digital TV&lt;/title&gt;
        &lt;editor&gt;
               &lt;last&gt;Gerbarg&lt;/last&gt;&lt;first&gt;Darcy&lt;/first&gt;
                &lt;affiliation&gt;CITI&lt;/affiliation&gt;
        &lt;/editor&gt;
            &lt;publisher&gt;Kluwer Academic Publishers&lt;/publisher&gt;
        &lt;price&gt;129.95&lt;/price&gt;
    &lt;/book&gt;
 
&lt;/bib&gt;
                    </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" xml:space="preserve">&lt;!ELEMENT reviews (entry*)&gt;
&lt;!ELEMENT entry   (title, price, review)&gt;
&lt;!ELEMENT title   (#PCDATA)&gt;
&lt;!ELEMENT price   (#PCDATA)&gt;
&lt;!ELEMENT review  (#PCDATA)&gt;
 </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" xml:space="preserve">&lt;reviews&gt;
    &lt;entry&gt;
        &lt;title&gt;Data on the Web&lt;/title&gt;
        &lt;price&gt;34.95&lt;/price&gt;
        &lt;review&gt;
               A very good discussion of semi-structured database
               systems and XML.
        &lt;/review&gt;
    &lt;/entry&gt;
    &lt;entry&gt;
        &lt;title&gt;Advanced Programming in the Unix environment&lt;/title&gt;
        &lt;price&gt;65.95&lt;/price&gt;
        &lt;review&gt;
               A clear and detailed discussion of UNIX programming.
        &lt;/review&gt;
    &lt;/entry&gt;
    &lt;entry&gt;
        &lt;title&gt;TCP/IP Illustrated&lt;/title&gt;
        &lt;price&gt;65.95&lt;/price&gt;
        &lt;review&gt;
               One of the best books on TCP/IP.
        &lt;/review&gt;
    &lt;/entry&gt;
&lt;/reviews&gt;</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" xml:space="preserve">&lt;!ELEMENT chapter (title, section*)&gt;
&lt;!ELEMENT section (title, section*)&gt;
&lt;!ELEMENT title (#PCDATA)&gt;
 </eg>
                </div3>
                <div3 id="xmp-data-q9">
                    <head>Data for Q9</head>
                    <p>Here are the contents of books.xml:</p>
                    <eg role="data" xml:space="preserve">&lt;chapter&gt;
    &lt;title&gt;Data Model&lt;/title&gt;
    &lt;section&gt;
        &lt;title&gt;Syntax For Data Model&lt;/title&gt;
    &lt;/section&gt;
    &lt;section&gt;
        &lt;title&gt;XML&lt;/title&gt;
        &lt;section&gt;
            &lt;title&gt;Basic Syntax&lt;/title&gt;
        &lt;/section&gt;
        &lt;section&gt;
            &lt;title&gt;XML and Semistructured Data&lt;/title&gt;
        &lt;/section&gt;
    &lt;/section&gt;
&lt;/chapter&gt;</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" xml:space="preserve">&lt;!ELEMENT prices (book*)&gt;
&lt;!ELEMENT book (title, source, price)&gt;
&lt;!ELEMENT title (#PCDATA)&gt;
&lt;!ELEMENT source (#PCDATA)&gt;
&lt;!ELEMENT price (#PCDATA)&gt;
                    </eg>
                </div3>
                <div3 id="xmp-data-q10">
                    <head>Data for Q10</head>
                    <p>Here are the contents of prices.xml:</p>
                    <eg role="data" xml:space="preserve">&lt;prices&gt;
    &lt;book&gt;
        &lt;title&gt;Advanced Programming in the Unix environment&lt;/title&gt;
        &lt;source&gt;bstore2.example.com&lt;/source&gt;
        &lt;price&gt;65.95&lt;/price&gt;
    &lt;/book&gt;
    &lt;book&gt;
        &lt;title&gt;Advanced Programming in the Unix environment&lt;/title&gt;
        &lt;source&gt;bstore1.example.com&lt;/source&gt;
        &lt;price&gt;65.95&lt;/price&gt;
    &lt;/book&gt;
    &lt;book&gt;
        &lt;title&gt;TCP/IP Illustrated&lt;/title&gt;
        &lt;source&gt;bstore2.example.com&lt;/source&gt;
        &lt;price&gt;65.95&lt;/price&gt;
    &lt;/book&gt;
    &lt;book&gt;
        &lt;title&gt;TCP/IP Illustrated&lt;/title&gt;
        &lt;source&gt;bstore1.example.com&lt;/source&gt;
        &lt;price&gt;65.95&lt;/price&gt;
    &lt;/book&gt;
    &lt;book&gt;
        &lt;title&gt;Data on the Web&lt;/title&gt;
        &lt;source&gt;bstore2.example.com&lt;/source&gt;
        &lt;price&gt;34.95&lt;/price&gt;
    &lt;/book&gt;
    &lt;book&gt;
        &lt;title&gt;Data on the Web&lt;/title&gt;
        &lt;source&gt;bstore1.example.com&lt;/source&gt;
        &lt;price&gt;39.95&lt;/price&gt;
    &lt;/book&gt;
&lt;/prices&gt;</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" xml:space="preserve">&lt;bib&gt;
 {
  for $b in doc("http://bstore1.example.com/bib.xml")/bib/book
  where $b/publisher = "Addison-Wesley" and $b/@year &gt; 1991
  return
    &lt;book year="{ $b/@year }"&gt;
     { $b/title }
    &lt;/book&gt;
 }
&lt;/bib&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;bib&gt;
    &lt;book year="1994"&gt;
        &lt;title&gt;TCP/IP Illustrated&lt;/title&gt;
    &lt;/book&gt;
    &lt;book year="1992"&gt;
        &lt;title&gt;Advanced Programming in the Unix environment&lt;/title&gt;
    &lt;/book&gt;
&lt;/bib&gt;
                        </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" xml:space="preserve">&lt;results&gt;
  {
    for $b in doc("http://bstore1.example.com/bib.xml")/bib/book,
        $t in $b/title,
        $a in $b/author
    return
        &lt;result&gt;
            { $t }    
            { $a }
        &lt;/result&gt;
  }
&lt;/results&gt;
                        </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;results&gt;
    &lt;result&gt;
        &lt;title&gt;TCP/IP Illustrated&lt;/title&gt;
        &lt;author&gt;
            &lt;last&gt;Stevens&lt;/last&gt;
            &lt;first&gt;W.&lt;/first&gt;
        &lt;/author&gt;
    &lt;/result&gt;
    &lt;result&gt;
        &lt;title&gt;Advanced Programming in the Unix environment&lt;/title&gt;
        &lt;author&gt;
            &lt;last&gt;Stevens&lt;/last&gt;
            &lt;first&gt;W.&lt;/first&gt;
        &lt;/author&gt;
    &lt;/result&gt;
    &lt;result&gt;
        &lt;title&gt;Data on the Web&lt;/title&gt;
        &lt;author&gt;
            &lt;last&gt;Abiteboul&lt;/last&gt;
            &lt;first&gt;Serge&lt;/first&gt;
        &lt;/author&gt;
    &lt;/result&gt;
    &lt;result&gt;
        &lt;title&gt;Data on the Web&lt;/title&gt;
        &lt;author&gt;
            &lt;last&gt;Buneman&lt;/last&gt;
            &lt;first&gt;Peter&lt;/first&gt;
        &lt;/author&gt;
    &lt;/result&gt;
    &lt;result&gt;
        &lt;title&gt;Data on the Web&lt;/title&gt;
        &lt;author&gt;
            &lt;last&gt;Suciu&lt;/last&gt;
            &lt;first&gt;Dan&lt;/first&gt;
        &lt;/author&gt;
    &lt;/result&gt;
&lt;/results&gt;</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" xml:space="preserve">&lt;results&gt;
{
    for $b in doc("http://bstore1.example.com/bib.xml")/bib/book
    return
        &lt;result&gt;
            { $b/title }
            { $b/author  }
        &lt;/result&gt;
}
&lt;/results&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;results&gt;
    &lt;result&gt;
        &lt;title&gt;TCP/IP Illustrated&lt;/title&gt;
        &lt;author&gt;
            &lt;last&gt;Stevens&lt;/last&gt;
            &lt;first&gt;W.&lt;/first&gt;
        &lt;/author&gt;
    &lt;/result&gt;
    &lt;result&gt;
        &lt;title&gt;Advanced Programming in the Unix environment&lt;/title&gt;
        &lt;author&gt;
            &lt;last&gt;Stevens&lt;/last&gt;
            &lt;first&gt;W.&lt;/first&gt;
        &lt;/author&gt;
    &lt;/result&gt;
    &lt;result&gt;
        &lt;title&gt;Data on the Web&lt;/title&gt;
        &lt;author&gt;
            &lt;last&gt;Abiteboul&lt;/last&gt;
            &lt;first&gt;Serge&lt;/first&gt;
        &lt;/author&gt;
        &lt;author&gt;
            &lt;last&gt;Buneman&lt;/last&gt;
            &lt;first&gt;Peter&lt;/first&gt;
        &lt;/author&gt;
        &lt;author&gt;
            &lt;last&gt;Suciu&lt;/last&gt;
            &lt;first&gt;Dan&lt;/first&gt;
        &lt;/author&gt;
    &lt;/result&gt;
    &lt;result&gt;
        &lt;title&gt;The Economics of Technology and Content for Digital TV&lt;/title&gt;
    &lt;/result&gt;
&lt;/results&gt;
                        </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" xml:space="preserve">&lt;results&gt;
  {
    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
        &lt;result&gt;
            &lt;author&gt;
               &lt;last&gt;{ $last }&lt;/last&gt;
               &lt;first&gt;{ $first }&lt;/first&gt;
            &lt;/author&gt;
            {
                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
            }
        &lt;/result&gt;
  }
&lt;/results&gt; </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" xml:space="preserve">&lt;results&gt;
    &lt;result&gt;
        &lt;author&gt;
            &lt;last&gt;Abiteboul&lt;/last&gt;
            &lt;first&gt;Serge&lt;/first&gt;
        &lt;/author&gt;
        &lt;title&gt;Data on the Web&lt;/title&gt;
    &lt;/result&gt;
    &lt;result&gt;
        &lt;author&gt;
            &lt;last&gt;Buneman&lt;/last&gt;
            &lt;first&gt;Peter&lt;/first&gt;
        &lt;/author&gt;
        &lt;title&gt;Data on the Web&lt;/title&gt;
    &lt;/result&gt;
    &lt;result&gt;
        &lt;author&gt;
            &lt;last&gt;Stevens&lt;/last&gt;
            &lt;first&gt;W.&lt;/first&gt;
        &lt;/author&gt;
        &lt;title&gt;TCP/IP Illustrated&lt;/title&gt;
        &lt;title&gt;Advanced Programming in the Unix environment&lt;/title&gt;
    &lt;/result&gt;
    &lt;result&gt;
        &lt;author&gt;
            &lt;last&gt;Suciu&lt;/last&gt;
            &lt;first&gt;Dan&lt;/first&gt;
        &lt;/author&gt;
        &lt;title&gt;Data on the Web&lt;/title&gt;
    &lt;/result&gt;
&lt;/results&gt;
                        </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" xml:space="preserve">&lt;books-with-prices&gt;
  {
    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
        &lt;book-with-prices&gt;
            { $b/title }
            &lt;price-bstore2&gt;{ $a/price/text() }&lt;/price-bstore2&gt;
            &lt;price-bstore1&gt;{ $b/price/text() }&lt;/price-bstore1&gt;
        &lt;/book-with-prices&gt;
  }
&lt;/books-with-prices&gt;</eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;books-with-prices&gt;
    &lt;book-with-prices&gt;
        &lt;title&gt;TCP/IP Illustrated&lt;/title&gt;
        &lt;price-bstore2&gt;65.95&lt;/price-bstore2&gt;
        &lt;price-bstore1&gt;65.95&lt;/price-bstore1&gt;
    &lt;/book-with-prices&gt;
    &lt;book-with-prices&gt;
        &lt;title&gt;Advanced Programming in the Unix environment&lt;/title&gt;
        &lt;price-bstore2&gt;65.95&lt;/price-bstore2&gt;
        &lt;price-bstore1&gt;65.95&lt;/price-bstore1&gt;
    &lt;/book-with-prices&gt;
    &lt;book-with-prices&gt;
        &lt;title&gt;Data on the Web&lt;/title&gt;
        &lt;price-bstore2&gt;34.95&lt;/price-bstore2&gt;
        &lt;price-bstore1&gt;39.95&lt;/price-bstore1&gt;
    &lt;/book-with-prices&gt;
&lt;/books-with-prices&gt; </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" xml:space="preserve">&lt;bib&gt;
  {
    for $b in doc("http://bstore1.example.com/bib.xml")//book
    where count($b/author) &gt; 0
    return
        &lt;book&gt;
            { $b/title }
            {
                for $a in $b/author[position()&lt;=2]  
                return $a
            }
            {
                if (count($b/author) &gt; 2)
                 then &lt;et-al/&gt;
                 else ()
            }
        &lt;/book&gt;
  }
&lt;/bib&gt;</eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;bib&gt;
    &lt;book&gt;
        &lt;title&gt;TCP/IP Illustrated&lt;/title&gt;
        &lt;author&gt;
            &lt;last&gt;Stevens&lt;/last&gt;
            &lt;first&gt;W.&lt;/first&gt;
        &lt;/author&gt;
    &lt;/book&gt;
    &lt;book&gt;
        &lt;title&gt;Advanced Programming in the Unix environment&lt;/title&gt;
        &lt;author&gt;
            &lt;last&gt;Stevens&lt;/last&gt;
            &lt;first&gt;W.&lt;/first&gt;
        &lt;/author&gt;
    &lt;/book&gt;
    &lt;book&gt;
        &lt;title&gt;Data on the Web&lt;/title&gt;
        &lt;author&gt;
            &lt;last&gt;Abiteboul&lt;/last&gt;
            &lt;first&gt;Serge&lt;/first&gt;
        &lt;/author&gt;
        &lt;author&gt;
            &lt;last&gt;Buneman&lt;/last&gt;
            &lt;first&gt;Peter&lt;/first&gt;
        &lt;/author&gt;
        &lt;et-al/&gt;
    &lt;/book&gt;
&lt;/bib&gt; </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" xml:space="preserve">&lt;bib&gt;
  {
    for $b in doc("http://bstore1.example.com/bib.xml")//book
    where $b/publisher = "Addison-Wesley" and $b/@year &gt; 1991
    order by $b/title
    return
        &lt;book&gt;
            { $b/@year }
            { $b/title }
        &lt;/book&gt;
  }
&lt;/bib&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;bib&gt;
    &lt;book year="1992"&gt;
        &lt;title&gt;Advanced Programming in the Unix environment&lt;/title&gt;
    &lt;/book&gt;
    &lt;book year="1994"&gt;
        &lt;title&gt;TCP/IP Illustrated&lt;/title&gt;
    &lt;/book&gt;
&lt;/bib&gt;     </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" xml:space="preserve">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
    &lt;book&gt;
        { $b/title }
        { $e }
    &lt;/book&gt; </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" xml:space="preserve">&lt;book&gt;
        &lt;title&gt;Data on the Web&lt;/title&gt;
        &lt;author&gt;
            &lt;last&gt;Suciu&lt;/last&gt;
            &lt;first&gt;Dan&lt;/first&gt;
        &lt;/author&gt;
 &lt;/book&gt;</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" xml:space="preserve">&lt;results&gt;
  {
    for $t in doc("books.xml")//(chapter | section)/title
    where contains($t/text(), "XML")
    return $t
  }
&lt;/results&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;results&gt;
    &lt;title&gt;XML&lt;/title&gt;
    &lt;title&gt;XML and Semistructured Data&lt;/title&gt;
&lt;/results&gt; </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" xml:space="preserve">&lt;results&gt;
  {
    let $doc := doc("prices.xml")
    for $t in distinct-values($doc//book/title)
    let $p := $doc//book[title = $t]/price
    return
      &lt;minprice title="{ $t }"&gt;
        &lt;price&gt;{ min($p) }&lt;/price&gt;
      &lt;/minprice&gt;
  }
&lt;/results&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;results&gt;
    &lt;minprice title="Advanced Programming in the Unix environment"&gt;
        &lt;price&gt;65.95&lt;/price&gt;
    &lt;/minprice&gt;
    &lt;minprice title="TCP/IP Illustrated"&gt;
        &lt;price&gt;65.95&lt;/price&gt;
    &lt;/minprice&gt;
    &lt;minprice title="Data on the Web"&gt;
        &lt;price&gt;34.95&lt;/price&gt;
    &lt;/minprice&gt;
&lt;/results&gt; </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" xml:space="preserve">&lt;bib&gt;
{
        for $b in doc("http://bstore1.example.com/bib.xml")//book[author]
        return
            &lt;book&gt;
                { $b/title }
                { $b/author }
            &lt;/book&gt;
}
{
        for $b in doc("http://bstore1.example.com/bib.xml")//book[editor]
        return
          &lt;reference&gt;
            { $b/title }
            {$b/editor/affiliation}
          &lt;/reference&gt;
}
&lt;/bib&gt;  </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;bib&gt;
    &lt;book&gt;
        &lt;title&gt;TCP/IP Illustrated&lt;/title&gt;
        &lt;author&gt;
            &lt;last&gt;Stevens&lt;/last&gt;
            &lt;first&gt;W.&lt;/first&gt;
        &lt;/author&gt;
    &lt;/book&gt;
    &lt;book&gt;
        &lt;title&gt;Advanced Programming in the Unix environment&lt;/title&gt;
        &lt;author&gt;
            &lt;last&gt;Stevens&lt;/last&gt;
            &lt;first&gt;W.&lt;/first&gt;
        &lt;/author&gt;
    &lt;/book&gt;
    &lt;book&gt;
        &lt;title&gt;Data on the Web&lt;/title&gt;
        &lt;author&gt;
            &lt;last&gt;Abiteboul&lt;/last&gt;
            &lt;first&gt;Serge&lt;/first&gt;
        &lt;/author&gt;
        &lt;author&gt;
            &lt;last&gt;Buneman&lt;/last&gt;
            &lt;first&gt;Peter&lt;/first&gt;
        &lt;/author&gt;
        &lt;author&gt;
            &lt;last&gt;Suciu&lt;/last&gt;
            &lt;first&gt;Dan&lt;/first&gt;
        &lt;/author&gt;
    &lt;/book&gt;
    &lt;reference&gt;
        &lt;title&gt;The Economics of Technology and Content for Digital TV&lt;/title&gt;
        &lt;affiliation&gt;CITI&lt;/affiliation&gt;
    &lt;/reference&gt;
&lt;/bib&gt; </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" xml:space="preserve">&lt;bib&gt;
{
    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 &lt;&lt; $book2
    and not($book1/title = $book2/title)
    and deep-equal($aut1, $aut2) 
    return
        &lt;book-pair&gt;
            { $book1/title }
            { $book2/title }
        &lt;/book-pair&gt;
}
&lt;/bib&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;bib&gt;
    &lt;book-pair&gt;
        &lt;title&gt;TCP/IP Illustrated&lt;/title&gt;
        &lt;title&gt;Advanced Programming in the Unix environment&lt;/title&gt;
    &lt;/book-pair&gt;
&lt;/bib&gt; </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" xml:space="preserve">  &lt;!ELEMENT book (title, author+, section+)&gt;
  &lt;!ELEMENT title (#PCDATA)&gt;
  &lt;!ELEMENT author (#PCDATA)&gt;
  &lt;!ELEMENT section (title, (p | figure | section)* )&gt;
  &lt;!ATTLIST section
      id         ID    #IMPLIED
      difficulty CDATA #IMPLIED&gt;
  &lt;!ELEMENT p (#PCDATA)&gt;
  &lt;!ELEMENT figure (title, image)&gt;
  &lt;!ATTLIST figure
       width   CDATA   #REQUIRED
       height  CDATA   #REQUIRED &gt;
  &lt;!ELEMENT image EMPTY&gt;
  &lt;!ATTLIST image
       source  CDATA   #REQUIRED &gt;  </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" xml:space="preserve">&lt;?xml version="1.0"?&gt;
&lt;!DOCTYPE book SYSTEM "book.dtd"&gt;
&lt;book&gt;
  &lt;title&gt;Data on the Web&lt;/title&gt;
  &lt;author&gt;Serge Abiteboul&lt;/author&gt;
  &lt;author&gt;Peter Buneman&lt;/author&gt;
  &lt;author&gt;Dan Suciu&lt;/author&gt;
  &lt;section id="intro" difficulty="easy" &gt;
    &lt;title&gt;Introduction&lt;/title&gt;
    &lt;p&gt;Text ... &lt;/p&gt;
    &lt;section&gt;
      &lt;title&gt;Audience&lt;/title&gt;
      &lt;p&gt;Text ... &lt;/p&gt;
    &lt;/section&gt;
    &lt;section&gt;
      &lt;title&gt;Web Data and the Two Cultures&lt;/title&gt;
      &lt;p&gt;Text ... &lt;/p&gt;
      &lt;figure height="400" width="400"&gt;
        &lt;title&gt;Traditional client/server architecture&lt;/title&gt;
        &lt;image source="csarch.gif"/&gt;
      &lt;/figure&gt;
      &lt;p&gt;Text ... &lt;/p&gt;
    &lt;/section&gt;
  &lt;/section&gt;
  &lt;section id="syntax" difficulty="medium" &gt;
    &lt;title&gt;A Syntax For Data&lt;/title&gt;
    &lt;p&gt;Text ... &lt;/p&gt;
    &lt;figure height="200" width="500"&gt;
      &lt;title&gt;Graph representations of structures&lt;/title&gt;
      &lt;image source="graphs.gif"/&gt;
    &lt;/figure&gt;
    &lt;p&gt;Text ... &lt;/p&gt;
    &lt;section&gt;
      &lt;title&gt;Base Types&lt;/title&gt;
      &lt;p&gt;Text ... &lt;/p&gt;
    &lt;/section&gt;
    &lt;section&gt;
      &lt;title&gt;Representing Relational Databases&lt;/title&gt;
      &lt;p&gt;Text ... &lt;/p&gt;
      &lt;figure height="250" width="400"&gt;
        &lt;title&gt;Examples of Relations&lt;/title&gt;
        &lt;image source="relations.gif"/&gt;
      &lt;/figure&gt;
    &lt;/section&gt;
    &lt;section&gt;
      &lt;title&gt;Representing Object Databases&lt;/title&gt;
      &lt;p&gt;Text ... &lt;/p&gt;
    &lt;/section&gt;       
  &lt;/section&gt;
&lt;/book&gt; </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" xml:space="preserve">declare function local:toc($book-or-section as element()) as element()*
{
    for $section in $book-or-section/section
    return
      &lt;section&gt;
         { $section/@* , $section/title , local:toc($section) }			
      &lt;/section&gt;
};

&lt;toc&gt;
   {
     for $s in doc("book.xml")/book return local:toc($s)
   }
&lt;/toc&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;toc&gt;
    &lt;section id="intro" difficulty="easy"&gt;
        &lt;title&gt;Introduction&lt;/title&gt;
        &lt;section&gt;
            &lt;title&gt;Audience&lt;/title&gt;
        &lt;/section&gt;
        &lt;section&gt;
            &lt;title&gt;Web Data and the Two Cultures&lt;/title&gt;
        &lt;/section&gt;
    &lt;/section&gt;
    &lt;section id="syntax" difficulty="medium"&gt;
        &lt;title&gt;A Syntax For Data&lt;/title&gt;
        &lt;section&gt;
            &lt;title&gt;Base Types&lt;/title&gt;
        &lt;/section&gt;
        &lt;section&gt;
            &lt;title&gt;Representing Relational Databases&lt;/title&gt;
        &lt;/section&gt;
        &lt;section&gt;
            &lt;title&gt;Representing Object Databases&lt;/title&gt;
        &lt;/section&gt;
    &lt;/section&gt;
&lt;/toc&gt; </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" xml:space="preserve">&lt;figlist&gt;
  {
    for $f in doc("book.xml")//figure
    return
        &lt;figure&gt;
            { $f/@* }
            { $f/title }
        &lt;/figure&gt;
  }
&lt;/figlist&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;figlist&gt;
    &lt;figure  height="400" width="400"&gt;
        &lt;title&gt;Traditional client/server architecture&lt;/title&gt;
    &lt;/figure&gt;
    &lt;figure  height="200" width="500"&gt;
        &lt;title&gt;Graph representations of structures&lt;/title&gt;
    &lt;/figure&gt;
    &lt;figure  height="250" width ="400"&gt;
        &lt;title&gt;Examples of Relations&lt;/title&gt;
    &lt;/figure&gt;
&lt;/figlist&gt; </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" xml:space="preserve">&lt;section_count&gt;{ count(doc("book.xml")//section) }&lt;/section_count&gt;, 
&lt;figure_count&gt;{ count(doc("book.xml")//figure) }&lt;/figure_count&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;section_count&gt;7&lt;/section_count&gt;
&lt;figure_count&gt;3&lt;/figure_count&gt; </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" xml:space="preserve">&lt;top_section_count&gt;
 { 
   count(doc("book.xml")/book/section) 
 }
&lt;/top_section_count&gt;</eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;top_section_count&gt;2&lt;/top_section_count&gt; </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" xml:space="preserve">&lt;section_list&gt;
  {
    for $s in doc("book.xml")//section
    let $f := $s/figure
    return
        &lt;section title="{ $s/title/text() }" figcount="{ count($f) }"/&gt;
  }
&lt;/section_list&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;section_list&gt;
    &lt;section title="Introduction" figcount="0"/&gt;
    &lt;section title="Audience" figcount="0"/&gt;
    &lt;section title="Web Data and the Two Cultures" figcount="1"/&gt;
    &lt;section title="A Syntax For Data" figcount="1"/&gt;
    &lt;section title="Base Types" figcount="0"/&gt;
    &lt;section title="Representing Relational Databases" figcount="1"/&gt;
    &lt;section title="Representing Object Databases" figcount="0"/&gt;
&lt;/section_list&gt;</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" xml:space="preserve">declare function local:section-summary($book-or-section as element()*)
  as element()*
{
  for $section in $book-or-section
  return
    &lt;section&gt;
       { $section/@* }
       { $section/title }	
       &lt;figcount&gt;		
         { count($section/figure) }
       &lt;/figcount&gt;		
       { local:section-summary($section/section) }			
    &lt;/section&gt;
};

&lt;toc&gt;
  {
    for $s in doc("book.xml")/book/section
    return local:section-summary($s)
  }
&lt;/toc&gt; </eg>
                        <ednote>
                            <edtext>This solution was provided by Michael
                                Wenger, a student at the University of
                                Würzburg.</edtext>
                        </ednote>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;toc&gt;
    &lt;section id="intro" difficulty="easy"&gt;
        &lt;title&gt;Introduction&lt;/title&gt;
        &lt;figcount&gt;0&lt;/figcount&gt;
        &lt;section&gt;
            &lt;title&gt;Audience&lt;/title&gt;
            &lt;figcount&gt;0&lt;/figcount&gt;
        &lt;/section&gt;
        &lt;section&gt;
            &lt;title&gt;Web Data and the Two Cultures&lt;/title&gt;
            &lt;figcount&gt;1&lt;/figcount&gt;
        &lt;/section&gt;
    &lt;/section&gt;
    &lt;section id="syntax" difficulty="medium"&gt;
        &lt;title&gt;A Syntax For Data&lt;/title&gt;
        &lt;figcount&gt;1&lt;/figcount&gt;
        &lt;section&gt;
            &lt;title&gt;Base Types&lt;/title&gt;
            &lt;figcount&gt;0&lt;/figcount&gt;
        &lt;/section&gt;
        &lt;section&gt;
            &lt;title&gt;Representing Relational Databases&lt;/title&gt;
            &lt;figcount&gt;1&lt;/figcount&gt;
        &lt;/section&gt;
        &lt;section&gt;
            &lt;title&gt;Representing Object Databases&lt;/title&gt;
            &lt;figcount&gt;0&lt;/figcount&gt;
        &lt;/section&gt;
    &lt;/section&gt;
&lt;/toc&gt; </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" xml:space="preserve">
&lt;!DOCTYPE report [
  &lt;!ELEMENT report (section*)&gt;
  &lt;!ELEMENT section (section.title, section.content)&gt;
  &lt;!ELEMENT section.title  (#PCDATA )&gt;
  &lt;!ELEMENT section.content  (#PCDATA | anesthesia | prep 
                            | incision | action | observation )*&gt;
  &lt;!ELEMENT anesthesia (#PCDATA)&gt;
  &lt;!ELEMENT prep ( (#PCDATA | action)* )&gt;
  &lt;!ELEMENT incision ( (#PCDATA | geography | instrument)* )&gt;
  &lt;!ELEMENT action ( (#PCDATA | instrument )* )&gt;
  &lt;!ELEMENT observation (#PCDATA)&gt;
  &lt;!ELEMENT geography (#PCDATA)&gt;
  &lt;!ELEMENT instrument (#PCDATA)&gt;
]&gt; </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" xml:space="preserve">&lt;report&gt;
  &lt;section&gt;
    &lt;section.title&gt;Procedure&lt;/section.title&gt;
     &lt;section.content&gt;
      The patient was taken to the operating room where she was placed
      in supine position and
      &lt;anesthesia&gt;induced under general anesthesia.&lt;/anesthesia&gt;
      &lt;prep&gt; 
        &lt;action&gt;A Foley catheter was placed to decompress the bladder&lt;/action&gt;
        and the abdomen was then prepped and draped in sterile fashion.
      &lt;/prep&gt;  
      &lt;incision&gt;
        A curvilinear incision was made
        &lt;geography&gt;in the midline immediately infraumbilical&lt;/geography&gt;
        and the subcutaneous tissue was divided
        &lt;instrument&gt;using electrocautery.&lt;/instrument&gt;
      &lt;/incision&gt;
      The fascia was identified and
      &lt;action&gt;#2 0 Maxon stay sutures were placed on each side of the midline.
      &lt;/action&gt;
      &lt;incision&gt;
        The fascia was divided using
        &lt;instrument&gt;electrocautery&lt;/instrument&gt;
        and the peritoneum was entered.
      &lt;/incision&gt;
      &lt;observation&gt;The small bowel was identified.&lt;/observation&gt;
      and
      &lt;action&gt;
        the
        &lt;instrument&gt;Hasson trocar&lt;/instrument&gt;
        was placed under direct visualization.
      &lt;/action&gt;
      &lt;action&gt;
        The
        &lt;instrument&gt;trocar&lt;/instrument&gt;
        was secured to the fascia using the stay sutures.
      &lt;/action&gt;
     &lt;/section.content&gt;
  &lt;/section&gt;
&lt;/report&gt;</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" xml:space="preserve">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" xml:space="preserve">&lt;instrument&gt;electrocautery&lt;/instrument&gt;</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" xml:space="preserve">for $s in doc("report1.xml")//section[section.title = "Procedure"]
return ($s//instrument)[position()&lt;=2]</eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;instrument&gt;using electrocautery.&lt;/instrument&gt;
&lt;instrument&gt;electrocautery&lt;/instrument&gt; </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" xml:space="preserve">let $i2 := (doc("report1.xml")//incision)[2]
for $a in (doc("report1.xml")//action)[. &gt;&gt; $i2][position()&lt;=2]
return $a//instrument </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;instrument&gt;Hasson trocar&lt;/instrument&gt;
&lt;instrument&gt;trocar&lt;/instrument&gt; </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" xml:space="preserve">for $p in doc("report1.xml")//section[section.title = "Procedure"]
where not(some $a in $p//anesthesia satisfies
        $a &lt;&lt; ($p//incision)[1] )
return $p </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">

                        </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" xml:space="preserve">declare function local:precedes($a as node(), $b as node()) as xs:boolean 
{
    $a &lt;&lt; $b
      and
    empty($a//node() intersect $b)
};


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

&lt;critical_sequence&gt;
 {
  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
 }
&lt;/critical_sequence&gt; </eg>
                        <p>Here is another solution that is perhaps more
                            efficient and less readable:</p>
                        <eg role="xquery" xml:space="preserve">&lt;critical_sequence&gt;
 {
  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 &gt;&gt; $i1 and $n &lt;&lt; $i2
  return $n 
 }
&lt;/critical_sequence&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;critical_sequence&gt;
      The fascia was identified and
      &lt;action&gt;#2 0 Maxon stay sutures were placed on each side of the midline.
      &lt;/action&gt;#2 0 Maxon stay sutures were placed on each side of the midline.
&lt;/critical_sequence&gt; </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" xml:space="preserve">$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" xml:space="preserve">
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 &gt;&gt; $start and $n &lt;&lt; $end
    return $n
  return $nodes except $nodes//node()
};

&lt;critical_sequence&gt;
 {
  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)
 }
&lt;/critical_sequence&gt;
                        </eg>
                        <p>Here is the output from the above query:</p>
                        <eg role="result" xml:space="preserve">&lt;critical_sequence&gt;
      The fascia was identified and
      &lt;action&gt;#2 0 Maxon stay sutures were placed on each side of the midline.
      &lt;/action&gt;
&lt;/critical_sequence&gt; </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 xml:space="preserve"> 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 xml:space="preserve"> &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" xml:space="preserve">&lt;items&gt;
  &lt;item_tuple&gt;
    &lt;itemno&gt;1001&lt;/itemno&gt;
    &lt;description&gt;Red Bicycle&lt;/description&gt;
    &lt;offered_by&gt;U01&lt;/offered_by&gt;
    &lt;start_date&gt;1999-01-05&lt;/start_date&gt;
    &lt;end_date&gt;1999-01-20&lt;/end_date&gt;
    &lt;reserve_price&gt;40&lt;/reserve_price&gt;
  &lt;/item_tuple&gt;
  &lt;!-- !!! Snip !!! --&gt;

&lt;users&gt;
  &lt;user_tuple&gt;
    &lt;userid&gt;U01&lt;/userid&gt;
    &lt;name&gt;Tom Jones&lt;/name&gt;
    &lt;rating&gt;B&lt;/rating&gt;
  &lt;/user_tuple&gt;
  &lt;!-- !!! Snip !!! --&gt;

&lt;bids&gt;
  &lt;bid_tuple&gt; 
    &lt;userid&gt;U02&lt;/userid&gt; 
    &lt;itemno&gt;1001&lt;/itemno&gt; 
    &lt;bid&gt;35&lt;/bid&gt; 
    &lt;bid_date&gt;1999-01-07&lt;/bid_date&gt; 
    &lt;/bid_tuple&gt; 
  &lt;bid_tuple&gt; 
  &lt;!-- !!! Snip !!! --&gt;
                    </eg>
                    <p>The entire data set is represented by the following
                        table:</p>
                    <table border="1">
                        <caption>USERS</caption>
                        <thead>
                            <tr>
                                <td rowspan="1" colspan="1">USERID</td>
                                <td rowspan="1" colspan="1">NAME</td>
                                <td rowspan="1" colspan="1">RATING</td>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td rowspan="1" colspan="1">U01</td>
                                <td rowspan="1" colspan="1">Tom Jones</td>
                                <td rowspan="1" colspan="1">B</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U02</td>
                                <td rowspan="1" colspan="1">Mary Doe</td>
                                <td rowspan="1" colspan="1">A</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U03</td>
                                <td rowspan="1" colspan="1">Dee Linquent</td>
                                <td rowspan="1" colspan="1">D</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U04</td>
                                <td rowspan="1" colspan="1">Roger Smith</td>
                                <td rowspan="1" colspan="1">C</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U05</td>
                                <td rowspan="1" colspan="1">Jack Sprat</td>
                                <td rowspan="1" colspan="1">B</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U06</td>
                                <td rowspan="1" colspan="1">Rip Van Winkle</td>
                                <td rowspan="1" colspan="1">B</td>
                            </tr>
                        </tbody>
                    </table>
                    <table border="1">
                        <caption>ITEMS</caption>
                        <thead>
                            <tr>
                                <td rowspan="1" colspan="1">ITEMNO</td>
                                <td rowspan="1" colspan="1">DESCRIPTION</td>
                                <td rowspan="1" colspan="1">OFFERED_BY</td>
                                <td rowspan="1" colspan="1">START_DATE</td>
                                <td rowspan="1" colspan="1">END_DATE</td>
                                <td rowspan="1" colspan="1">RESERVE_PRICE</td>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td rowspan="1" colspan="1">1001</td>
                                <td rowspan="1" colspan="1">Red Bicycle</td>
                                <td rowspan="1" colspan="1">U01</td>
                                <td rowspan="1" colspan="1">1999-01-05</td>
                                <td rowspan="1" colspan="1">1999-01-20</td>
                                <td rowspan="1" colspan="1">40</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">1002</td>
                                <td rowspan="1" colspan="1">Motorcycle</td>
                                <td rowspan="1" colspan="1">U02</td>
                                <td rowspan="1" colspan="1">1999-02-11</td>
                                <td rowspan="1" colspan="1">1999-03-15</td>
                                <td rowspan="1" colspan="1">500</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">1003</td>
                                <td rowspan="1" colspan="1">Old Bicycle</td>
                                <td rowspan="1" colspan="1">U02</td>
                                <td rowspan="1" colspan="1">1999-01-10</td>
                                <td rowspan="1" colspan="1">1999-02-20</td>
                                <td rowspan="1" colspan="1">25</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">1004</td>
                                <td rowspan="1" colspan="1">Tricycle</td>
                                <td rowspan="1" colspan="1">U01</td>
                                <td rowspan="1" colspan="1">1999-02-25</td>
                                <td rowspan="1" colspan="1">1999-03-08</td>
                                <td rowspan="1" colspan="1">15</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">1005</td>
                                <td rowspan="1" colspan="1">Tennis Racket</td>
                                <td rowspan="1" colspan="1">U03</td>
                                <td rowspan="1" colspan="1">1999-03-19</td>
                                <td rowspan="1" colspan="1">1999-04-30</td>
                                <td rowspan="1" colspan="1">20</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">1006</td>
                                <td rowspan="1" colspan="1">Helicopter</td>
                                <td rowspan="1" colspan="1">U03</td>
                                <td rowspan="1" colspan="1">1999-05-05</td>
                                <td rowspan="1" colspan="1">1999-05-25</td>
                                <td rowspan="1" colspan="1">50000</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">1007</td>
                                <td rowspan="1" colspan="1">Racing Bicycle</td>
                                <td rowspan="1" colspan="1">U04</td>
                                <td rowspan="1" colspan="1">1999-01-20</td>
                                <td rowspan="1" colspan="1">1999-02-20</td>
                                <td rowspan="1" colspan="1">200</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">1008</td>
                                <td rowspan="1" colspan="1">Broken Bicycle</td>
                                <td rowspan="1" colspan="1">U01</td>
                                <td rowspan="1" colspan="1">1999-02-05</td>
                                <td rowspan="1" colspan="1">1999-03-06</td>
                                <td rowspan="1" colspan="1">25</td>
                            </tr>
                        </tbody>
                    </table>
                    <table border="1">
                        <caption>BIDS</caption>
                        <thead>
                            <tr>
                                <td rowspan="1" colspan="1">USERID</td>
                                <td rowspan="1" colspan="1">ITEMNO</td>
                                <td rowspan="1" colspan="1">BID</td>
                                <td rowspan="1" colspan="1">BID_DATE</td>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td rowspan="1" colspan="1">U02</td>
                                <td rowspan="1" colspan="1">1001</td>
                                <td rowspan="1" colspan="1">35</td>
                                <td rowspan="1" colspan="1">1999-01-07</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U04</td>
                                <td rowspan="1" colspan="1">1001</td>
                                <td rowspan="1" colspan="1">40</td>
                                <td rowspan="1" colspan="1">1999-01-08</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U02</td>
                                <td rowspan="1" colspan="1">1001</td>
                                <td rowspan="1" colspan="1">45</td>
                                <td rowspan="1" colspan="1">1999-01-11</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U04</td>
                                <td rowspan="1" colspan="1">1001</td>
                                <td rowspan="1" colspan="1">50</td>
                                <td rowspan="1" colspan="1">1999-01-13</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U02</td>
                                <td rowspan="1" colspan="1">1001</td>
                                <td rowspan="1" colspan="1">55</td>
                                <td rowspan="1" colspan="1">1999-01-15</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U01</td>
                                <td rowspan="1" colspan="1">1002</td>
                                <td rowspan="1" colspan="1">400</td>
                                <td rowspan="1" colspan="1">1999-02-14</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U02</td>
                                <td rowspan="1" colspan="1">1002</td>
                                <td rowspan="1" colspan="1">600</td>
                                <td rowspan="1" colspan="1">1999-02-16</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U03</td>
                                <td rowspan="1" colspan="1">1002</td>
                                <td rowspan="1" colspan="1">800</td>
                                <td rowspan="1" colspan="1">1999-02-17</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U04</td>
                                <td rowspan="1" colspan="1">1002</td>
                                <td rowspan="1" colspan="1">1000</td>
                                <td rowspan="1" colspan="1">1999-02-25</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U02</td>
                                <td rowspan="1" colspan="1">1002</td>
                                <td rowspan="1" colspan="1">1200</td>
                                <td rowspan="1" colspan="1">1999-03-02</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U04</td>
                                <td rowspan="1" colspan="1">1003</td>
                                <td rowspan="1" colspan="1">15</td>
                                <td rowspan="1" colspan="1">1999-01-22</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U05</td>
                                <td rowspan="1" colspan="1">1003</td>
                                <td rowspan="1" colspan="1">20</td>
                                <td rowspan="1" colspan="1">1999-02-03</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U01</td>
                                <td rowspan="1" colspan="1">1004</td>
                                <td rowspan="1" colspan="1">40</td>
                                <td rowspan="1" colspan="1">1999-03-05</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U03</td>
                                <td rowspan="1" colspan="1">1007</td>
                                <td rowspan="1" colspan="1">175</td>
                                <td rowspan="1" colspan="1">1999-01-25</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U05</td>
                                <td rowspan="1" colspan="1">1007</td>
                                <td rowspan="1" colspan="1">200</td>
                                <td rowspan="1" colspan="1">1999-02-08</td>
                            </tr>
                            <tr>
                                <td rowspan="1" colspan="1">U04</td>
                                <td rowspan="1" colspan="1">1007</td>
                                <td rowspan="1" colspan="1">225</td>
                                <td rowspan="1" colspan="1">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" xml:space="preserve">&lt;result&gt;
  {
    for $i in doc("items.xml")//item_tuple
    where $i/start_date &lt;= current-date()
      and $i/end_date &gt;= current-date() 
      and contains($i/description, "Bicycle")
    order by $i/itemno
    return
        &lt;item_tuple&gt;
            { $i/itemno }
            { $i/description }
        &lt;/item_tuple&gt;
  }
&lt;/result&gt;</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" xml:space="preserve">
&lt;result&gt;
    &lt;item_tuple&gt;
        &lt;itemno&gt;1003&lt;/itemno&gt;
        &lt;description&gt;Old Bicycle&lt;/description&gt;
    &lt;/item_tuple&gt;
    &lt;item_tuple&gt;
        &lt;itemno&gt;1007&lt;/itemno&gt;
        &lt;description&gt;Racing Bicycle&lt;/description&gt;
    &lt;/item_tuple&gt;
&lt;/result&gt;</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" xml:space="preserve">&lt;result&gt;
  {
    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
        &lt;item_tuple&gt;
            { $i/itemno }
            { $i/description }
            &lt;high_bid&gt;{ max($b/bid) }&lt;/high_bid&gt;
        &lt;/item_tuple&gt;
  }
&lt;/result&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;result&gt;
  &lt;item_tuple&gt;
    &lt;itemno&gt;1001&lt;/itemno&gt;
    &lt;description&gt;Red Bicycle&lt;/description&gt;
    &lt;high_bid&gt;55&lt;/high_bid&gt;
  &lt;/item_tuple&gt;
  &lt;item_tuple&gt;
    &lt;itemno&gt;1003&lt;/itemno&gt;
    &lt;description&gt;Old Bicycle&lt;/description&gt;
    &lt;high_bid&gt;20&lt;/high_bid&gt;
  &lt;/item_tuple&gt;
  &lt;item_tuple&gt;
    &lt;itemno&gt;1007&lt;/itemno&gt;
    &lt;description&gt;Racing Bicycle&lt;/description&gt;
    &lt;high_bid&gt;225&lt;/high_bid&gt;
  &lt;/item_tuple&gt;
  &lt;item_tuple&gt;
    &lt;itemno&gt;1008&lt;/itemno&gt;
    &lt;description&gt;Broken Bicycle&lt;/description&gt;
    &lt;high_bid&gt;&lt;/high_bid&gt;
  &lt;/item_tuple&gt;
&lt;/result&gt; </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" xml:space="preserve">&lt;result&gt;
  {
    for $u in doc("users.xml")//user_tuple
    for $i in doc("items.xml")//item_tuple
    where $u/rating &gt; "C" 
       and $i/reserve_price &gt; 1000 
       and $i/offered_by = $u/userid
    return
        &lt;warning&gt;
            { $u/name }
            { $u/rating }
            { $i/description }
            { $i/reserve_price }
        &lt;/warning&gt;
  }
&lt;/result&gt;</eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;result&gt;
    &lt;warning&gt;
        &lt;name&gt;Dee Linquent&lt;/name&gt;
        &lt;rating&gt;D&lt;/rating&gt;
        &lt;description&gt;Helicopter&lt;/description&gt;
        &lt;reserve_price&gt;50000&lt;/reserve_price&gt;
    &lt;/warning&gt;
&lt;/result&gt;</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" xml:space="preserve">&lt;result&gt;
  {
    for $i in doc("items.xml")//item_tuple
    where empty(doc("bids.xml")//bid_tuple[itemno = $i/itemno])
    return
        &lt;no_bid_item&gt;
            { $i/itemno }
            { $i/description }
        &lt;/no_bid_item&gt;
  }
&lt;/result&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">
&lt;result&gt;
    &lt;no_bid_item&gt;
        &lt;itemno&gt;1005&lt;/itemno&gt;
        &lt;description&gt;Tennis Racket&lt;/description&gt;
    &lt;/no_bid_item&gt;
    &lt;no_bid_item&gt;
        &lt;itemno&gt;1006&lt;/itemno&gt;
        &lt;description&gt;Helicopter&lt;/description&gt;
    &lt;/no_bid_item&gt;
    &lt;no_bid_item&gt;
        &lt;itemno&gt;1008&lt;/itemno&gt;
        &lt;description&gt;Broken Bicycle&lt;/description&gt;
    &lt;/no_bid_item&gt;
&lt;/result&gt;</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" xml:space="preserve">&lt;result&gt;
  {
    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
        &lt;jones_bike&gt;
            { $item/itemno }
            { $item/description }
            &lt;high_bid&gt;{ $highbid/bid }&lt;/high_bid&gt;
            &lt;high_bidder&gt;{ $buyer/name }&lt;/high_bidder&gt;
        &lt;/jones_bike&gt;
  }
&lt;/result&gt; </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" xml:space="preserve">&lt;result&gt;
  {
   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
        &lt;jones_bike&gt;
            { $item/itemno }
            { $item/description }
            &lt;high_bid&gt;{ $highbid/bid }&lt;/high_bid&gt;
            &lt;high_bidder&gt;{ $buyer/name }&lt;/high_bidder&gt;
        &lt;/jones_bike&gt;
    )
  }
&lt;/result&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">
&lt;result&gt;
    &lt;jones_bike&gt;
        &lt;itemno&gt;1001&lt;/itemno&gt;
        &lt;description&gt;Red Bicycle&lt;/description&gt;
        &lt;high_bid&gt;
            &lt;bid&gt;55&lt;/bid&gt;
        &lt;/high_bid&gt;
        &lt;high_bidder&gt;
            &lt;name&gt;Mary Doe&lt;/name&gt;
        &lt;/high_bidder&gt;
    &lt;/jones_bike&gt;
&lt;/result&gt;</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" xml:space="preserve">&lt;result&gt;
  {
    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 &lt; $z
    return
        &lt;successful_item&gt;
            { $item/itemno }
            { $item/description }
            { $item/reserve_price }
            &lt;high_bid&gt;{$z }&lt;/high_bid&gt;
         &lt;/successful_item&gt;
  }
&lt;/result&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">
&lt;result&gt;
    &lt;successful_item&gt;
        &lt;itemno&gt;1002&lt;/itemno&gt;
        &lt;description&gt;Motorcycle&lt;/description&gt;
        &lt;reserve_price&gt;500&lt;/reserve_price&gt;
        &lt;high_bid&gt;1200&lt;/high_bid&gt;
    &lt;/successful_item&gt;
    &lt;successful_item&gt;
        &lt;itemno&gt;1004&lt;/itemno&gt;
        &lt;description&gt;Tricycle&lt;/description&gt;
        &lt;reserve_price&gt;15&lt;/reserve_price&gt;
        &lt;high_bid&gt;40&lt;/high_bid&gt;
    &lt;/successful_item&gt;
&lt;/result&gt; </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" xml:space="preserve">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
    &lt;high_bid&gt;
      { 
        max($bikebids/bid) 
      }
    &lt;/high_bid&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;high_bid&gt;225&lt;/high_bid&gt;</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" xml:space="preserve">let $item := doc("items.xml")//item_tuple
  [end_date &gt;= xs:date("1999-03-01") and end_date &lt;= xs:date("1999-03-31")]
return
    &lt;item_count&gt;
      { 
        count($item) 
      }
    &lt;/item_count&gt;</eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;item_count&gt;3&lt;/item_count&gt;</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" xml:space="preserve">&lt;result&gt;
  {
    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
        &lt;monthly_result&gt;
            &lt;month&gt;{ $m }&lt;/month&gt;
            &lt;item_count&gt;{ count($item) }&lt;/item_count&gt;
        &lt;/monthly_result&gt;
  }
&lt;/result&gt;</eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;result&gt;
    &lt;monthly_result&gt;
        &lt;month&gt;1&lt;/month&gt;
        &lt;item_count&gt;1&lt;/item_count&gt;
    &lt;/monthly_result&gt;
    &lt;monthly_result&gt;
        &lt;month&gt;2&lt;/month&gt;
        &lt;item_count&gt;2&lt;/item_count&gt;
    &lt;/monthly_result&gt;
    &lt;monthly_result&gt;
        &lt;month&gt;3&lt;/month&gt;
        &lt;item_count&gt;3&lt;/item_count&gt;
    &lt;/monthly_result&gt;
    &lt;monthly_result&gt;
        &lt;month&gt;4&lt;/month&gt;
        &lt;item_count&gt;1&lt;/item_count&gt;
    &lt;/monthly_result&gt;
    &lt;monthly_result&gt;
        &lt;month&gt;5&lt;/month&gt;
        &lt;item_count&gt;1&lt;/item_count&gt;
    &lt;/monthly_result&gt;
&lt;/result&gt; </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" xml:space="preserve">&lt;result&gt;
 {
    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
        &lt;high_bid&gt;
            { $highbid/itemno }
            { $highbid/bid }
            &lt;bidder&gt;{ $user/name/text() }&lt;/bidder&gt;
        &lt;/high_bid&gt;
  }
&lt;/result&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">
&lt;result&gt;
    &lt;high_bid&gt;
        &lt;itemno&gt;1001&lt;/itemno&gt;
        &lt;bid&gt;55&lt;/bid&gt;
        &lt;bidder&gt;Mary Doe&lt;/bidder&gt;
    &lt;/high_bid&gt;
    &lt;high_bid&gt;
        &lt;itemno&gt;1002&lt;/itemno&gt;
        &lt;bid&gt;1200&lt;/bid&gt;
        &lt;bidder&gt;Mary Doe&lt;/bidder&gt;
    &lt;/high_bid&gt;
    &lt;high_bid&gt;
        &lt;itemno&gt;1003&lt;/itemno&gt;
        &lt;bid&gt;20&lt;/bid&gt;
        &lt;bidder&gt;Jack Sprat&lt;/bidder&gt;
    &lt;/high_bid&gt;
    &lt;high_bid&gt;
        &lt;itemno&gt;1004&lt;/itemno&gt;
        &lt;bid&gt;40&lt;/bid&gt;
        &lt;bidder&gt;Tom Jones&lt;/bidder&gt;
    &lt;/high_bid&gt;
    &lt;high_bid&gt;
        &lt;itemno&gt;1007&lt;/itemno&gt;
        &lt;bid&gt;225&lt;/bid&gt;
        &lt;bidder&gt;Roger Smith&lt;/bidder&gt;
    &lt;/high_bid&gt;
&lt;/result&gt; </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" xml:space="preserve">let $highbid := max(doc("bids.xml")//bid_tuple/bid)
return
    &lt;result&gt;
     {
        for $item in doc("items.xml")//item_tuple,
            $b in doc("bids.xml")//bid_tuple[itemno = $item/itemno]
        where $b/bid = $highbid
        return
            &lt;expensive_item&gt;
                { $item/itemno }
                { $item/description }
                &lt;high_bid&gt;{ $highbid }&lt;/high_bid&gt;
            &lt;/expensive_item&gt;
     }
    &lt;/result&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">
&lt;result&gt;
    &lt;expensive_item&gt;
        &lt;itemno&gt;1002&lt;/itemno&gt;
        &lt;description&gt;Motorcycle&lt;/description&gt;
        &lt;high_bid&gt;1200&lt;/high_bid&gt;
    &lt;/expensive_item&gt;
&lt;/result&gt; </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" xml:space="preserve">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
        &lt;bid_count&gt;
            &lt;itemno&gt;{ $i }&lt;/itemno&gt;
            &lt;nbids&gt;{ count($b) }&lt;/nbids&gt;
        &lt;/bid_count&gt;
};

&lt;result&gt;
 {
    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
        &lt;popular_item&gt;
            { $item/itemno }
            { $item/description }
            &lt;bid_count&gt;{ $bc/nbids/text() }&lt;/bid_count&gt;
        &lt;/popular_item&gt;
 }
&lt;/result&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;result&gt;
    &lt;popular_item&gt;
        &lt;itemno&gt;1001&lt;/itemno&gt;
        &lt;description&gt;Red Bicycle&lt;/description&gt;
        &lt;bid_count&gt;5&lt;/bid_count&gt;
    &lt;/popular_item&gt;
    &lt;popular_item&gt;
        &lt;itemno&gt;1002&lt;/itemno&gt;
        &lt;description&gt;Motorcycle&lt;/description&gt;
        &lt;bid_count&gt;5&lt;/bid_count&gt;
    &lt;/popular_item&gt;
&lt;/result&gt; </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" xml:space="preserve">&lt;result&gt;
 {
    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
        &lt;bidder&gt;
            { $u/userid }
            { $u/name }
            &lt;bidcount&gt;{ count($b) }&lt;/bidcount&gt;
            &lt;avgbid&gt;{ avg($b/bid) }&lt;/avgbid&gt;
        &lt;/bidder&gt;
  }
&lt;/result&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;result&gt;
    &lt;bidder&gt;
        &lt;userid&gt;U01&lt;/userid&gt;
        &lt;name&gt;Tom Jones&lt;/name&gt;
        &lt;bidcount&gt;2&lt;/bidcount&gt;
        &lt;avgbid&gt;220&lt;/avgbid&gt;
    &lt;/bidder&gt;
    &lt;bidder&gt;
        &lt;userid&gt;U02&lt;/userid&gt;
        &lt;name&gt;Mary Doe&lt;/name&gt;
        &lt;bidcount&gt;5&lt;/bidcount&gt;
        &lt;avgbid&gt;387&lt;/avgbid&gt;
    &lt;/bidder&gt;
    &lt;bidder&gt;
        &lt;userid&gt;U03&lt;/userid&gt;
        &lt;name&gt;Dee Linquent&lt;/name&gt;
        &lt;bidcount&gt;2&lt;/bidcount&gt;
        &lt;avgbid&gt;487.5&lt;/avgbid&gt;
    &lt;/bidder&gt;
    &lt;bidder&gt;
        &lt;userid&gt;U04&lt;/userid&gt;
        &lt;name&gt;Roger Smith&lt;/name&gt;
        &lt;bidcount&gt;5&lt;/bidcount&gt;
        &lt;avgbid&gt;266&lt;/avgbid&gt;
    &lt;/bidder&gt;
    &lt;bidder&gt;
        &lt;userid&gt;U05&lt;/userid&gt;
        &lt;name&gt;Jack Sprat&lt;/name&gt;
        &lt;bidcount&gt;2&lt;/bidcount&gt;
        &lt;avgbid&gt;110&lt;/avgbid&gt;
    &lt;/bidder&gt;
&lt;/result&gt; </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" xml:space="preserve">&lt;result&gt;
 {
    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) &gt;= 3
    order by $avgbid descending
    return
        &lt;popular_item&gt;
            &lt;itemno&gt;{ $i }&lt;/itemno&gt;
            &lt;avgbid&gt;{ $avgbid }&lt;/avgbid&gt;
        &lt;/popular_item&gt;
  }
&lt;/result&gt; </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">
&lt;result&gt;
    &lt;popular_item&gt;
        &lt;itemno&gt;1002&lt;/itemno&gt;
        &lt;avgbid&gt;800&lt;/avgbid&gt;
    &lt;/popular_item&gt;
    &lt;popular_item&gt;
        &lt;itemno&gt;1007&lt;/itemno&gt;
        &lt;avgbid&gt;200&lt;/avgbid&gt;
    &lt;/popular_item&gt;
    &lt;popular_item&gt;
        &lt;itemno&gt;1001&lt;/itemno&gt;
        &lt;avgbid&gt;45&lt;/avgbid&gt;
    &lt;/popular_item&gt;
&lt;/result&gt;</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" xml:space="preserve">&lt;result&gt;
  {
    for $u in doc("users.xml")//user_tuple
    let $b := doc("bids.xml")//bid_tuple[userid=$u/userid and bid&gt;=100]
    where count($b) &gt; 1
    return
        &lt;big_spender&gt;{ $u/name/text() }&lt;/big_spender&gt;
  }
&lt;/result&gt;
                        </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">
&lt;result&gt;
    &lt;big_spender&gt;Mary Doe&lt;/big_spender&gt;
    &lt;big_spender&gt;Dee Linquent&lt;/big_spender&gt;
    &lt;big_spender&gt;Roger Smith&lt;/big_spender&gt;
&lt;/result&gt;</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" xml:space="preserve">&lt;result&gt;
  {
    for $u in doc("users.xml")//user_tuple
    let $b := doc("bids.xml")//bid_tuple[userid = $u/userid]
    order by $u/userid
    return
        &lt;user&gt;
            { $u/userid }
            { $u/name }
            {
                if (empty($b))
                  then &lt;status&gt;inactive&lt;/status&gt;
                  else &lt;status&gt;active&lt;/status&gt;
            }
        &lt;/user&gt;
  }
&lt;/result&gt;
                        </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">
&lt;result&gt;
    &lt;user&gt;
        &lt;userid&gt;U01&lt;/userid&gt;
        &lt;name&gt;Tom Jones&lt;/name&gt;
        &lt;status&gt;active&lt;/status&gt;
    &lt;/user&gt;
    &lt;user&gt;
        &lt;userid&gt;U02&lt;/userid&gt;
        &lt;name&gt;Mary Doe&lt;/name&gt;
        &lt;status&gt;active&lt;/status&gt;
    &lt;/user&gt;
    &lt;user&gt;
        &lt;userid&gt;U03&lt;/userid&gt;
        &lt;name&gt;Dee Linquent&lt;/name&gt;
        &lt;status&gt;active&lt;/status&gt;
    &lt;/user&gt;
    &lt;user&gt;
        &lt;userid&gt;U04&lt;/userid&gt;
        &lt;name&gt;Roger Smith&lt;/name&gt;
        &lt;status&gt;active&lt;/status&gt;
    &lt;/user&gt;
    &lt;user&gt;
        &lt;userid&gt;U05&lt;/userid&gt;
        &lt;name&gt;Jack Sprat&lt;/name&gt;
        &lt;status&gt;active&lt;/status&gt;
    &lt;/user&gt;
    &lt;user&gt;
        &lt;userid&gt;U06&lt;/userid&gt;
        &lt;name&gt;Rip Van Winkle&lt;/name&gt;
        &lt;status&gt;inactive&lt;/status&gt;
    &lt;/user&gt;
&lt;/result&gt;</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" xml:space="preserve">&lt;frequent_bidder&gt;
  {
    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
  }
&lt;/frequent_bidder&gt;</eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">&lt;frequent_bidder /&gt; </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" xml:space="preserve">&lt;result&gt;
  {
    for $u in doc("users.xml")//user_tuple
    order by $u/name
    return
        &lt;user&gt;
            { $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
                    &lt;bid_on_item&gt;{ $descr }&lt;/bid_on_item&gt;
            }
        &lt;/user&gt;
  }
&lt;/result&gt;
                        </eg>
                        <p>
                            <emph>Expected Result:</emph>
                        </p>
                        <eg role="result" xml:space="preserve">
&lt;result&gt;
    &lt;user&gt;
        &lt;name&gt;Dee Linquent&lt;/name&gt;
        &lt;bid_on_item&gt;Motorcycle&lt;/bid_on_item&gt;
        &lt;bid_on_item&gt;Racing Bicycle&lt;/bid_on_item&gt;
    &lt;/user&gt;
    &lt;user&gt;
        &lt;name&gt;Jack Sprat&lt;/name&gt;
        &lt;bid_on_item&gt;Old Bicycle&lt;/bid_on_item&gt;
        &lt;bid_on_item&gt;Racing Bicycle&lt;/bid_on_item&gt;
    &lt;/user&gt;
    &lt;user&gt;
        &lt;name&gt;Mary Doe&lt;/name&gt;
        &lt;bid_on_item&gt;Motorcycle&lt;/bid_on_item&gt;
        &lt;bid_on_item&gt;Red Bicycle&lt;/bid_on_item&gt;
    &lt;/user&gt;
    &lt;user&gt;
        &lt;name&gt;Rip Van Winkle&lt;/name&gt;
    &lt;/user&gt;
    &lt;user&gt;
        &lt;name&gt;Roger Smith&lt;/name&gt;
        &lt;bid_on_item&gt;Motorcycle&lt;/bid_on_item&gt;
        &lt;bid_on_item&gt;Old Bicycle&lt;/bid_on_item&gt;
        &lt;bid_on_item&gt;Racing Bicycle&lt;/bid_on_item&gt;
        &lt;bid_on_item&gt;Red Bicycle&lt;/bid_on_item&gt;
    &lt;/user&gt;
    &lt;user&gt;
        &lt;name&gt;Tom Jones&lt;/name&gt;
        &lt;bid_on_item&gt;Motorcycle&lt;/bid_on_item&gt;
        &lt;bid_on_item&gt;Tricycle&lt;/bid_on_item&gt;
    &lt;/user&gt;
&lt;/result&gt;</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" xml:space="preserve"> &lt;!NOTATION cgm PUBLIC "Computer Graphics Metafile"&gt;
 &lt;!NOTATION ccitt PUBLIC "CCITT group 4 raster"&gt;

 &lt;!ENTITY % text "(#PCDATA | emph)*"&gt;
 &lt;!ENTITY infoflow SYSTEM "infoflow.ccitt" NDATA ccitt&gt;
 &lt;!ENTITY tagexamp SYSTEM "tagexamp.cgm"   NDATA cgm&gt;

 &lt;!ELEMENT report (title, chapter+)&gt;
 &lt;!ELEMENT title %text;&gt;
 &lt;!ELEMENT chapter (title, intro?, section*)&gt;
 &lt;!ATTLIST chapter
               shorttitle CDATA #IMPLIED&gt;
 &lt;!ELEMENT intro (para | graphic)+&gt;
 &lt;!ELEMENT section (title, intro?, topic*)&gt;
 &lt;!ATTLIST section
               shorttitle CDATA #IMPLIED
               sectid ID #IMPLIED&gt;
 &lt;!ELEMENT topic (title, (para | graphic)+)&gt;
 &lt;!ATTLIST topic
               shorttitle CDATA #IMPLIED
               topicid ID #IMPLIED&gt;
 &lt;!ELEMENT para (#PCDATA | emph | xref)*&gt;
 &lt;!ATTLIST para
               security (u | c | s | ts) "u"&gt;
 &lt;!ELEMENT emph %text;&gt;
 &lt;!ELEMENT graphic EMPTY&gt;
 &lt;!ATTLIST graphic
               graphname ENTITY #REQUIRED&gt;
 &lt;!ELEMENT xref EMPTY&gt;
 &lt;!ATTLIST xref
               xrefid IDREF #IMPLIED&gt;
                    </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" xml:space="preserve">
 0: &lt;!DOCTYPE report SYSTEM "report.dtd"&gt;
 1: &lt;report&gt;
 2: &lt;title&gt;Getting started with SGML&lt;/title&gt;
 3: &lt;chapter&gt;
 4: &lt;title&gt;The business challenge&lt;/title&gt;
 5: &lt;intro&gt;
 6: &lt;para&gt;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.&lt;/para&gt;
11: &lt;para&gt;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.&lt;/para&gt;
16: &lt;graphic graphname="infoflow"/&gt;&lt;/intro&gt;&lt;/chapter&gt;
17: &lt;chapter&gt;
18: &lt;title&gt;Getting to know SGML&lt;/title&gt;
19: &lt;intro&gt;
20: &lt;para&gt;While SGML is a fairly recent technology, the use of
21: &lt;emph&gt;markup&lt;/emph&gt; in computer-generated documents has existed for a
22: while.&lt;/para&gt;&lt;/intro&gt;
23: &lt;section shorttitle="What is markup?"&gt;
24: &lt;title&gt;What is markup, or everything you always wanted to know about
25: document preparation but were afraid to ask?&lt;/title&gt;
26: &lt;intro&gt;
27: &lt;para&gt;Markup is everything in a document that is not content. The
28: traditional meaning of markup is the manual &lt;emph&gt;marking&lt;/emph&gt; 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 &lt;emph&gt;procedural markup&lt;/emph&gt;.&lt;/para&gt;&lt;/intro&gt;
32: &lt;topic topicid="top1"&gt;
33: &lt;title&gt;Procedural markup&lt;/title&gt;
34: &lt;para&gt;Most electronic publishing systems today use some form of
35: procedural markup. Procedural markup codes are good for one
36: presentation of the information.&lt;/para&gt;&lt;/topic&gt;
37: &lt;topic topicid="top2"&gt;
38: &lt;title&gt;Generic markup&lt;/title&gt;
39: &lt;para&gt;Generic markup (also known as descriptive markup) describes the
40: &lt;emph&gt;purpose&lt;/emph&gt; 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.&lt;/para&gt;&lt;/topic&gt;
44: &lt;topic topicid="top3"&gt;
45: &lt;title&gt;Drawbacks of procedural markup&lt;/title&gt;
46: &lt;para&gt;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.&lt;/para&gt;&lt;/topic&gt;&lt;/section&gt;
50: &lt;section shorttitle="What is SGML?"&gt;
51: &lt;title&gt;What &lt;emph&gt;is&lt;/emph&gt; SGML in the grand scheme of the universe, anyway?&lt;/title&gt;
52: &lt;intro&gt;
53: &lt;para&gt;SGML defines a strict markup scheme with a syntax for defining
54: document data elements and an overall framework for marking up
55: documents.&lt;/para&gt;
56: &lt;para&gt;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.&lt;/para&gt;&lt;/intro&gt;&lt;/section&gt;
59: &lt;section shorttitle="How does SGML work?"&gt;
60: &lt;title&gt;How is SGML and would you recommend it to your grandmother?&lt;/title&gt;
61: &lt;intro&gt;
62: &lt;para&gt;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.&lt;/para&gt;&lt;/intro&gt;
65: &lt;topic topicid="top4"&gt;
66: &lt;title&gt;Structure&lt;/title&gt;
67: &lt;para&gt;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.&lt;/para&gt;
71: &lt;para&gt;A database schema also defines the relationships between the
72: various types of data. Similarly, a DTD specifies &lt;emph&gt;rules&lt;/emph&gt;
73: to help ensure documents have a consistent, logical structure.&lt;/para&gt;&lt;/topic&gt;
74: &lt;topic topicid="top5"&gt;
75: &lt;title&gt;Content&lt;/title&gt;
76: &lt;para&gt;Content is the information itself. The method for identifying
77: the information and its meaning within this framework is called
78: &lt;emph&gt;tagging&lt;/emph&gt;. Tagging must
79: conform to the rules established in the DTD (see &lt;xref xrefid="top4"/&gt;).&lt;/para&gt;
80: &lt;graphic graphname="tagexamp"/&gt;&lt;/topic&gt;
81: &lt;topic topicid="top6"&gt;
82: &lt;title&gt;Style&lt;/title&gt;
83: &lt;para&gt;SGML does not standardize style or other processing methods for
84: information stored in SGML.&lt;/para&gt;&lt;/topic&gt;&lt;/section&gt;&lt;/chapter&gt;
85: &lt;chapter&gt;
86: &lt;title&gt;Resources&lt;/title&gt;
87: &lt;section&gt;
88: &lt;title&gt;Conferences, tutorials, and training&lt;/title&gt;
89: &lt;intro&gt;
90: &lt;para&gt;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.&lt;/para&gt;
94: &lt;para security="c"&gt;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.&lt;/para&gt;
97: &lt;/intro&gt;
98: &lt;/section&gt;
99: &lt;/chapter&gt;
100:&lt;/report&gt;
                    </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" xml:space="preserve">&lt;result&gt;
  { 
    doc("sgml.xml")//report//para 
  }
&lt;/result&gt;</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" xml:space="preserve">&lt;result&gt;
  { 
    doc("sgml.xml")//intro/para 
  }
&lt;/result&gt;</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 