Another abandoned server code base... this is kind of an ancestor of taskrambler.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

7361 lines
239 KiB

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="EN" xml:lang="EN">
<head>
<meta name="generator" content=
"HTML Tidy for Mac OS X (vers 1st December 2004), see www.w3.org" />
<title>XML Query Use Cases</title>
<style type="text/css">
/*<![CDATA[*/
code { font-family: monospace; }
div.constraint,
div.issue,
div.note,
div.notice { margin-left: 2em; }
div.issue
p.title { margin-left: -2em; }
ol.enumar { list-style-type: decimal; }
ol.enumla { list-style-type: lower-alpha; }
ol.enumlr { list-style-type: lower-roman; }
ol.enumua { list-style-type: upper-alpha; }
ol.enumur { list-style-type: upper-roman; }
li p { margin-top: 0.3em;
margin-bottom: 0.3em; }
sup small { font-style: italic;
color: #8F8F8F;
}
div.exampleInner pre { margin-left: 1em;
margin-top: 0em; margin-bottom: 0em}
div.exampleOuter {border: 4px double gray;
margin: 0em; padding: 0em}
div.exampleInner { background-color: #d5dee3;
border-top-width: 4px;
border-top-style: double;
border-top-color: #d3d3d3;
border-bottom-width: 4px;
border-bottom-style: double;
border-bottom-color: #d3d3d3;
padding: 4px; margin: 0em }
div.exampleWrapper { margin: 4px }
div.exampleHeader { font-weight: bold;
margin: 4px}
div.issue { border-bottom-color: black;
border-bottom-style: solid;
border-bottom-width: 1pt;
margin-bottom: 20pt;
}
th.issue-toc-head { border-bottom-color: black;
border-bottom-style: solid;
border-bottom-width: 1pt;
}
table.small { font-size: x-small; }
/*]]>*/
</style>
<link type="text/css" rel="stylesheet" href=
"http://www.w3.org/StyleSheets/TR/W3C-WG-NOTE" />
</head>
<body>
<div class="head">
<p><a href="http://www.w3.org/"><img width="72" height="48"
alt="W3C" src="http://www.w3.org/Icons/w3c_home" /></a></p>
<h1><a id="title" name="title"></a>XML Query Use Cases</h1>
<h2><a id="w3c-doctype" name="w3c-doctype"></a>W3C Working
Group Note 23 March 2007</h2>
<dl>
<dt>This version:</dt>
<dd><a href=
"http://www.w3.org/TR/2007/NOTE-xquery-use-cases-20070323/">http://www.w3.org/TR/2007/NOTE-xquery-use-cases-20070323/</a></dd>
<dt>Latest version:</dt>
<dd><a href=
"http://www.w3.org/TR/xquery-use-cases/">http://www.w3.org/TR/xquery-use-cases/</a></dd>
<dt>Previous version:</dt>
<dd><a href=
"http://www.w3.org/TR/2006/WD-xquery-use-cases-20060608/">http://www.w3.org/TR/2006/WD-xquery-use-cases-20060608/</a></dd>
<dt>Editors:</dt>
<dd>Don Chamberlin, IBM Almaden Research Center <a href=
"mailto:chamberlin@almaden.ibm.com">&lt;chamberlin@almaden.ibm.com
&gt;</a></dd>
<dd>Peter Fankhauser, Infonyte GmbH <a href=
"mailto:fankhauser@infonyte.com">&lt;fankhauser@infonyte.com&gt;</a></dd>
<dd>Daniela Florescu, Oracle corporation <a href=
"mailto:dana.florescu@oracle.com">&lt;dana.florescu@oracle.com&gt;</a></dd>
<dd>Massimo Marchiori, University of Venice <a href=
"mailto:massimo@w3.org">&lt;massimo@w3.org&gt;</a></dd>
<dd>Jonathan Robie, DataDirect Technologies <a href=
"mailto:jonathan.robie@datadirect.com">&lt;jonathan.robie@datadirect.com&gt;</a></dd>
</dl>
<p>This document is also available in these non-normative
formats: <a href=
"http://www.w3.org/TR/2007/NOTE-xquery-use-cases-20070323/xquery-use-cases.xml">
XML</a>.</p>
<p class="copyright"><a href=
"http://www.w3.org/Consortium/Legal/ipr-notice#Copyright">Copyright</a>&#160;©&#160;2007&#160;<a href="http://www.w3.org/">
<acronym title=
"World Wide Web Consortium">W3C</acronym></a><sup>®</sup>
(<a href="http://www.csail.mit.edu/"><acronym title=
"Massachusetts Institute of Technology">MIT</acronym></a>,
<a href="http://www.ercim.org/"><acronym title=
"European Research Consortium for Informatics and Mathematics">ERCIM</acronym></a>,
<a href="http://www.keio.ac.jp/">Keio</a>), All Rights
Reserved. W3C <a href=
"http://www.w3.org/Consortium/Legal/ipr-notice#Legal_Disclaimer">
liability</a>, <a href=
"http://www.w3.org/Consortium/Legal/ipr-notice#W3C_Trademarks">trademark</a>
and <a href=
"http://www.w3.org/Consortium/Legal/copyright-documents">document
use</a> rules apply.</p>
</div>
<hr />
<div>
<h2><a id="abstract" name="abstract"></a>Abstract</h2>
<p>This document specifies usage scenarios for XQuery.</p>
</div>
<div>
<h2><a id="status" name="status"></a>Status of this
Document</h2>
<p><em>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 <a href=
"http://www.w3.org/TR/">W3C technical reports index</a> at
http://www.w3.org/TR/.</em></p>
<p>This is the W3C <a href=
"http://www.w3.org/2005/10/Process-20051014/tr.html#tr-end">Working
Group Note</a> of "XML Query (XQuery) Use Cases", produced by
the W3C <a href="http://www.w3.org/XML/Query/">XML Query
Working Group</a>, part of the <a href=
"http://www.w3.org/XML/Activity">XML Activity</a>. This
document is being published as a <a href=
"http://www.w3.org/2005/10/Process-20051014/tr.html#tr-end">Working
Group Note</a> to persistently record the Use Cases that guided
the development of <a href=
"http://www.w3.org/TR/xquery/">XQuery 1.0: An XML Query
Language</a> and its associated specifications as W3C
Recommendations.</p>
<p>Please submit comments about this document using W3C's
<a href="http://www.w3.org/Bugs/Public/">public Bugzilla
system</a> (instructions can be found at <a href=
"http://www.w3.org/XML/2005/04/qt-bugzilla">http://www.w3.org/XML/2005/04/qt-bugzilla</a>).
If access to that system is not feasible, you may send your
comments to the W3C XSLT/XPath/XQuery public comments mailing
list, <a href=
"mailto:public-qt-comments@w3.org">public-qt-comments@w3.org</a>.
It will be very helpful if you include the string
[XQRecUseCases] in the subject line of your report, whether
made in Bugzilla or in email. Each Bugzilla entry and email
message should contain only one comment. Archives of the
comments and responses are available at <a href=
"http://lists.w3.org/Archives/Public/public-qt-comments/">http://lists.w3.org/Archives/Public/public-qt-comments/</a>.</p>
<!--*
<p>Publication as a <a href=
"http://www.w3.org/2005/10/Process-20051014/tr.html#tr-end">Working
Group Note</a> 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>Publication as a <a
href="http://www.w3.org/2005/10/Process-20051014/tr.html#tr-end">Working
Group Note</a> does not imply endorsement by the W3C Membership. At
the time of publication, work on this document was considered complete
and no further revisions are anticipated. It is a stable document and
may be used as reference material or cited from another
document. However, this document may be updated, replaced, or made
obsolete by other documents at any time.</p>
<p>This document was produced by a group operating under the
<a href=
"http://www.w3.org/Consortium/Patent-Policy-20040205/">5
February 2004 W3C Patent Policy</a>. W3C maintains a <a href=
"http://www.w3.org/2004/01/pp-impl/18797/status#disclosures">public
list of any patent disclosures</a> 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 <a href=
"http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential">
Essential Claim(s)</a> must disclose the information in
accordance with <a href=
"http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure">
section 6 of the W3C Patent Policy</a>.</p>
</div>
<div class="toc">
<h2><a id="contents" name="contents"></a>Table of Contents</h2>
<p class="toc">1 <a href="#ucforxq">Use Cases for XML
Queries</a><br />
&#160;&#160;&#160;&#160;1.1 <a href="#xmp">Use Case "XMP":
Experiences and Exemplars</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.1 <a href=
"#xmp-dtd">Document Type Definitions (DTD)</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.2 <a href=
"#xmp-data">Sample Data</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.3 <a href=
"#xmp-dtd-q5">DTD for Q5</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.4 <a href=
"#xmp-data-q5">Sample Data for Q5</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.5 <a href=
"#xmp-dtd-q9">DTD for Q9</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.6 <a href=
"#xmp-data-q9">Data for Q9</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.7 <a href=
"#xmp-dtd-q10">DTD for Q10</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.8 <a href=
"#xmp-data-q10">Data for Q10</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.9 <a href=
"#xmp-queries-results">Queries and Results</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.9.1
<a href="#xmp-queries-results-q1">Q1</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.9.2
<a href="#xmp-queries-results-q2">Q2</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.9.3
<a href="#xmp-queries-results-q3">Q3</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.9.4
<a href="#xmp-queries-results-q4">Q4</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.9.5
<a href="#xmp-queries-results-q5">Q5</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.9.6
<a href="#xmp-queries-results-q6">Q6</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.9.7
<a href="#xmp-queries-results-q7">Q7</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.9.8
<a href="#xmp-queries-results-q8">Q8</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.9.9
<a href="#xmp-queries-results-q9">Q9</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.9.10
<a href="#xmp-queries-results-q10">Q10</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.9.11
<a href="#xmp-queries-results-q11">Q11</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.1.9.12
<a href="#xmp-queries-results-q12">Q12</a><br />
&#160;&#160;&#160;&#160;1.2 <a href="#tree">Use Case "TREE":
Queries that preserve hierarchy</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.2.1 <a href=
"#tree-description">Description</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.2.2 <a href=
"#tree-dtd">Document Type Definition (DTD)</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.2.3 <a href=
"#tree-data">Sample Data</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.2.4 <a href=
"#tree-queries-results">Queries and Results</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.2.4.1
<a href="#tree-queries-results-q1">Q1</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.2.4.2
<a href="#tree-queries-results-q2">Q2</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.2.4.3
<a href="#tree-queries-results-q3">Q3</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.2.4.4
<a href="#tree-queries-results-q4">Q4</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.2.4.5
<a href="#tree-queries-results-q5">Q5</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.2.4.6
<a href="#tree-queries-results-q6">Q6</a><br />
&#160;&#160;&#160;&#160;1.3 <a href="#seq">Use Case "SEQ" -
Queries based on Sequence</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.3.1 <a href=
"#seq-description">Description</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.3.2 <a href=
"#seq-dtd">Document Type Definition (DTD)</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.3.3 <a href=
"#seq-data">Sample Data</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.3.4 <a href=
"#seq-queries-results">Queries and Results</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.3.4.1
<a href="#seq-queries-results-q1">Q1</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.3.4.2
<a href="#seq-queries-results-q2">Q2</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.3.4.3
<a href="#seq-queries-results-q3">Q3</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.3.4.4
<a href="#seq-queries-results-q4">Q4</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.3.4.5
<a href="#seq-queries-results-q5">Q5</a><br />
&#160;&#160;&#160;&#160;1.4 <a href="#rdb">Use Case "R" -
Access to Relational Data</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.1 <a href=
"#rdb-description">Description</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.2 <a href=
"#rdb-dtd">Document Type Definition (DTD)</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.3 <a href=
"#rdb-data">Sample Data</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.4 <a href=
"#rdb-queries-results">Queries and Results</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.4.1
<a href="#rdb-queries-results-q1">Q1</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.4.2
<a href="#rdb-queries-results-q2">Q2</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.4.3
<a href="#rdb-queries-results-q3">Q3</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.4.4
<a href="#rdb-queries-results-q4">Q4</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.4.5
<a href="#rdb-queries-results-q5">Q5</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.4.6
<a href="#rdb-queries-results-q6">Q6</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.4.7
<a href="#rdb-queries-results-q7">Q7</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.4.8
<a href="#rdb-queries-results-q8">Q8</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.4.9
<a href="#rdb-queries-results-q9">Q9</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.4.10
<a href="#rdb-queries-results-q10">Q10</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.4.11
<a href="#rdb-queries-results-q11">Q11</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.4.12
<a href="#rdb-queries-results-q12">Q12</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.4.13
<a href="#rdb-queries-results-q13">Q13</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.4.14
<a href="#rdb-queries-results-q14">Q14</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.4.15
<a href="#rdb-queries-results-q15">Q15</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.4.16
<a href="#rdb-queries-results-q16">Q16</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.4.17
<a href="#rdb-queries-results-q17">Q17</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.4.4.18
<a href="#rdb-queries-results-q18">Q18</a><br />
&#160;&#160;&#160;&#160;1.5 <a href="#sgml">Use Case "SGML":
Standard Generalized Markup Language</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.5.1 <a href=
"#sgml-description">Description</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.5.2 <a href=
"#sgml-dtd">Document Type Definition (DTD)</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.5.3 <a href=
"#sgml-data">Sample Data</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.5.4 <a href=
"#sgml-queries-results">Queries and Results</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.5.4.1
<a href="#sgml-queries-results-q1">Q1</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.5.4.2
<a href="#sgml-queries-results-q2">Q2</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.5.4.3
<a href="#sgml-queries-results-q3">Q3</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.5.4.4
<a href="#sgml-queries-results-q4">Q4</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.5.4.5
<a href="#sgml-queries-results-q5">Q5</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.5.4.6
<a href="#sgml-queries-results-q6">Q6</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.5.4.7
<a href="#sgml-queries-results-q7">Q7</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.5.4.8
<a href="#sgml-queries-results-q8a">Q8a</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.5.4.9
<a href="#sgml-queries-results-q8b">Q8b</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.5.4.10
<a href="#sgml-queries-results-q9">Q9</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.5.4.11
<a href="#sgml-queries-results-q10">Q10</a><br />
&#160;&#160;&#160;&#160;1.6 <a href="#text">Use Case "STRING":
String Search</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.6.1 <a href=
"#text-description">Description</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.6.2 <a href=
"#text-dtd">Document Type Definition (DTD)</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.6.3 <a href=
"#text-data">Sample Data</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.6.4 <a href=
"#text-queries-results">Queries and Results</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.6.4.1
<a href="#text-queries-results-q1">Q1</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.6.4.2
<a href="#text-queries-results-q2">Q2</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.6.4.3
<a href="#text-queries-results-q3">Q3</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.6.4.4
<a href="#text-queries-results-q4">Q4</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.6.4.5
<a href="#text-queries-results-q5">Q5</a><br />
&#160;&#160;&#160;&#160;1.7 <a href="#ns">Use Case "NS" -
Queries Using Namespaces</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.7.1 <a href=
"#ns-description">Description</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.7.2 <a href=
"#ns-dtd">Document Type Definition (DTD)</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.7.3 <a href=
"#ns-data">Sample Data</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.7.4 <a href=
"#ns-queries-results">Queries and Results</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.7.4.1
<a href="#ns-queries-results-q1">Q1</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.7.4.2
<a href="#ns-queries-results-q2">Q2</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.7.4.3
<a href="#ns-queries-results-q3">Q3</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.7.4.4
<a href="#ns-queries-results-q4">Q4</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.7.4.5
<a href="#ns-queries-results-q5">Q5</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.7.4.6
<a href="#ns-queries-results-q6">Q6</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.7.4.7
<a href="#ns-queries-results-q7">Q7</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.7.4.8
<a href="#ns-queries-results-q8">Q8</a><br />
&#160;&#160;&#160;&#160;1.8 <a href="#parts">Use Case "PARTS" -
Recursive Parts Explosion</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.8.1 <a href=
"#parts-description">Description</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.8.2 <a href=
"#parts-dtd">Document Type Definitions (DTD)</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.8.3 <a href=
"#parts-data">Sample Data</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.8.4 <a href=
"#parts-queries-results">Queries and Results</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.8.4.1
<a href="#parts-queries-results-q1">Q1</a><br />
&#160;&#160;&#160;&#160;1.9 <a href="#strong">Use Case "STRONG"
- queries that exploit strongly typed data</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.9.1 <a href=
"#strong-description">Description</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.9.2 <a href=
"#strong-schema">Schema</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.9.3 <a href=
"#strong-data">Sample Data</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.9.4 <a href=
"#strong-queries-results">Queries</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.9.4.1
<a href="#strong-queries-results-q1">Q1</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.9.4.2
<a href="#strong-queries-results-q2">Q2</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.9.4.3
<a href="#strong-queries-results-q3">Q3</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.9.4.4
<a href="#strong-queries-results-q4">Q4</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.9.4.5
<a href="#strong-queries-results-q5">Q5</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.9.4.6
<a href="#strong-queries-results-q6">Q6</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.9.4.7
<a href="#strong-queries-results-q7">Q7</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.9.4.8
<a href="#strong-queries-results-q8">Q8</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.9.4.9
<a href="#strong-queries-results-q9">Q9</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.9.4.10
<a href="#strong-queries-results-q10">Q10</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.9.4.11
<a href="#strong-queries-results-q11">Q11</a><br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1.9.4.12
<a href="#strong-queries-results-q12">Q12</a><br /></p>
<h3><a id="appendices" name="appendices"></a>Appendices</h3>
<p class="toc">A <a href=
"#acknowledgements">Acknowledgements</a><br />
B <a href="#ChangeLog">Change Log</a> (Non-Normative)<br />
&#160;&#160;&#160;&#160;B.1 <a href="#id-2006-05-08">8 May
2006</a><br />
&#160;&#160;&#160;&#160;B.2 <a href="#id-2005-08-31">31 Aug
2005</a><br />
&#160;&#160;&#160;&#160;B.3 <a href="#id-2005-07-11">11 July
2005</a><br />
&#160;&#160;&#160;&#160;B.4 <a href="#id-2005-04-04">04 April
2005</a><br />
&#160;&#160;&#160;&#160;B.5 <a href="#id-2005-01-30">30 Jan
2005</a><br />
C <a href="#references">References</a>
(Non-Normative)<br /></p>
</div>
<hr />
<div class="body">
<div class="div1">
<h2><a id="ucforxq" name="ucforxq"></a>1 Use Cases for XML
Queries</h2>
<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 <a href=
"#xquery-requirements">[XQuery Requirements]</a> document,
without solutions in concrete query languages. Now it is
being republished with solutions for <a href=
"#xquery">[XQuery]</a>. These use cases are also being used
by the <a href="http://www.w3.org/XML/Group/xquery-test/">W3C
XML Query Testing Task Force</a>.</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>
<div class="exampleInner">
<pre>
doc("http://bstore1.example.com/bib.xml")//author
</pre>
</div>
<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>
<div class="exampleInner">
<pre>
$b//author
</pre>
</div>
<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>
<div class="exampleInner">
<pre>
//author
</pre>
</div>
<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 <a href="#UseCaseQueries">[Use Case Sample
Queries]</a>. Also, the queries from the XQuery specification
itself have been made available in <a href=
"#XQueryQueries">[XQuery Sample Queries]</a>.</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>
<div class="div2">
<h3><a id="xmp" name="xmp"></a>1.1 Use Case "XMP":
Experiences and Exemplars</h3>
<p>This use case contains several example queries that
illustrate requirements gathered from the database and
document communities.</p>
<div class="div3">
<h4><a id="xmp-dtd" name="xmp-dtd"></a>1.1.1 Document
Type Definitions (DTD)</h4>
<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>
<div class="schema">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="xmp-data" name="xmp-data"></a>1.1.2 Sample
Data</h4>
<p>Here is the data found at
"bstore1.example.com/bib.xml":</p>
<div class="data">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="xmp-dtd-q5" name="xmp-dtd-q5"></a>1.1.3 DTD
for Q5</h4>
<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>
<div class="schema">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="xmp-data-q5" name="xmp-data-q5"></a>1.1.4
Sample Data for Q5</h4>
<p>Here are the contents of
"http://bstore2.example.com/reviews.xml":</p>
<div class="data">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="xmp-dtd-q9" name="xmp-dtd-q9"></a>1.1.5 DTD
for Q9</h4>
<p>Q9 uses an input document named "books.xml", with the
following DTD:</p>
<div class="schema">
<div class="exampleInner">
<pre>
&lt;!ELEMENT chapter (title, section*)&gt;
&lt;!ELEMENT section (title, section*)&gt;
&lt;!ELEMENT title (#PCDATA)&gt;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="xmp-data-q9" name="xmp-data-q9"></a>1.1.6 Data
for Q9</h4>
<p>Here are the contents of books.xml:</p>
<div class="data">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="xmp-dtd-q10" name="xmp-dtd-q10"></a>1.1.7 DTD
for Q10</h4>
<p>Q10 uses an input document named "prices.xml", with
the following DTD:</p>
<div class="schema">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="xmp-data-q10" name="xmp-data-q10"></a>1.1.8
Data for Q10</h4>
<p>Here are the contents of prices.xml:</p>
<div class="data">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="xmp-queries-results" name=
"xmp-queries-results"></a>1.1.9 Queries and Results</h4>
<div class="div4">
<h5><a id="xmp-queries-results-q1" name=
"xmp-queries-results-q1"></a>1.1.9.1 Q1</h5>
<p>List books published by Addison-Wesley after 1991,
including their year and title.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="xmp-queries-results-q2" name=
"xmp-queries-results-q2"></a>1.1.9.2 Q2</h5>
<p>Create a flat list of all the title-author pairs,
with each pair enclosed in a "result" element.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="xmp-queries-results-q3" name=
"xmp-queries-results-q3"></a>1.1.9.3 Q3</h5>
<p>For each book in the bibliography, list the title
and authors, grouped inside a "result" element.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="xmp-queries-results-q4" name=
"xmp-queries-results-q4"></a>1.1.9.4 Q4</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<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><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="xmp-queries-results-q5" name=
"xmp-queries-results-q5"></a>1.1.9.5 Q5</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="xmp-queries-results-q6" name=
"xmp-queries-results-q6"></a>1.1.9.6 Q6</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="xmp-queries-results-q7" name=
"xmp-queries-results-q7"></a>1.1.9.7 Q7</h5>
<p>List the titles and years of all books published by
Addison-Wesley after 1991, in alphabetic order.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="xmp-queries-results-q8" name=
"xmp-queries-results-q8"></a>1.1.9.8 Q8</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
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;
</pre>
</div>
</div>
<p>In the above solution, string(), local-name() and
ends-with() are functions defined in the Functions and
Operators document.</p>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="xmp-queries-results-q9" name=
"xmp-queries-results-q9"></a>1.1.9.9 Q9</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&lt;results&gt;
{
for $t in doc("books.xml")//(chapter | section)/title
where contains($t/text(), "XML")
return $t
}
&lt;/results&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;results&gt;
&lt;title&gt;XML&lt;/title&gt;
&lt;title&gt;XML and Semistructured Data&lt;/title&gt;
&lt;/results&gt;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="xmp-queries-results-q10" name=
"xmp-queries-results-q10"></a>1.1.9.10 Q10</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="xmp-queries-results-q11" name=
"xmp-queries-results-q11"></a>1.1.9.11 Q11</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="xmp-queries-results-q12" name=
"xmp-queries-results-q12"></a>1.1.9.12 Q12</h5>
<p>Find pairs of books that have different titles but
the same set of authors (possibly in a different
order).</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<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>
</div>
</div>
</div>
<div class="div2">
<h3><a id="tree" name="tree"></a>1.2 Use Case "TREE":
Queries that preserve hierarchy</h3>
<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>
<div class="div3">
<h4><a id="tree-description" name=
"tree-description"></a>1.2.1 Description</h4>
<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>
</div>
<div class="div3">
<h4><a id="tree-dtd" name="tree-dtd"></a>1.2.2 Document
Type Definition (DTD)</h4>
<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>
<div class="schema">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="tree-data" name="tree-data"></a>1.2.3 Sample
Data</h4>
<p>The queries in this use case are based on the
following sample data.</p>
<div class="data">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="tree-queries-results" name=
"tree-queries-results"></a>1.2.4 Queries and Results</h4>
<div class="div4">
<h5><a id="tree-queries-results-q1" name=
"tree-queries-results-q1"></a>1.2.4.1 Q1</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="tree-queries-results-q2" name=
"tree-queries-results-q2"></a>1.2.4.2 Q2</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&lt;figlist&gt;
{
for $f in doc("book.xml")//figure
return
&lt;figure&gt;
{ $f/@* }
{ $f/title }
&lt;/figure&gt;
}
&lt;/figlist&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="tree-queries-results-q3" name=
"tree-queries-results-q3"></a>1.2.4.3 Q3</h5>
<p>How many sections are in Book1, and how many
figures?</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;section_count&gt;7&lt;/section_count&gt;
&lt;figure_count&gt;3&lt;/figure_count&gt;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="tree-queries-results-q4" name=
"tree-queries-results-q4"></a>1.2.4.4 Q4</h5>
<p>How many top-level sections are in Book1?</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&lt;top_section_count&gt;
{
count(doc("book.xml")/book/section)
}
&lt;/top_section_count&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;top_section_count&gt;2&lt;/top_section_count&gt;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="tree-queries-results-q5" name=
"tree-queries-results-q5"></a>1.2.4.5 Q5</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="tree-queries-results-q6" name=
"tree-queries-results-q6"></a>1.2.4.6 Q6</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
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;
</pre>
</div>
</div>
<table border="1" summary="Editorial note">
<tr>
<td width="50%" valign="top" align="left">
<b>Editorial note</b></td>
<td width="50%" valign="top" align="right">
&#160;</td>
</tr>
<tr>
<td valign="top" align="left" colspan="2">This
solution was provided by Michael Wenger, a student
at the University of Würzburg.</td>
</tr>
</table>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="div2">
<h3><a id="seq" name="seq"></a>1.3 Use Case "SEQ" - Queries
based on Sequence</h3>
<p>This use case illustrates queries based on the sequence
in which elements appear in a document.</p>
<div class="div3">
<h4><a id="seq-description" name=
"seq-description"></a>1.3.1 Description</h4>
<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>
</div>
<div class="div3">
<h4><a id="seq-dtd" name="seq-dtd"></a>1.3.2 Document
Type Definition (DTD)</h4>
<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>
<div class="schema">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="seq-data" name="seq-data"></a>1.3.3 Sample
Data</h4>
<p>The queries in this use case are based on the
following sample data.</p>
<div class="data">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="seq-queries-results" name=
"seq-queries-results"></a>1.3.4 Queries and Results</h4>
<div class="div4">
<h5><a id="seq-queries-results-q1" name=
"seq-queries-results-q1"></a>1.3.4.1 Q1</h5>
<p>In the Procedure section of Report1, what
Instruments were used in the second Incision?</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
for $s in doc("report1.xml")//section[section.title = "Procedure"]
return ($s//incision)[2]/instrument
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;instrument&gt;electrocautery&lt;/instrument&gt;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="seq-queries-results-q2" name=
"seq-queries-results-q2"></a>1.3.4.2 Q2</h5>
<p>In the Procedure section of Report1, what are the
first two Instruments to be used?</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
for $s in doc("report1.xml")//section[section.title = "Procedure"]
return ($s//instrument)[position()&lt;=2]
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;instrument&gt;using electrocautery.&lt;/instrument&gt;
&lt;instrument&gt;electrocautery&lt;/instrument&gt;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="seq-queries-results-q3" name=
"seq-queries-results-q3"></a>1.3.4.3 Q3</h5>
<p>In Report1, what Instruments were used in the first
two Actions after the second Incision?</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
let $i2 := (doc("report1.xml")//incision)[2]
for $a in (doc("report1.xml")//action)[. &gt;&gt; $i2][position()&lt;=2]
return $a//instrument
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;instrument&gt;Hasson trocar&lt;/instrument&gt;
&lt;instrument&gt;trocar&lt;/instrument&gt;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="seq-queries-results-q4" name=
"seq-queries-results-q4"></a>1.3.4.4 Q4</h5>
<p>In Report1, find "Procedure" sections where no
Anesthesia element occurs before the first Incision</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
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
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
</pre>
</div>
</div>
<p>(No sections satisfy Q4, thankfully.)</p>
</div>
<div class="div4">
<h5><a id="seq-queries-results-q5" name=
"seq-queries-results-q5"></a>1.3.4.5 Q5</h5>
<p>In Report1, what happened between the first Incision
and the second Incision?</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
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;
</pre>
</div>
</div>
<p>Here is another solution that is perhaps more
efficient and less readable:</p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<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>
<div class="xquery">
<div class="exampleInner">
<pre>
$c except $c//node()
</pre>
</div>
</div>
<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>
<div class="xquery">
<div class="exampleInner">
<pre>
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;
</pre>
</div>
</div>
<p>Here is the output from the above query:</p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="div2">
<h3><a id="rdb" name="rdb"></a>1.4 Use Case "R" - Access to
Relational Data</h3>
<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>
<div class="div3">
<h4><a id="rdb-description" name=
"rdb-description"></a>1.4.1 Description</h4>
<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>
<div class="exampleInner">
<pre>
USERS ( USERID, NAME, RATING ) ITEMS ( ITEMNO,
DESCRIPTION, OFFERED_BY, START_DATE, END_DATE,
RESERVE_PRICE ) BIDS ( USERID, ITEMNO, BID, BID_DATE
)
</pre>
</div>
</div>
<div class="div3">
<h4><a id="rdb-dtd" name="rdb-dtd"></a>1.4.2 Document
Type Definition (DTD)</h4>
<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>
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<div class="div3">
<h4><a id="rdb-data" name="rdb-data"></a>1.4.3 Sample
Data</h4>
<p>Here is an abbreviated set of data showing the XML
format of the instances:</p>
<div class="data">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p>The entire data set is represented by the following
table:</p>
<table border="1">
<caption>
USERS
</caption>
<thead>
<tr>
<td>USERID</td>
<td>NAME</td>
<td>RATING</td>
</tr>
</thead>
<tbody>
<tr>
<td>U01</td>
<td>Tom Jones</td>
<td>B</td>
</tr>
<tr>
<td>U02</td>
<td>Mary Doe</td>
<td>A</td>
</tr>
<tr>
<td>U03</td>
<td>Dee Linquent</td>
<td>D</td>
</tr>
<tr>
<td>U04</td>
<td>Roger Smith</td>
<td>C</td>
</tr>
<tr>
<td>U05</td>
<td>Jack Sprat</td>
<td>B</td>
</tr>
<tr>
<td>U06</td>
<td>Rip Van Winkle</td>
<td>B</td>
</tr>
</tbody>
</table>
<table border="1">
<caption>
ITEMS
</caption>
<thead>
<tr>
<td>ITEMNO</td>
<td>DESCRIPTION</td>
<td>OFFERED_BY</td>
<td>START_DATE</td>
<td>END_DATE</td>
<td>RESERVE_PRICE</td>
</tr>
</thead>
<tbody>
<tr>
<td>1001</td>
<td>Red Bicycle</td>
<td>U01</td>
<td>1999-01-05</td>
<td>1999-01-20</td>
<td>40</td>
</tr>
<tr>
<td>1002</td>
<td>Motorcycle</td>
<td>U02</td>
<td>1999-02-11</td>
<td>1999-03-15</td>
<td>500</td>
</tr>
<tr>
<td>1003</td>
<td>Old Bicycle</td>
<td>U02</td>
<td>1999-01-10</td>
<td>1999-02-20</td>
<td>25</td>
</tr>
<tr>
<td>1004</td>
<td>Tricycle</td>
<td>U01</td>
<td>1999-02-25</td>
<td>1999-03-08</td>
<td>15</td>
</tr>
<tr>
<td>1005</td>
<td>Tennis Racket</td>
<td>U03</td>
<td>1999-03-19</td>
<td>1999-04-30</td>
<td>20</td>
</tr>
<tr>
<td>1006</td>
<td>Helicopter</td>
<td>U03</td>
<td>1999-05-05</td>
<td>1999-05-25</td>
<td>50000</td>
</tr>
<tr>
<td>1007</td>
<td>Racing Bicycle</td>
<td>U04</td>
<td>1999-01-20</td>
<td>1999-02-20</td>
<td>200</td>
</tr>
<tr>
<td>1008</td>
<td>Broken Bicycle</td>
<td>U01</td>
<td>1999-02-05</td>
<td>1999-03-06</td>
<td>25</td>
</tr>
</tbody>
</table>
<table border="1">
<caption>
BIDS
</caption>
<thead>
<tr>
<td>USERID</td>
<td>ITEMNO</td>
<td>BID</td>
<td>BID_DATE</td>
</tr>
</thead>
<tbody>
<tr>
<td>U02</td>
<td>1001</td>
<td>35</td>
<td>1999-01-07</td>
</tr>
<tr>
<td>U04</td>
<td>1001</td>
<td>40</td>
<td>1999-01-08</td>
</tr>
<tr>
<td>U02</td>
<td>1001</td>
<td>45</td>
<td>1999-01-11</td>
</tr>
<tr>
<td>U04</td>
<td>1001</td>
<td>50</td>
<td>1999-01-13</td>
</tr>
<tr>
<td>U02</td>
<td>1001</td>
<td>55</td>
<td>1999-01-15</td>
</tr>
<tr>
<td>U01</td>
<td>1002</td>
<td>400</td>
<td>1999-02-14</td>
</tr>
<tr>
<td>U02</td>
<td>1002</td>
<td>600</td>
<td>1999-02-16</td>
</tr>
<tr>
<td>U03</td>
<td>1002</td>
<td>800</td>
<td>1999-02-17</td>
</tr>
<tr>
<td>U04</td>
<td>1002</td>
<td>1000</td>
<td>1999-02-25</td>
</tr>
<tr>
<td>U02</td>
<td>1002</td>
<td>1200</td>
<td>1999-03-02</td>
</tr>
<tr>
<td>U04</td>
<td>1003</td>
<td>15</td>
<td>1999-01-22</td>
</tr>
<tr>
<td>U05</td>
<td>1003</td>
<td>20</td>
<td>1999-02-03</td>
</tr>
<tr>
<td>U01</td>
<td>1004</td>
<td>40</td>
<td>1999-03-05</td>
</tr>
<tr>
<td>U03</td>
<td>1007</td>
<td>175</td>
<td>1999-01-25</td>
</tr>
<tr>
<td>U05</td>
<td>1007</td>
<td>200</td>
<td>1999-02-08</td>
</tr>
<tr>
<td>U04</td>
<td>1007</td>
<td>225</td>
<td>1999-02-12</td>
</tr>
</tbody>
</table>
</div>
<div class="div3">
<h4><a id="rdb-queries-results" name=
"rdb-queries-results"></a>1.4.4 Queries and Results</h4>
<div class="div4">
<h5><a id="rdb-queries-results-q1" name=
"rdb-queries-results-q1"></a>1.4.4.1 Q1</h5>
<p>List the item number and description of all bicycles
that currently have an auction in progress, ordered by
item number.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<div class="note">
<p class="prefix"><b>Note:</b></p>
<p>This solution assumes that the current date is
1999-01-31.</p>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<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>
</div>
<div class="div4">
<h5><a id="rdb-queries-results-q2" name=
"rdb-queries-results-q2"></a>1.4.4.2 Q2</h5>
<p>For all bicycles, list the item number, description,
and highest bid (if any), ordered by item number.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="rdb-queries-results-q3" name=
"rdb-queries-results-q3"></a>1.4.4.3 Q3</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="rdb-queries-results-q4" name=
"rdb-queries-results-q4"></a>1.4.4.4 Q4</h5>
<p>List item numbers and descriptions of items that
have no bids.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="rdb-queries-results-q5" name=
"rdb-queries-results-q5"></a>1.4.4.5 Q5</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<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><em>Unordered Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="rdb-queries-results-q6" name=
"rdb-queries-results-q6"></a>1.4.4.6 Q6</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="rdb-queries-results-q7" name=
"rdb-queries-results-q7"></a>1.4.4.7 Q7</h5>
<p>Find the highest bid ever made for a bicycle or
tricycle.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;high_bid&gt;225&lt;/high_bid&gt;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="rdb-queries-results-q8" name=
"rdb-queries-results-q8"></a>1.4.4.8 Q8</h5>
<p>How many items were actioned (auction ended) in
March 1999?</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;item_count&gt;3&lt;/item_count&gt;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="rdb-queries-results-q9" name=
"rdb-queries-results-q9"></a>1.4.4.9 Q9</h5>
<p>List the number of items auctioned each month in
1999 for which data is available, ordered by month.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="rdb-queries-results-q10" name=
"rdb-queries-results-q10"></a>1.4.4.10 Q10</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="rdb-queries-results-q11" name=
"rdb-queries-results-q11"></a>1.4.4.11 Q11</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="rdb-queries-results-q12" name=
"rdb-queries-results-q12"></a>1.4.4.12 Q12</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="rdb-queries-results-q13" name=
"rdb-queries-results-q13"></a>1.4.4.13 Q13</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="rdb-queries-results-q14" name=
"rdb-queries-results-q14"></a>1.4.4.14 Q14</h5>
<p>List item numbers and average bids for items that
have received three or more bids, in descending order
by average bid.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="rdb-queries-results-q15" name=
"rdb-queries-results-q15"></a>1.4.4.15 Q15</h5>
<p>List names of users who have placed multiple bids of
at least $100 each.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="rdb-queries-results-q16" name=
"rdb-queries-results-q16"></a>1.4.4.16 Q16</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="rdb-queries-results-q17" name=
"rdb-queries-results-q17"></a>1.4.4.17 Q17</h5>
<p>List the names of users, if any, who have bid on
every item.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;frequent_bidder /&gt;
</pre>
</div>
</div>
<p>(No users satisfy Q17.)</p>
</div>
<div class="div4">
<h5><a id="rdb-queries-results-q18" name=
"rdb-queries-results-q18"></a>1.4.4.18 Q18</h5>
<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><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="div2">
<h3><a id="sgml" name="sgml"></a>1.5 Use Case "SGML":
Standard Generalized Markup Language</h3>
<div class="div3">
<h4><a id="sgml-description" name=
"sgml-description"></a>1.5.1 Description</h4>
<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>
</div>
<div class="div3">
<h4><a id="sgml-dtd" name="sgml-dtd"></a>1.5.2 Document
Type Definition (DTD)</h4>
<p>This use case is based on data conforming to the DTD
shown below.</p>
<div class="schema">
<div class="exampleInner">
<pre>
&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;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="sgml-data" name="sgml-data"></a>1.5.3 Sample
Data</h4>
<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>
<div class="data">
<div class="exampleInner">
<pre>
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;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="sgml-queries-results" name=
"sgml-queries-results"></a>1.5.4 Queries and Results</h4>
<div class="div4">
<h5><a id="sgml-queries-results-q1" name=
"sgml-queries-results-q1"></a>1.5.4.1 Q1</h5>
<p>Locate all paragraphs in the report (all "para"
elements occurring anywhere within the "report"
element).</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&lt;result&gt;
{
doc("sgml.xml")//report//para
}
&lt;/result&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></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>
</div>
<div class="div4">
<h5><a id="sgml-queries-results-q2" name=
"sgml-queries-results-q2"></a>1.5.4.2 Q2</h5>
<p>Locate all paragraph elements in an introduction
(all "para" elements directly contained within an
"intro" element).</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&lt;result&gt;
{
doc("sgml.xml")//intro/para
}
&lt;/result&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<p>Elements whose start-tags are on lines 6, 11, 20,
27, 53, 56, 62, 90, 94</p>
</div>
<div class="div4">
<h5><a id="sgml-queries-results-q3" name=
"sgml-queries-results-q3"></a>1.5.4.3 Q3</h5>
<p>Locate all paragraphs in the introduction of a
section that is in a chapter that has no introduction
(all "para" elements directly contained within an
"intro" element directly contained in a "section"
element directly contained in a "chapter" element. The
"chapter" element must not directly contain an "intro"
element).</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&lt;result&gt;
{
for $c in doc("sgml.xml")//chapter
where empty($c/intro)
return $c/section/intro/para
}
&lt;/result&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<p>Elements whose start-tags are on lines 90, 94</p>
</div>
<div class="div4">
<h5><a id="sgml-queries-results-q4" name=
"sgml-queries-results-q4"></a>1.5.4.4 Q4</h5>
<p>Locate the second paragraph in the third section in
the second chapter (the second "para" element occurring
in the third "section" element occurring in the second
"chapter" element occurring in the "report").</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&lt;result&gt;
{
(((doc("sgml.xml")//chapter)[2]//section)[3]//para)[2]
}
&lt;/result&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<p>Element whose start-tag is on line 67</p>
</div>
<div class="div4">
<h5><a id="sgml-queries-results-q5" name=
"sgml-queries-results-q5"></a>1.5.4.5 Q5</h5>
<p>Locate all classified paragraphs (all "para"
elements whose "security" attribute has the value
"c").</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&lt;result&gt;
{
doc("sgml.xml")//para[@security = "c"]
}
&lt;/result&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<p>Element whose start-tag is on line 94</p>
</div>
<div class="div4">
<h5><a id="sgml-queries-results-q6" name=
"sgml-queries-results-q6"></a>1.5.4.6 Q6</h5>
<p>List the short titles of all sections (the values of
the "shorttitle" attributes of all "section" elements,
expressing each short title as the value of a new
element.)</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&lt;result&gt;
{
for $s in doc("sgml.xml")//section/@shorttitle
return &lt;stitle&gt;{ $s }&lt;/stitle&gt;
}
&lt;/result&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<p>Attribute values in start-tags on lines 23, 50,
59</p>
</div>
<div class="div4">
<h5><a id="sgml-queries-results-q7" name=
"sgml-queries-results-q7"></a>1.5.4.7 Q7</h5>
<p>Locate the initial letter of the initial paragraph
of all introductions (the first character in the
content [character content as well as element content]
of the first "para" element contained in an "intro"
element).</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&lt;result&gt;
{
for $i in doc("sgml.xml")//intro/para[1]
return
&lt;first_letter&gt;{ substring(string($i), 1, 1) }&lt;/first_letter&gt;
}
&lt;/result&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<p>Character after start-tag on lines 6, 20, 27, 53,
62, 90</p>
</div>
<div class="div4">
<h5><a id="sgml-queries-results-q8a" name=
"sgml-queries-results-q8a"></a>1.5.4.8 Q8a</h5>
<p>Locate all sections with a title that has "is SGML"
in it. The string may occur anywhere in the descendants
of the title element, and markup boundaries are
ignored.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&lt;result&gt;
{
doc("sgml.xml")//section[.//title[contains(., "is SGML")]]
}
&lt;/result&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<p>Elements whose start-tags are on lines 50, 59</p>
</div>
<div class="div4">
<h5><a id="sgml-queries-results-q8b" name=
"sgml-queries-results-q8b"></a>1.5.4.9 Q8b</h5>
<p>Same as (Q8a), but the string "is SGML" cannot be
interrupted by sub-elements, and must appear in a
single text node.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&lt;result&gt;
{
doc("sgml.xml")//section[.//title/text()[contains(., "is SGML")]]
}
&lt;/result&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<p>Element whose start-tag is on line 59</p>
</div>
<div class="div4">
<h5><a id="sgml-queries-results-q9" name=
"sgml-queries-results-q9"></a>1.5.4.10 Q9</h5>
<p>Locate all the topics referenced by a
cross-reference anywhere in the report (all the "topic"
elements whose "topicid" attribute value is the same as
an "xrefid" attribute value of any "xref" element).</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&lt;result&gt;
{
for $id in doc("sgml.xml")//xref/@xrefid
return doc("sgml.xml")//topic[@topicid = $id]
}
&lt;/result&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<p>Element whose start-tag is on line 65</p>
</div>
<div class="div4">
<h5><a id="sgml-queries-results-q10" name=
"sgml-queries-results-q10"></a>1.5.4.11 Q10</h5>
<p>Locate the closest title preceding the
cross-reference ("xref") element whose "xrefid"
attribute is "top4" (the "title" element that would be
touched last before this "xref" element when touching
each element in document order).</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&lt;result&gt;
{
let $x := doc("sgml.xml")//xref[@xrefid = "top4"],
$t := doc("sgml.xml")//title[. &lt;&lt; $x]
return $t[last()]
}
&lt;/result&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<p>Given xref on line 79, element whose start-tag is on
line 75</p>
</div>
</div>
</div>
<div class="div2">
<h3><a id="text" name="text"></a>1.6 Use Case "STRING":
String Search</h3>
<div class="div3">
<h4><a id="text-description" name=
"text-description"></a>1.6.1 Description</h4>
<p>This use case is based on company profiles and a set
of news documents which contain data for PR, mergers and
acquisitions, etc. Given a company, the use case
illustrates several different queries for searching text
in news documents and different ways of providing query
results by matching the information from the company
profile and the content of the news items.</p>
<p>In this use case, the <code>contains</code> function
is used to test whether a string occurs within a node or
a string. Obviously, using full-text functions would
provide more powerful searching, but the current
Functions and Operators draft does not have full-text
functions.</p>
</div>
<div class="div3">
<h4><a id="text-dtd" name="text-dtd"></a>1.6.2 Document
Type Definition (DTD)</h4>
<p>This use case uses data that corresponds to the
following DTDs:</p>
<div class="schema">
<div class="exampleInner">
<pre>
&lt;!ELEMENT company (name, ticker_symbol?, description?,
business_code, partners?, competitors?)&gt;
&lt;!ELEMENT name (#PCDATA)&gt;
&lt;!ELEMENT ticker_symbol (#PCDATA)&gt;
&lt;!ELEMENT description (#PCDATA)&gt;
&lt;!ELEMENT business_code (#PCDATA)&gt;
&lt;!ELEMENT partners (partner+)&gt;
&lt;!ELEMENT partner (#PCDATA)&gt;
&lt;!ELEMENT competitors (competitor+)&gt;
&lt;!ELEMENT competitor (#PCDATA)&gt;
</pre>
</div>
</div>
<div class="schema">
<div class="exampleInner">
<pre>
&lt;!ELEMENT news (news_item*)&gt;
&lt;!ELEMENT news_item (title, content, date, author?, news_agent)&gt;
&lt;!ELEMENT title (#PCDATA)&gt;
&lt;!ELEMENT content (par | figure)+ &gt;
&lt;!ELEMENT date (#PCDATA)&gt;
&lt;!ELEMENT author (#PCDATA)&gt;
&lt;!ELEMENT news_agent (#PCDATA)&gt;
&lt;!ELEMENT par (#PCDATA | quote | footnote)*&gt;
&lt;!ELEMENT quote (#PCDATA)&gt;
&lt;!ELEMENT footnote (#PCDATA)&gt;
&lt;!ELEMENT figure (title, image)&gt;
&lt;!ELEMENT image EMPTY&gt;
&lt;!ATTLIST image
source CDATA #REQUIRED &gt;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="text-data" name="text-data"></a>1.6.3 Sample
Data</h4>
<p>The queries in this use case are based on the
following input data, which is found in the file
"string.xml".</p>
<div class="data">
<div class="exampleInner">
<pre>
&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
&lt;news&gt;
&lt;news_item&gt;
&lt;title&gt; Gorilla Corporation acquires YouNameItWeIntegrateIt.com &lt;/title&gt;
&lt;content&gt;
&lt;par&gt; Today, Gorilla Corporation announced that it will purchase
YouNameItWeIntegrateIt.com. The shares of
YouNameItWeIntegrateIt.com dropped $3.00 as a result of this
announcement.
&lt;/par&gt;
&lt;par&gt; As a result of this acquisition, the CEO of
YouNameItWeIntegrateIt.com Bill Smarts resigned. He did not
announce what he will do next. Sources close to
YouNameItWeIntegrateIt.com hint that Bill Smarts might be
taking a position in Foobar Corporation.
&lt;/par&gt;
&lt;par&gt;YouNameItWeIntegrateIt.com is a leading systems integrator
that enables &lt;quote&gt;brick and mortar&lt;/quote&gt; companies to
have a presence on the web.
&lt;/par&gt;
&lt;/content&gt;
&lt;date&gt;1-20-2000&lt;/date&gt;
&lt;author&gt;Mark Davis&lt;/author&gt;
&lt;news_agent&gt;News Online&lt;/news_agent&gt;
&lt;/news_item&gt;
&lt;news_item&gt;
&lt;title&gt;Foobar Corporation releases its new line of Foo products
today&lt;/title&gt;
&lt;content&gt;
&lt;par&gt; Foobar Corporation releases the 20.9 version of its Foo
products. The new version of Foo products solve known
performance problems which existed in 20.8 line and
increases the speed of Foo based products tenfold. It also
allows wireless clients to be connected to the Foobar
servers.
&lt;/par&gt;
&lt;par&gt; The President of Foobar Corporation announced that they
were proud to release 20.9 version of Foo products and
they will upgrade existing customers &lt;footnote&gt;where
service agreements exist&lt;/footnote&gt;
promptly. TheAppCompany Inc. immediately announced that it
will release the new version of its products to utilize
the 20.9 architecture within the next three months.
&lt;/par&gt;
&lt;figure&gt;
&lt;title&gt;Presidents of Foobar Corporation and TheAppCompany
Inc. Shake Hands&lt;/title&gt; &lt;image source="handshake.jpg"/&gt;
&lt;/figure&gt;
&lt;/content&gt;
&lt;date&gt;1-20-2000&lt;/date&gt;
&lt;news_agent&gt;Foobar Corporation&lt;/news_agent&gt;
&lt;/news_item&gt;
&lt;news_item&gt; &lt;title&gt;Foobar Corporation is suing Gorilla Corporation for
patent infringement &lt;/title&gt;
&lt;content&gt;
&lt;par&gt; In surprising developments today, Foobar Corporation
announced that it is suing Gorilla Corporation for patent
infringement. The patents that were mentioned as part of the
lawsuit are considered to be the basis of Foobar
Corporation's &lt;quote&gt;Wireless Foo&lt;/quote&gt; line of products.
&lt;/par&gt;
&lt;par&gt;The tension between Foobar and Gorilla Corporations has
been increasing ever since the Gorilla Corporation acquired
more than 40 engineers who have left Foobar Corporation,
TheAppCompany Inc. and YouNameItWeIntegrateIt.com over the
past 3 months. The engineers who have left the Foobar
corporation and its partners were rumored to be working on
the next generation of server products and applications which
will directly compete with Foobar's Foo 20.9 servers. Most of
the engineers have relocated to Hawaii where the Gorilla
Corporation's server development is located.
&lt;/par&gt;
&lt;/content&gt;
&lt;date&gt;1-20-2000&lt;/date&gt;
&lt;news_agent&gt;Reliable News Corporation&lt;/news_agent&gt;
&lt;/news_item&gt;
&lt;/news&gt;
</pre>
</div>
</div>
<p>In addition, the following data, listing the partners
and competitors of companies, is found in the file
"company-data.xml".</p>
<div class="data">
<div class="exampleInner">
<pre>
&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
&lt;!DOCTYPE company SYSTEM "company.dtd"&gt;
&lt;company&gt;
&lt;name&gt;Foobar Corporation&lt;/name&gt;
&lt;ticker_symbol&gt;FOO&lt;/ticker_symbol&gt;
&lt;description&gt;Foobar Corporation is a maker of Foo(TM) and
Foobar(TM) products and a leading software company with a 300
Billion dollar revenue in 1999. It is located in Alaska.
&lt;/description&gt;
&lt;business_code&gt;Software&lt;/business_code&gt;
&lt;partners&gt;
&lt;partner&gt;YouNameItWeIntegrateIt.com&lt;/partner&gt;
&lt;partner&gt;TheAppCompany Inc.&lt;/partner&gt;
&lt;/partners&gt;
&lt;competitors&gt;
&lt;competitor&gt;Gorilla Corporation&lt;/competitor&gt;
&lt;/competitors&gt;
&lt;/company&gt;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="text-queries-results" name=
"text-queries-results"></a>1.6.4 Queries and Results</h4>
<div class="div4">
<h5><a id="text-queries-results-q1" name=
"text-queries-results-q1"></a>1.6.4.1 Q1</h5>
<p>Find the titles of all news items where the string
"Foobar Corporation" appears in the title.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
doc("string.xml")//news_item/title[contains(., "Foobar Corporation")]
</pre>
</div>
</div>
<p><em>Expected Results</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;title&gt;Foobar Corporation releases its new line of Foo
products today&lt;/title&gt;
&lt;title&gt;Foobar Corporation is suing Gorilla Corporation for patent
infringement &lt;/title&gt;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="text-queries-results-q2" name=
"text-queries-results-q2"></a>1.6.4.2 Q2</h5>
<p>Find news items where the Foobar Corporation and one
or more of its partners are mentioned in the same
paragraph and/or title. List each news item by its
title and date.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
declare function local:partners($company as xs:string) as element()*
{
let $c := doc("company-data.xml")//company[name = $company]
return $c//partner
};
let $foobar_partners := local:partners("Foobar Corporation")
for $item in doc("string.xml")//news_item
where
some $t in $item//title satisfies
(contains($t/text(), "Foobar Corporation")
and (some $partner in $foobar_partners satisfies
contains($t/text(), $partner/text())))
or (some $par in $item//par satisfies
(contains(string($par), "Foobar Corporation")
and (some $partner in $foobar_partners satisfies
contains(string($par), $partner/text()))))
return
&lt;news_item&gt;
{ $item/title }
{ $item/date }
&lt;/news_item&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;news_item&gt;
&lt;title&gt; Gorilla Corporation acquires YouNameItWeIntegrateIt.com &lt;/title&gt;
&lt;date&gt;1-20-2000&lt;/date&gt;
&lt;/news_item&gt;
&lt;news_item&gt;
&lt;title&gt;Foobar Corporation releases its new line of Foo products today&lt;/title&gt;
&lt;date&gt;1-20-2000&lt;/date&gt;
&lt;/news_item&gt;
&lt;news_item&gt;
&lt;title&gt;Foobar Corporation is suing Gorilla Corporation for patent
infringement &lt;/title&gt;
&lt;date&gt;1-20-2000&lt;/date&gt;
&lt;/news_item&gt;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="text-queries-results-q3" name=
"text-queries-results-q3"></a>1.6.4.3 Q3</h5>
<div class="note">
<p class="prefix"><b>Note:</b></p>
<p>Query Q3 has been withdrawn from the use cases
document.</p>
</div>
</div>
<div class="div4">
<h5><a id="text-queries-results-q4" name=
"text-queries-results-q4"></a>1.6.4.4 Q4</h5>
<p>Find news items where a company and one of its
partners is mentioned in the same news item and the
news item is not authored by the company itself.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
declare function local:partners($company as xs:string) as element()*
{
let $c := doc("company-data.xml")//company[name = $company]
return $c//partner
};
for $item in doc("string.xml")//news_item,
$c in doc("company-data.xml")//company
let $partners := local:partners($c/name)
where contains(string($item), $c/name)
and (some $p in $partners satisfies
contains(string($item), $p) and $item/news_agent != $c/name)
return
$item
</pre>
</div>
</div>
<p><em>Expected Results:</em> The expected results are
the news item elements with the following titles:</p>
<ul>
<li>
<p>Gorilla Corporation acquires
YouNameItWeIntegrateIt.com</p>
</li>
<li>
<p>Foobar Corporation is suing Gorilla Corporation
for patent infringement</p>
</li>
</ul>
</div>
<div class="div4">
<h5><a id="text-queries-results-q5" name=
"text-queries-results-q5"></a>1.6.4.5 Q5</h5>
<p>For each news item that is relevant to the Gorilla
Corporation, create an "item summary" element. The
content of the item summary is the content of the
title, date, and first paragraph of the news item,
separated by periods. A news item is relevant if the
name of the company is mentioned anywhere within the
content of the news item.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
for $item in doc("string.xml")//news_item
where contains(string($item/content), "Gorilla Corporation")
return
&lt;item_summary&gt;
{ concat($item/title,". ") }
{ concat($item/date,". ") }
{ string(($item//par)[1]) }
&lt;/item_summary&gt;
</pre>
</div>
</div>
<p><em>Expected Result: (with whitespace reformatted
for readability)</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;item_summary&gt;Gorilla Corporation acquires
YouNameItWeIntegrateIt.com. 1-20-2000. Today, Gorilla Corporation
announced that it will purchase YouNameItWeIntegrateIt.com. The shares
of YouNameItWeIntegrateIt.com dropped $3.00 as a result of this
announcement.&lt;/item_summary&gt;
&lt;item_summary&gt;Foobar Corporation is suing Gorilla Corporation for
patent infringement. 1-20-2000. In surprising developments today,
Foobar Corporation announced that it is suing Gorilla Corporation for
patent infringement. The patents that were mentioned as part of the
lawsuit are considered to be the basis of Foobar Corporation's
Wireless Foo line of products.&lt;/item_summary&gt;
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="div2">
<h3><a id="ns" name="ns"></a>1.7 Use Case "NS" - Queries
Using Namespaces</h3>
<p>This use case performs a variety of queries on
namespace-qualified names.</p>
<div class="div3">
<h4><a id="ns-description" name=
"ns-description"></a>1.7.1 Description</h4>
<p>This use case is based on a scenario in which a
neutral mediator is acting with public auction servers on
behalf of clients. The reason for a client to use this
imaginary service may be anonymity, better insurance, or
the possibility to cover more than one market at a time.
The following aspects of namespaces are illustrated by
this use case:</p>
<ul>
<li>
<p>Syntactic disambiguation when combining XML data
from different sources</p>
</li>
<li>
<p>Re-use of predefined modules, such as XLinks or
XML Schema</p>
</li>
<li>
<p>Support for global classification schemas, such as
the Dublin Core</p>
</li>
</ul>
<p>The sample data consists of two records. The schema
used for this data uses W3C XML Schema's schema
composition to create a schema from predefined, namespace
separated modules, and uses XLink to express references.
Each record describes a running auction. It embeds data
specific to an auctioneer (e.g. the company's credit
rating system) and a taxonomy specific to a particular
good (jazz records) in a framework that contains data
common to all auctions (e.g. start and end time), using
namespaces to distinguish the three vocabularies.</p>
<p>Note that namespace prefixes must be resolved to their
Namespace URIs before matching namespace qualified names.
It is not sufficient to use the literal prefixes to
denote namespaces. Furthermore, there are several
possible ways to represent namespace declarations.
Therefore, processing must be done on the namespace
processed XML Information Set, not on the XML text
representation.</p>
</div>
<div class="div3">
<h4><a id="ns-dtd" name="ns-dtd"></a>1.7.2 Document Type
Definition (DTD)</h4>
<p>DTDs are not fully compatible with namespaces as they
can not express the equality of nodes in the same
namespace, but different namespace proxies. In a later
version of this paper, an XML Schema should be added
here.</p>
<p>This data for this use case is in the file
"auction.xml".</p>
</div>
<div class="div3">
<h4><a id="ns-data" name="ns-data"></a>1.7.3 Sample
Data</h4>
<div class="data">
<div class="exampleInner">
<pre>
&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
&lt;ma:AuctionWatchList
xmlns:ma="http://www.example.com/AuctionWatch"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:anyzone="http://www.example.com/auctioneers#anyzone"
xmlns:eachbay="http://www.example.com/auctioneers#eachbay"
xmlns:yabadoo="http://www.example.com/auctioneers#yabadoo"
&gt;
&lt;!-- ________________________________________________________________________________ --&gt;
&lt;ma:Auction anyzone:ID="0321K372910"&gt;
&lt;ma:AuctionHomepage
xlink:type="simple"
xlink:href="http://www.example.com/item/0321K372910"
/&gt;
&lt;ma:Schedule&gt;
&lt;ma:Open xmlns:dt="http://www.w3.org/2001/XMLSchema"
dt:type="timeInstant"&gt;2000-03-21:07:41:34-05:00&lt;/ma:Open&gt;
&lt;ma:Close xmlns:dt="http://www.w3.org/2001/XMLSchema"
dt:type="timeInstant"&gt;2000-03-23:07:41:34-05:00&lt;/ma:Close&gt;
&lt;/ma:Schedule&gt;
&lt;ma:Price&gt;
&lt;ma:Start ma:currency="USD"&gt;3.00&lt;/ma:Start&gt;
&lt;ma:Current ma:currency="USD"&gt;10.00&lt;/ma:Current&gt;
&lt;ma:Number_of_Bids&gt;5&lt;/ma:Number_of_Bids&gt;
&lt;/ma:Price&gt;
&lt;ma:Trading_Partners&gt;
&lt;ma:High_Bidder&gt;
&lt;eachbay:ID&gt;RecordsRUs&lt;/eachbay:ID&gt;
&lt;eachbay:PositiveComments&gt;231&lt;/eachbay:PositiveComments&gt;
&lt;eachbay:NeutralComments&gt;2&lt;/eachbay:NeutralComments&gt;
&lt;eachbay:NegativeComments&gt;5&lt;/eachbay:NegativeComments&gt;
&lt;ma:MemberInfoPage
xlink:type="simple"
xlink:href="http://auction.eachbay.com/members?get=RecordsRUs"
xlink:role="ma:MemberInfoPage"
/&gt;
&lt;/ma:High_Bidder&gt;
&lt;ma:Seller&gt;
&lt;anyzone:ID&gt;VintageRecordFreak&lt;/anyzone:ID&gt;
&lt;anyzone:Member_Since&gt;October 1999&lt;/anyzone:Member_Since&gt;
&lt;anyzone:Rating&gt;5&lt;/anyzone:Rating&gt;
&lt;ma:MemberInfoPage
xlink:type="simple"
xlink:href="http://auction.anyzone.com/members/VintageRecordFreak"
xlink:role="ma:MemberInfoPage"
/&gt;
&lt;/ma:Seller&gt;
&lt;/ma:Trading_Partners&gt;
&lt;ma:Details&gt;
&lt;record xmlns="http://www.example.org/music/records"&gt;
&lt;artist&gt;Miles Davis&lt;/artist&gt;
&lt;title&gt;In a Silent Way&lt;/title&gt;
&lt;recorded&gt;1969&lt;/recorded&gt;
&lt;label&gt;Columbia Records&lt;/label&gt;
&lt;remark&gt;With Miles Davis (trumpet), Herbie Hancock (Electric
Piano), Chick Corea (Electric Piano), Wayne Shorter
(Tenor Sax), Josef Zawinul (Electric Piano &amp;amp;
Organ), John McLaughlin (Guitar), and Tony Williams
(Drums). The liner notes were written by Frank Glenn,
and the record is in fine condition.&lt;/remark&gt;
&lt;/record&gt;
&lt;/ma:Details&gt;
&lt;/ma:Auction&gt;
&lt;!-- ________________________________________________________________________________ --&gt;
&lt;ma:Auction yabadoo:ID="13143816"&gt;
&lt;ma:AuctionHomepage
xlink:type="simple"
xlink:href="http://auctions.yabadoo.com/auction/13143816"
/&gt;
&lt;ma:Schedule&gt;
&lt;ma:Open xmlns:dt="http://www.w3.org/2001/XMLSchema"
dt:type="timeInstant"&gt;2000-03-19:17:03:00-04:00&lt;/ma:Open&gt;
&lt;ma:Close xmlns:dt="http://www.w3.org/2001/XMLSchema"
dt:type="timeInstant"&gt;2000-03-29:17:03:00-04:00&lt;/ma:Close&gt;
&lt;/ma:Schedule&gt;
&lt;ma:Price&gt;
&lt;ma:Start ma:currency="USD"&gt;3.00&lt;/ma:Start&gt;
&lt;ma:Current ma:currency="USD"&gt;3.00&lt;/ma:Current&gt;
&lt;ma:Number_of_Bids&gt;0&lt;/ma:Number_of_Bids&gt;
&lt;/ma:Price&gt;
&lt;ma:Trading_Partners&gt;
&lt;ma:High_Bidder&gt;
&lt;eachbay:ID&gt;VintageRecordFreak&lt;/eachbay:ID&gt;
&lt;eachbay:PositiveComments&gt;232&lt;/eachbay:PositiveComments&gt;
&lt;eachbay:NeutralComments&gt;0&lt;/eachbay:NeutralComments&gt;
&lt;eachbay:NegativeComments&gt;0&lt;/eachbay:NegativeComments&gt;
&lt;ma:MemberInfoPage
xlink:type="simple"
xlink:href="http://auction.eachbay.com/showRating/user=VintageRecordFreak"
xlink:role="ma:MemberInfoPage"
/&gt;
&lt;/ma:High_Bidder&gt;
&lt;ma:Seller xmlns:seller="http://www.example.com/auctioneers#eachbay"&gt;
&lt;seller:ID&gt;StarsOn45&lt;/seller:ID&gt;
&lt;seller:PositiveComments&gt;80&lt;/seller:PositiveComments&gt;
&lt;seller:NeutralComments&gt;1&lt;/seller:NeutralComments&gt;
&lt;seller:NegativeComments&gt;2&lt;/seller:NegativeComments&gt;
&lt;ma:MemberInfoPage
xlink:type="simple"
xlink:href="http://auction.eachbay.com/showRating/user=StarsOn45"
xlink:role="ma:MemberInfoPage"
/&gt;
&lt;/ma:Seller&gt;
&lt;/ma:Trading_Partners&gt;
&lt;ma:Details&gt;
&lt;record xmlns="http://www.example.org/music/records"&gt;
&lt;artist&gt;Wynton Marsalis&lt;/artist&gt;
&lt;title&gt;Think of One ...&lt;/title&gt;
&lt;recorded&gt;1983&lt;/recorded&gt;
&lt;label&gt;Columbia Records&lt;/label&gt;
&lt;remark xml:lang="en"&gt;Columbia Records 12" 33-1/3 rpm LP,
#FC-38641, Stereo. The record is still clean and shiny
and looks unplayed (looks like NM condition). The
cover has very light surface and edge wear.&lt;/remark&gt;
&lt;remark xml:lang="de"&gt;Columbia Records 12" 33-1/3 rpm LP,
#FC-38641, Stereo. Die Platte ist noch immer sauber
und glänzend und sieht ungespielt aus
(NM Zustand). Das Cover hat leichte Abnutzungen an
Oberfläche und Ecken.&lt;/remark&gt;
&lt;/record&gt;
&lt;/ma:Details&gt;
&lt;/ma:Auction&gt;
&lt;/ma:AuctionWatchList&gt;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="ns-queries-results" name=
"ns-queries-results"></a>1.7.4 Queries and Results</h4>
<div class="div4">
<h5><a id="ns-queries-results-q1" name=
"ns-queries-results-q1"></a>1.7.4.1 Q1</h5>
<p>List all unique namespaces used in the sample
data.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
&lt;Q1&gt;
{
for $n in distinct-values(
for $i in (doc("auction.xml")//* | doc("auction.xml")//@*)
return namespace-uri($i)
)
return &lt;ns&gt;{$n}&lt;/ns&gt;
}
&lt;/Q1&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;Q1&gt;
&lt;ns&gt;http://www.example.com/AuctionWatch&lt;/ns&gt;
&lt;ns&gt;http://www.example.com/auctioneers#anyzone&lt;/ns&gt;
&lt;ns&gt;http://www.w3.org/1999/xlink&lt;/ns&gt;
&lt;ns&gt;http://www.w3.org/2001/XMLSchema&lt;/ns&gt;
&lt;ns&gt;http://www.example.com/auctioneers#eachbay&lt;/ns&gt;
&lt;ns&gt;http://www.example.org/music/records&lt;/ns&gt;
&lt;ns&gt;http://www.example.com/auctioneers#yabadoo&lt;/ns&gt;
&lt;ns&gt;http://www.w3.org/XML/1998/namespace&lt;/ns&gt;
&lt;/Q1&gt;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="ns-queries-results-q2" name=
"ns-queries-results-q2"></a>1.7.4.2 Q2</h5>
<p>Select the title of each record that is for
sale.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
declare namespace music = "http://www.example.org/music/records";
&lt;Q2&gt;
{
doc("auction.xml")//music:title
}
&lt;/Q2&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;Q2&gt;
&lt;title xmlns="http://www.example.org/music/records"
xmlns:anyzone="http://www.example.com/auctioneers#anyzone"
xmlns:eachbay="http://www.example.com/auctioneers#eachbay"
xmlns:ma="http://www.example.com/AuctionWatch"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:yabadoo="http://www.example.com/auctioneers#yabadoo"&gt;In a Silent Way&lt;/title&gt;
&lt;title xmlns="http://www.example.org/music/records"
xmlns:anyzone="http://www.example.com/auctioneers#anyzone"
xmlns:eachbay="http://www.example.com/auctioneers#eachbay"
xmlns:ma="http://www.example.com/AuctionWatch"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:yabadoo="http://www.example.com/auctioneers#yabadoo"&gt;Think of One ...&lt;/title&gt;
&lt;/Q2&gt;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="ns-queries-results-q3" name=
"ns-queries-results-q3"></a>1.7.4.3 Q3</h5>
<p>Select all elements that have an attribute whose
name is in the XML Schema namespace.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
declare namespace dt = "http://www.w3.org/2001/XMLSchema";
&lt;Q3&gt;
{
doc("auction.xml")//*[@dt:*]
}
&lt;/Q3&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;Q3&gt;
&lt;ma:Open
dt:type="timeInstant"
xmlns:anyzone="http://www.example.com/auctioneers#anyzone"
xmlns:dt="http://www.w3.org/2001/XMLSchema"
xmlns:eachbay="http://www.example.com/auctioneers#eachbay"
xmlns:ma="http://www.example.com/AuctionWatch"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:yabadoo="http://www.example.com/auctioneers#yabadoo"&gt;2000-03-21:07:41:34-05:00&lt;/ma:Open&gt;
&lt;ma:Close
dt:type="timeInstant"
xmlns:anyzone="http://www.example.com/auctioneers#anyzone"
xmlns:dt="http://www.w3.org/2001/XMLSchema"
xmlns:eachbay="http://www.example.com/auctioneers#eachbay"
xmlns:ma="http://www.example.com/AuctionWatch"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:yabadoo="http://www.example.com/auctioneers#yabadoo"&gt;2000-03-23:07:41:34-05:00&lt;/ma:Close&gt;
&lt;ma:Open
dt:type="timeInstant"
xmlns:anyzone="http://www.example.com/auctioneers#anyzone"
xmlns:dt="http://www.w3.org/2001/XMLSchema"
xmlns:eachbay="http://www.example.com/auctioneers#eachbay"
xmlns:ma="http://www.example.com/AuctionWatch"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:yabadoo="http://www.example.com/auctioneers#yabadoo"&gt;2000-03-19:17:03:00-04:00&lt;/ma:Open&gt;
&lt;ma:Close
dt:type="timeInstant"
xmlns:anyzone="http://www.example.com/auctioneers#anyzone"
xmlns:dt="http://www.w3.org/2001/XMLSchema"
xmlns:eachbay="http://www.example.com/auctioneers#eachbay"
xmlns:ma="http://www.example.com/AuctionWatch"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:yabadoo="http://www.example.com/auctioneers#yabadoo"&gt;2000-03-29:17:03:00-04:00&lt;/ma:Close&gt;
&lt;/Q3&gt;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="ns-queries-results-q4" name=
"ns-queries-results-q4"></a>1.7.4.4 Q4</h5>
<p>List the target URI's of all XLinks in the
document.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
declare namespace xlink = "http://www.w3.org/1999/xlink";
&lt;Q4 xmlns:xlink="http://www.w3.org/1999/xlink"&gt;
{
for $hr in doc("auction.xml")//@xlink:href
return &lt;ns&gt;{ $hr }&lt;/ns&gt;
}
&lt;/Q4&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;Q4 xmlns:xlink="http://www.w3.org/1999/xlink"&gt;
&lt;ns xlink:href="http://www.example.com/item/0321K372910"/&gt;
&lt;ns xlink:href="http://auction.eachbay.com/members?get=RecordsRUs"/&gt;
&lt;ns xlink:href="http://auction.anyzone.com/members/VintageRecordFreak"/&gt;
&lt;ns xlink:href="http://auctions.yabadoo.com/auction/13143816"/&gt;
&lt;ns xlink:href="http://auction.eachbay.com/showRating/user=VintageRecordFreak"/&gt;
&lt;ns xlink:href="http://auction.eachbay.com/showRating/user=StarsOn45"/&gt;
&lt;/Q4&gt;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="ns-queries-results-q5" name=
"ns-queries-results-q5"></a>1.7.4.5 Q5</h5>
<p>Select all records that have a remark in German.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
declare namespace music = "http://www.example.org/music/records";
&lt;Q5 xmlns:music="http://www.example.org/music/records"&gt;
{
doc("auction.xml")//music:record[music:remark/@xml:lang = "de"]
}
&lt;/Q5&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;Q5 xmlns:music="http://www.example.org/music/records"&gt;
&lt;record xmlns="http://www.example.org/music/records"
xmlns:anyzone="http://www.example.com/auctioneers#anyzone"
xmlns:eachbay="http://www.example.com/auctioneers#eachbay"
xmlns:ma="http://www.example.com/AuctionWatch"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:yabadoo="http://www.example.com/auctioneers#yabadoo"&gt;
&lt;artist&gt;Wynton Marsalis&lt;/artist&gt;
&lt;title&gt;Think of One ...&lt;/title&gt;
&lt;recorded&gt;1983&lt;/recorded&gt;
&lt;label&gt;Columbia Records&lt;/label&gt;
&lt;remark xml:lang="en"&gt;Columbia Records 12" 33-1/3 rpm LP,
#FC-38641, Stereo. The record is still clean and shiny
and looks unplayed (looks like NM condition). The
cover has very light surface and edge wear.&lt;/remark&gt;
&lt;remark xml:lang="de"&gt;Columbia Records 12" 33-1/3 rpm LP,
#FC-38641, Stereo. Die Platte ist noch immer sauber
und glänzend und sieht ungespielt aus
(NM Zustand). Das Cover hat leichte Abnutzungen an
Oberfläche und Ecken.&lt;/remark&gt;
&lt;/record&gt;
&lt;/Q5&gt;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="ns-queries-results-q6" name=
"ns-queries-results-q6"></a>1.7.4.6 Q6</h5>
<p>Select the closing time elements of all AnyZone
auctions currently monitored.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
declare namespace ma = "http://www.example.com/AuctionWatch";
declare namespace anyzone = "http://www.example.com/auctioneers#anyzone";
&lt;Q6 xmlns:ma="http://www.example.com/AuctionWatch"&gt;
{
doc("auction.xml")//ma:Auction[@anyzone:ID]/ma:Schedule/ma:Close
}
&lt;/Q6&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;Q6 xmlns:ma="http://www.example.com/AuctionWatch"&gt;
&lt;ma:Close
xmlns:anyzone="http://www.example.com/auctioneers#anyzone"
xmlns:dt="http://www.w3.org/2001/XMLSchema"
xmlns:eachbay="http://www.example.com/auctioneers#eachbay"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:yabadoo="http://www.example.com/auctioneers#yabadoo"
dt:type="timeInstant"&gt;2000-03-23:07:41:34-05:00&lt;/ma:Close&gt;
&lt;/Q6&gt;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="ns-queries-results-q7" name=
"ns-queries-results-q7"></a>1.7.4.7 Q7</h5>
<p>Select the homepage of all auctions where both
seller and high bidder are registered at the same
auctioneer.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
declare namespace ma = "http://www.example.com/AuctionWatch";
&lt;Q7 xmlns:xlink="http://www.w3.org/1999/xlink"&gt;
{
for $a in doc("auction.xml")//ma:Auction
let $seller_id := $a/ma:Trading_Partners/ma:Seller/*:ID,
$buyer_id := $a/ma:Trading_Partners/ma:High_Bidder/*:ID
where namespace-uri($seller_id) = namespace-uri($buyer_id)
return
$a/ma:AuctionHomepage
}
&lt;/Q7&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;Q7 xmlns:xlink="http://www.w3.org/1999/xlink" &gt;
&lt;ma:AuctionHomepage xmlns:ma="http://www.example.com/AuctionWatch"
xlink:type="simple"
xlink:href="http://auctions.yabadoo.com/auction/13143816" /&gt;
&lt;/Q7&gt;
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="ns-queries-results-q8" name=
"ns-queries-results-q8"></a>1.7.4.8 Q8</h5>
<p>Select all traders (either seller or high bidder)
without negative comments</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
declare namespace ma = "http://www.example.com/AuctionWatch";
&lt;Q8 xmlns:ma="http://www.example.com/AuctionWatch"
xmlns:eachbay="http://www.example.com/auctioneers#eachbay"
xmlns:xlink="http://www.w3.org/1999/xlink"&gt;
{
for $s in doc("auction.xml")//ma:Trading_Partners/(ma:Seller | ma:High_Bidder)
where $s/*:NegativeComments = 0
return $s
}
&lt;/Q8&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;Q8 xmlns:eachbay="http://www.example.com/auctioneers#eachbay"
xmlns:ma="http://www.example.com/AuctionWatch"
xmlns:xlink="http://www.w3.org/1999/xlink"&gt;
&lt;ma:High_Bidder xmlns:anyzone="http://www.example.com/auctioneers#anyzone"
xmlns:yabadoo="http://www.example.com/auctioneers#yabadoo"&gt;
&lt;eachbay:ID&gt;VintageRecordFreak&lt;/eachbay:ID&gt;
&lt;eachbay:PositiveComments&gt;232&lt;/eachbay:PositiveComments&gt;
&lt;eachbay:NeutralComments&gt;0&lt;/eachbay:NeutralComments&gt;
&lt;eachbay:NegativeComments&gt;0&lt;/eachbay:NegativeComments&gt;
&lt;ma:MemberInfoPage
xlink:href="http://auction.eachbay.com/showRating/user=VintageRecordFreak"
xlink:role="ma:MemberInfoPage"
xlink:type="simple"/&gt;
&lt;/ma:High_Bidder&gt;
&lt;/Q8&gt;
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="div2">
<h3><a id="parts" name="parts"></a>1.8 Use Case "PARTS" -
Recursive Parts Explosion</h3>
<p>This use case illustrates how a recursive query might be
used to construct a hierarchic document of arbitrary depth
from flat structures stored in a database.</p>
<div class="div3">
<h4><a id="parts-description" name=
"parts-description"></a>1.8.1 Description</h4>
<p>This use case is based on a "parts explosion" database
that contains information about how parts are used in
other parts.</p>
<p>The input to the use case is a "flat" document in
which each different part is represented by a
&lt;part&gt; element with partid and name attributes.
Each part may or may not be part of a larger part; if so,
the partid of the larger part is contained in a partof
attribute. This input document might be derived from a
relational database in which each part is represented by
a row of a table with partid as primary key and partof as
a foreign key referencing partid.</p>
<p>The challenge of this use case is to write a query
that converts the "flat" representation of the parts
explosion, based on foreign keys, into a hierarchic
representation in which part containment is represented
by the structure of the document.</p>
</div>
<div class="div3">
<h4><a id="parts-dtd" name="parts-dtd"></a>1.8.2 Document
Type Definitions (DTD)</h4>
<p>The input data set uses the following DTD:</p>
<div class="schema">
<div class="exampleInner">
<pre>
&lt;!DOCTYPE partlist [
&lt;!ELEMENT partlist (part*)&gt;
&lt;!ELEMENT part EMPTY&gt;
&lt;!ATTLIST part
partid CDATA #REQUIRED
partof CDATA #IMPLIED
name CDATA #REQUIRED&gt;
]&gt;
</pre>
</div>
</div>
<p>Although the <code>partid</code> and
<code>partof</code> attributes could have been of type ID
and IDREF, respectively, in this schema they are treated
as character data, possibly materialized in a
straightforward way from a relational database. Each
<code>partof</code> attribute matches exactly one
<code>partid</code>. Parts having no <code>partof</code>
attribute are not contained in any other part.</p>
<p>The output data conforms to the following DTD:</p>
<div class="schema">
<div class="exampleInner">
<pre>
&lt;!DOCTYPE parttree [
&lt;!ELEMENT parttree (part*)&gt;
&lt;!ELEMENT part (part*)&gt;
&lt;!ATTLIST part
partid CDATA #REQUIRED
name CDATA #REQUIRED&gt;
]&gt;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="parts-data" name="parts-data"></a>1.8.3 Sample
Data</h4>
<div class="data">
<div class="exampleInner">
<pre>
&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
&lt;partlist&gt;
&lt;part partid="0" name="car"/&gt;
&lt;part partid="1" partof="0" name="engine"/&gt;
&lt;part partid="2" partof="0" name="door"/&gt;
&lt;part partid="3" partof="1" name="piston"/&gt;
&lt;part partid="4" partof="2" name="window"/&gt;
&lt;part partid="5" partof="2" name="lock"/&gt;
&lt;part partid="10" name="skateboard"/&gt;
&lt;part partid="11" partof="10" name="board"/&gt;
&lt;part partid="12" partof="10" name="wheel"/&gt;
&lt;part partid="20" name="canoe"/&gt;
&lt;/partlist&gt;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="parts-queries-results" name=
"parts-queries-results"></a>1.8.4 Queries and
Results</h4>
<div class="div4">
<h5><a id="parts-queries-results-q1" name=
"parts-queries-results-q1"></a>1.8.4.1 Q1</h5>
<p>Convert the sample document from "partlist" format
to "parttree" format (see DTD section for definitions).
In the result document, part containment is represented
by containment of one &lt;part&gt; element inside
another. Each part that is not part of any other part
should appear as a separate top-level element in the
output document.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
declare function local:one_level($p as element()) as element()
{
&lt;part partid="{ $p/@partid }"
name="{ $p/@name }" &gt;
{
for $s in doc("partlist.xml")//part
where $s/@partof = $p/@partid
return local:one_level($s)
}
&lt;/part&gt;
};
&lt;parttree&gt;
{
for $p in doc("partlist.xml")//part[empty(@partof)]
return local:one_level($p)
}
&lt;/parttree&gt;
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="result">
<div class="exampleInner">
<pre>
&lt;parttree&gt;
&lt;part partid="0" name="car"&gt;
&lt;part partid="1" name="engine"&gt;
&lt;part partid="3" name="piston"/&gt;
&lt;/part&gt;
&lt;part partid="2" name="door"&gt;
&lt;part partid="4" name="window"/&gt;
&lt;part partid="5" name="lock"/&gt;
&lt;/part&gt;
&lt;/part&gt;
&lt;part partid="10" name="skateboard"&gt;
&lt;part partid="11" name="board"/&gt;
&lt;part partid="12" name="wheel"/&gt;
&lt;/part&gt;
&lt;part partid="20" name="canoe"/&gt;
&lt;/parttree&gt;
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="div2">
<h3><a id="strong" name="strong"></a>1.9 Use Case "STRONG"
- queries that exploit strongly typed data</h3>
<div class="div3">
<h4><a id="strong-description" name=
"strong-description"></a>1.9.1 Description</h4>
<p>Strongly typed and weakly typed data are both
important kinds of XML data. Most of the queries in this
document focus on weakly typed data that is governed by a
DTD and does not contain XML Schema simple datatypes or
named complex types. This use case explores XQuery's
support for types, using data that is governed by a
strongly typed XML Schema <a href=
"#xmlschema-0">[XMLSchema0]</a>.</p>
</div>
<div class="div3">
<h4><a id="strong-schema" name="strong-schema"></a>1.9.2
Schema</h4>
<p>The schema for this example is the International
Purchase Order schema taken from the XML Schema Primer,
which imports a schema for addresses. The main schema is
found in a schema document named "ipo.xsd":</p>
<div class="schema">
<div class="exampleInner">
<pre>
&lt;?xml version="1.0"?&gt;
&lt;schema targetNamespace="http://www.example.com/IPO"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:ipo="http://www.example.com/IPO"&gt;
&lt;annotation&gt;
&lt;documentation xml:lang="en"&gt;
International Purchase order schema for Example.com
Copyright 2000 Example.com. All rights reserved.
&lt;/documentation&gt;
&lt;/annotation&gt;
&lt;!-- include address constructs --&gt;
&lt;include
schemaLocation="address.xsd"/&gt;
&lt;element name="purchaseOrder" type="ipo:PurchaseOrderType"/&gt;
&lt;element name="comment" type="string"/&gt;
&lt;element name="shipComment" type="string" substitutionGroup="ipo:comment"/&gt;
&lt;element name="customerComment" type="string" substitutionGroup="ipo:comment"/&gt;
&lt;complexType name="PurchaseOrderType"&gt;
&lt;sequence&gt;
&lt;element name="shipTo" type="ipo:Address"/&gt;
&lt;element name="billTo" type="ipo:Address"/&gt;
&lt;element ref="ipo:comment" minOccurs="0"/&gt;
&lt;element name="items" type="ipo:Items"/&gt;
&lt;/sequence&gt;
&lt;attribute name="orderDate" type="date"/&gt;
&lt;/complexType&gt;
&lt;complexType name="Items"&gt;
&lt;sequence&gt;
&lt;element name="item" minOccurs="0" maxOccurs="unbounded"&gt;
&lt;complexType&gt;
&lt;sequence&gt;
&lt;element name="productName" type="string"/&gt;
&lt;element name="quantity"&gt;
&lt;simpleType&gt;
&lt;restriction base="positiveInteger"&gt;
&lt;maxExclusive value="100"/&gt;
&lt;/restriction&gt;
&lt;/simpleType&gt;
&lt;/element&gt;
&lt;element name="USPrice" type="decimal"/&gt;
&lt;element ref="ipo:comment" minOccurs="0" maxOccurs="unbounded"/&gt;
&lt;element name="shipDate" type="date" minOccurs="0" maxOccurs="unbounded"/&gt;
&lt;/sequence&gt;
&lt;attribute name="partNum" type="ipo:SKU" use="required"/&gt;
&lt;/complexType&gt;
&lt;/element&gt;
&lt;/sequence&gt;
&lt;/complexType&gt;
&lt;simpleType name="SKU"&gt;
&lt;restriction base="string"&gt;
&lt;pattern value="\d{3}-[A-Z]{2}"/&gt;
&lt;/restriction&gt;
&lt;/simpleType&gt;
&lt;/schema&gt;
</pre>
</div>
</div>
<p>The address constructs are found in a schema document
named "address.xsd":</p>
<div class="schema">
<div class="exampleInner">
<pre>
&lt;schema targetNamespace="http://www.example.com/IPO"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:ipo="http://www.example.com/IPO"&gt;
&lt;annotation&gt;
&lt;documentation xml:lang="en"&gt;
Addresses for International Purchase order schema
Copyright 2000 Example.com. All rights reserved.
&lt;/documentation&gt;
&lt;/annotation&gt;
&lt;complexType name="Address"&gt;
&lt;sequence&gt;
&lt;element name="name" type="string"/&gt;
&lt;element name="street" type="string"/&gt;
&lt;element name="city" type="string"/&gt;
&lt;/sequence&gt;
&lt;/complexType&gt;
&lt;complexType name="USAddress"&gt;
&lt;complexContent&gt;
&lt;extension base="ipo:Address"&gt;
&lt;sequence&gt;
&lt;element name="state" type="ipo:USState"/&gt;
&lt;element name="zip" type="positiveInteger"/&gt;
&lt;/sequence&gt;
&lt;/extension&gt;
&lt;/complexContent&gt;
&lt;/complexType&gt;
&lt;complexType name="UKAddress"&gt;
&lt;complexContent&gt;
&lt;extension base="ipo:Address"&gt;
&lt;sequence&gt;
&lt;element name="postcode" type="ipo:UKPostcode"/&gt;
&lt;/sequence&gt;
&lt;attribute name="exportCode" type="positiveInteger" fixed="1"/&gt;
&lt;/extension&gt;
&lt;/complexContent&gt;
&lt;/complexType&gt;
&lt;!-- other Address derivations for more countries --&gt;
&lt;simpleType name="USState"&gt;
&lt;restriction base="string"&gt;
&lt;enumeration value="AK"/&gt;
&lt;enumeration value="AL"/&gt;
&lt;enumeration value="AR"/&gt;
&lt;!-- and so on ... --&gt;
&lt;enumeration value="PA"/&gt;
&lt;/restriction&gt;
&lt;/simpleType&gt;
&lt;!-- simple type definition for UKPostcode --&gt;
&lt;simpleType name="UKPostcode"&gt;
&lt;restriction base="string"&gt;
&lt;pattern value="[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][A-Z]{2}"/&gt;
&lt;/restriction&gt;
&lt;/simpleType&gt;
&lt;/schema&gt;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="strong-data" name="strong-data"></a>1.9.3
Sample Data</h4>
<p>The sample data used for the query is found in a file
named "ipo.xml":</p>
<div class="data">
<div class="exampleInner">
<pre>
&lt;?xml version="1.0"?&gt;
&lt;ipo:purchaseOrder
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ipo="http://www.example.com/IPO"
orderDate="1999-12-01"&gt;
&lt;shipTo exportCode="1" xsi:type="ipo:UKAddress"&gt;
&lt;name&gt;Helen Zoe&lt;/name&gt;
&lt;street&gt;47 Eden Street&lt;/street&gt;
&lt;city&gt;Cambridge&lt;/city&gt;
&lt;postcode&gt;CB1 1JR&lt;/postcode&gt;
&lt;/shipTo&gt;
&lt;billTo xsi:type="ipo:USAddress"&gt;
&lt;name&gt;Robert Smith&lt;/name&gt;
&lt;street&gt;8 Oak Avenue&lt;/street&gt;
&lt;city&gt;Old Town&lt;/city&gt;
&lt;state&gt;PA&lt;/state&gt;
&lt;zip&gt;95819&lt;/zip&gt;
&lt;/billTo&gt;
&lt;items&gt;
&lt;item partNum="833-AA"&gt;
&lt;productName&gt;Lapis necklace&lt;/productName&gt;
&lt;quantity&gt;1&lt;/quantity&gt;
&lt;USPrice&gt;99.95&lt;/USPrice&gt;
&lt;ipo:shipComment&gt;
Use gold wrap if possible
&lt;/ipo:shipComment&gt;
&lt;ipo:customerComment&gt;
Want this for the holidays!
&lt;/ipo:customerComment&gt;
&lt;shipDate&gt;1999-12-05&lt;/shipDate&gt;
&lt;/item&gt;
&lt;/items&gt;
&lt;/ipo:purchaseOrder&gt;
</pre>
</div>
</div>
</div>
<div class="div3">
<h4><a id="strong-queries-results" name=
"strong-queries-results"></a>1.9.4 Queries</h4>
<div class="div4">
<h5><a id="strong-queries-results-q1" name=
"strong-queries-results-q1"></a>1.9.4.1 Q1</h5>
<p>Count the invoices shipped to the United
Kingdom.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
import schema namespace ipo = "http://www.example.com/IPO" at "ipo.xsd";
count(
doc("ipo.xml")//shipTo[. instance of element(*, ipo:UKAddress)]
)
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
1
</pre>
</div>
</div>
<p>In this dataset, the data for an address does not
contain the name of the country, and the name of the
shipTo element is the same regardless of the country to
which items are shipped. Only the types allow us to
identify UK addresses - in the schema, there is one
address type for UK addresses and another for US
addresses, both derived from a common base class. In
the above query, we use the UKAddress type to identify
invoices shipped to the UK.</p>
</div>
<div class="div4">
<h5><a id="strong-queries-results-q2" name=
"strong-queries-results-q2"></a>1.9.4.2 Q2</h5>
<p>Write a function that tests an American address to
check if it has the right zip code.</p>
<p>In our solution, we will assume zip code data is
stored in a file named "zips.xml", which looks like
this.</p>
<div class="data">
<div class="exampleInner">
<pre>
&lt;zips xmlns="http://www.example.com/zips"&gt;
&lt;row&gt;
&lt;city&gt;Old Town&lt;/city&gt;
&lt;state&gt;PA&lt;/state&gt;
&lt;zip&gt;95819&lt;/zip&gt;
&lt;/row&gt;
&lt;row&gt;
&lt;city&gt;Durham&lt;/city&gt;
&lt;state&gt;NC&lt;/state&gt;
&lt;zip&gt;27701&lt;/zip&gt;
&lt;/row&gt;
&lt;row&gt;
&lt;city&gt;Durham&lt;/city&gt;
&lt;state&gt;NC&lt;/state&gt;
&lt;zip&gt;27703&lt;/zip&gt;
&lt;/row&gt;
&lt;/zips&gt;
</pre>
</div>
</div>
<p>The corresponding schema document is named
"zips.xsd":</p>
<div class="schema">
<div class="exampleInner">
<pre>
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://www.example.com/zips"
xmlns:zips="http://www.example.com/zips"&gt;
&lt;xs:element name="zips"&gt;
&lt;xs:complexType&gt;
&lt;xs:sequence&gt;
&lt;xs:element maxOccurs="unbounded" ref="zips:row"/&gt;
&lt;/xs:sequence&gt;
&lt;/xs:complexType&gt;
&lt;/xs:element&gt;
&lt;xs:element name="row"&gt;
&lt;xs:complexType&gt;
&lt;xs:sequence&gt;
&lt;xs:element ref="zips:city"/&gt;
&lt;xs:element ref="zips:state"/&gt;
&lt;xs:element ref="zips:zip"/&gt;
&lt;/xs:sequence&gt;
&lt;/xs:complexType&gt;
&lt;/xs:element&gt;
&lt;xs:element name="city" type="xs:string"/&gt;
&lt;xs:element name="state" type="xs:string"/&gt;
&lt;xs:element name="zip" type="xs:integer"/&gt;
&lt;/xs:schema&gt;
</pre>
</div>
</div>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
module namespace z="http://www.example.com/xq/zips";
import schema namespace ipo = "http://www.example.com/IPO" at "ipo.xsd";
import schema namespace zips = "http://www.example.com/zips" at "zips.xsd";
declare function z:zip-ok($a as element(*, ipo:USAddress))
as xs:boolean
{
some $i in doc("zips.xml")/zips:zips/element(zips:row)
satisfies $i/zips:city = $a/city
and $i/zips:state = $a/state
and $i/zips:zip = $a/zip
};
</pre>
</div>
</div>
<div class="note">
<p class="prefix"><b>Note:</b></p>
<p>This is not a complete query, it is a function
that is meant to be called in a query. We will use
this function in Q4.</p>
</div>
<p>An attempt to call this function with an element of
the wrong address type raises an error. For instance,
you can not call z:zip-ok() with an element of type
UKAddress.</p>
<p>Note that the parameter for this function specifies
the type rather than the element name, since it is
written to be used with any element that has the proper
address type - for instance, in our sample schema,
'billTo' and 'shipTo' are two different elements which
may both have the USAddress type.</p>
</div>
<div class="div4">
<h5><a id="strong-queries-results-q3" name=
"strong-queries-results-q3"></a>1.9.4.3 Q3</h5>
<p>Write a function that tests a UK address to see if
it has the right postal code.</p>
<p>For England, we store the information needed to test
postal codes in a file named "postals.xml", which looks
like this:</p>
<div class="data">
<div class="exampleInner">
<pre>
&lt;postals xmlns="http://www.example.com/postals"&gt;
&lt;row&gt;
&lt;city&gt;Cambridge&lt;/city&gt;
&lt;prefix&gt;CB&lt;/prefix&gt;
&lt;/row&gt;
&lt;row&gt;
&lt;city&gt;Oxford&lt;/city&gt;
&lt;prefix&gt;OX&lt;/prefix&gt;
&lt;/row&gt;
&lt;/postals&gt;
</pre>
</div>
</div>
<p>Here is the schema for the above file.</p>
<div class="schema">
<div class="exampleInner">
<pre>
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
targetNamespace="http://www.example.com/postals"
xmlns:postals="http://www.example.com/postals"&gt;
&lt;xs:element name="postals"&gt;
&lt;xs:complexType&gt;
&lt;xs:sequence&gt;
&lt;xs:element maxOccurs="unbounded" ref="postals:row"/&gt;
&lt;/xs:sequence&gt;
&lt;/xs:complexType&gt;
&lt;/xs:element&gt;
&lt;xs:element name="row"&gt;
&lt;xs:complexType&gt;
&lt;xs:sequence&gt;
&lt;xs:element ref="postals:city"/&gt;
&lt;xs:element ref="postals:prefix"/&gt;
&lt;/xs:sequence&gt;
&lt;/xs:complexType&gt;
&lt;/xs:element&gt;
&lt;xs:element name="city" type="xs:string"/&gt;
&lt;xs:element name="prefix" type="xs:string"/&gt;
&lt;/xs:schema&gt;
</pre>
</div>
</div>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
module namespace p="http://www.example.com/xq/postals";
import schema namespace ipo = "http://www.example.com/IPO" at "ipo.xsd";
import schema namespace pst = "http://www.example.com/postals" at "postals.xsd";
declare function p:postal-ok($a as element(*, ipo:UKAddress))
as xs:boolean
{
some $i in doc("postals.xml")/pst:postals/element(pst:row)
satisfies $i/pst:city = $a/city
and starts-with($a/postcode, $i/pst:prefix)
};
</pre>
</div>
</div>
<div class="note">
<p class="prefix"><b>Note:</b></p>
<p>This is not a complete query, it is a function
that is meant to be called in a query. We will use
this function in Q4.</p>
</div>
</div>
<div class="div4">
<h5><a id="strong-queries-results-q4" name=
"strong-queries-results-q4"></a>1.9.4.4 Q4</h5>
<p>Determine whether the postal code or zip code for a
purchase order is right.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
import schema namespace ipo="http://www.example.com/IPO" at "ipo.xsd";
import schema namespace pst="http://www.example.com/postals" at "postals.xsd";
import schema namespace zips="http://www.example.com/zips" at "zips.xsd";
import module namespace zok="http://www.example.com/xq/zips";
import module namespace pok="http://www.example.com/xq/postals";
declare function local:address-ok($a as element(*, ipo:Address))
as xs:boolean
{
typeswitch ($a)
case $zip as element(*, ipo:USAddress)
return zok:zip-ok($zip)
case $postal as element(*, ipo:UKAddress )
return pok:postal-ok($postal)
default return false()
};
let $shipTo := doc("ipo.xml")/element(ipo:purchaseOrder)/shipTo
let $billTo := doc("ipo.xml")/element(ipo:purchaseOrder)/billTo
return local:address-ok($shipTo) and local:address-ok($billTo)
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
true
</pre>
</div>
</div>
<p>This query calls the functions defined in Q2 and
Q3.</p>
<p>Note that the function local:address-ok() accepts
any element whose type is ipo:Address, which is the
base type for both ipo:UKAddress and ipo:USAddress.
Note also that this function uses a typeswitch to
select the appropriate function to test American or
British addresses. This can be considered a primitive
form of polymorphism.</p>
</div>
<div class="div4">
<h5><a id="strong-queries-results-q5" name=
"strong-queries-results-q5"></a>1.9.4.5 Q5</h5>
<p>Determine whether the shipping address matches the
billing address.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
import schema namespace ipo="http://www.example.com/IPO" at "ipo.xsd";
declare function local:names-match( $s as element(shipTo, ipo:Address),
$b as element(billTo, ipo:Address))
as xs:boolean
{
$s/name = $b/name
};
let $p := doc("ipo.xml")/element(ipo:purchaseOrder)
return local:names-match( $p/shipTo, $p/billTo )
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
false
</pre>
</div>
</div>
<p>In this function, note that the function specifies
both the element names and the type names for its
parameters.</p>
<p>Note also that the schema says both of these
elements are local elements, defined only within a
purchase order. The element test matches an element
with a given name even if that element is locally
declared.</p>
</div>
<div class="div4">
<h5><a id="strong-queries-results-q6" name=
"strong-queries-results-q6"></a>1.9.4.6 Q6</h5>
<p>Determine whether the invoice has a USAddress and
gives all prices as USPrices.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
import schema namespace ipo="http://www.example.com/IPO" at "ipo.xsd";
let $p := doc("ipo.xml")//element(ipo:purchaseOrder)
let $billTo := $p/billTo
let $shipTo := $p/shipTo
return
if ($billTo instance of element(*, ipo:USAddress))
then every $i in $p/items/item
satisfies (exists($i/USPrice))
else false()
</pre>
</div>
</div>
<p><em>Expected Result:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
true
</pre>
</div>
</div>
<p>Addresses are part of a type hierarchy, and the
element name, ipo:shipTo, does not tell us whether an
address is a US price or not, so we have to test the
type.</p>
<p>This example is rather contrived, since the schema
specifies that all prices are USPrice elements.
Nevertheless, it illustrates the ability to easily
combine information derived from type information with
information derived from structure.</p>
</div>
<div class="div4">
<h5><a id="strong-queries-results-q7" name=
"strong-queries-results-q7"></a>1.9.4.7 Q7</h5>
<p>Write a function that returns the text of a comment.
Call this function for each shipping comment found in
an item shipped to Helen Zoe on the date
1999-12-01.</p>
<p>Our source schema models comments with the following
substitution groups:</p>
<div class="exampleInner">
<pre>
&lt;element name="comment" type="string"/&gt;
. . .
&lt;element name="shipComment" type="string"
substitutionGroup="ipo:comment"/&gt;
&lt;element name="customerComment" type="string"
substitutionGroup="ipo:comment"/&gt;
</pre>
</div>
<p>The following sample data contains instances of
these substitution groups:</p>
<div class="exampleInner">
<pre>
&lt;items&gt;
&lt;item partNum="833-AA"&gt;
&lt;productName&gt;Lapis necklace&lt;/productName&gt;
&lt;quantity&gt;1&lt;/quantity&gt;
&lt;USPrice&gt;99.95&lt;/USPrice&gt;
&lt;ipo:shipComment&gt;
Use gold wrap if possible
&lt;/ipo:shipComment&gt;
&lt;ipo:customerComment&gt;
Want this for the holidays!
&lt;/ipo:customerComment&gt;
&lt;shipDate&gt;1999-12-05&lt;/shipDate&gt;
&lt;/item&gt;
&lt;/items&gt;
</pre>
</div>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
import schema namespace ipo="http://www.example.com/IPO" at "ipo.xsd";
declare function local:comment-text($c as schema-element(ipo:comment))
as xs:string
{
xs:string( $c )
};
for $p in doc("ipo.xml")//element(ipo:purchaseOrder),
$t in local:comment-text( $p//ipo:shipComment )
where $p/shipTo/name="Helen Zoe"
and $p/@orderDate = xs:date("1999-12-01")
return $t
</pre>
</div>
</div>
<p>In this query, the function specifies ipo:comment as
the name of the element, but any element in the
substitution group of ipo:comment may also be passed to
this function. That means that we can call the same
function for ipo:shipComment elements or
ipo:customerComment elements - for instance, the
following query also succeeds:</p>
<div class="xquery">
<div class="exampleInner">
<pre>
import schema namespace ipo="http://www.example.com/IPO" at "ipo.xsd";
declare function local:comment-text($c as schema-element(ipo:comment))
as xs:string
{
xs:string( $c )
};
for $p in doc("ipo.xml")/schema-element(ipo:purchaseOrder)
where $p/shipTo/name="Helen Zoe"
and $p/@orderDate = xs:date("1999-12-01")
return local:comment-text( $p//ipo:customerComment )
</pre>
</div>
</div>
</div>
<div class="div4">
<h5><a id="strong-queries-results-q8" name=
"strong-queries-results-q8"></a>1.9.4.8 Q8</h5>
<p>Find all comments found in an item shipped to Helen
Zoe on the date 1999-12-01, including all elements in
the substitution group for ipo:comment.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
import schema namespace ipo="http://www.example.com/IPO" at "ipo.xsd";
for $p in doc("ipo.xml")//element(ipo:purchaseOrder)
where $p/shipTo/name="Helen Zoe"
and $p/@orderDate = xs:date("1999-12-01")
return $p//schema-element(ipo:comment)
</pre>
</div>
</div>
<p>Note that <code>schema-element(ipo:comment)</code>
matches any valid element in the substitution group of
<code>ipo:comment</code>.</p>
</div>
<div class="div4">
<h5><a id="strong-queries-results-q9" name=
"strong-queries-results-q9"></a>1.9.4.9 Q9</h5>
<p>Write a function that returns all comments found on
an element, whether an item element or some other
element that may have a comment.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
import schema namespace ipo="http://www.example.com/IPO" at "ipo.xsd";
declare function local:comments-for-element( $e as element() )
as element(ipo:comment)*
{
$e/schema-element(ipo:comment)
};
for $p in doc("ipo.xml")//element(ipo:purchaseOrder)
where $p/shipTo/name="Helen Zoe"
and $p/@orderDate = xs:date("1999-12-01")
return
&lt;comments name="{$p/shipTo/name}" date="{$p/@orderDate}"&gt;
{
local:comments-for-element( $p )
}
{
for $i in $p//item
return local:comments-for-element( $i )
}
&lt;/comments&gt;
</pre>
</div>
</div>
<p>In this schema, comments can occur on either a
purchase order or on an item. In a more complete
schema, they could presumably occur in other areas as
well. This function returns all comments found on an
element, regardless of the name of the element,
illustrating the need to write functions that can
accept any element as a parameter.</p>
</div>
<div class="div4">
<h5><a id="strong-queries-results-q10" name=
"strong-queries-results-q10"></a>1.9.4.10 Q10</h5>
<p>Write a function that determines whether the person
listed in a billTo element is known to be a deadbeat,
using a US database.</p>
<div class="note">
<p class="prefix"><b>Note:</b></p>
<p>In American slang, a "deadbeat" is a person who
fails to meet a financial obligation.</p>
</div>
<p>This query assumes that "deadbeats.xml" lists the
names deadbeats in the following format:</p>
<div class="data">
<div class="exampleInner">
<pre>
&lt;?xml version="1.0"?&gt;
&lt;deadbeats&gt;
&lt;row&gt;
&lt;name&gt;Dick Dastardly&lt;/name&gt;
&lt;creditrating&gt;0&lt;/creditrating&gt;
&lt;/row&gt;
&lt;row&gt;
&lt;name&gt;Peter Doubt&lt;/name&gt;
&lt;creditrating&gt;1&lt;/creditrating&gt;
&lt;/row&gt;
&lt;row&gt;
&lt;name&gt;Robert Smith&lt;/name&gt;
&lt;creditrating&gt;0&lt;/creditrating&gt;
&lt;/row&gt;
&lt;/deadbeats&gt;
</pre>
</div>
</div>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
import schema namespace ipo="http://www.example.com/IPO" at "ipo.xsd";
declare function local:deadbeat( $b as element(billTo, ipo:USAddress) )
as xs:boolean
{
$b/name = doc("deadbeats.xml")/deadbeats/row/name
};
for $p in doc("ipo.xml")//element(ipo:purchaseOrder)
where local:deadbeat( $p/element(billTo) )
return &lt;warning&gt;{ string($p/billTo/name) } is a known deadbeat!&lt;/warning&gt;
</pre>
</div>
</div>
<p><em>Expected Output:</em></p>
<div class="exampleInner">
<pre>
&lt;warning&gt;Robert Smith is a known deadbeat!&lt;/warning&gt;
</pre>
</div>
<p>Note that this function specifies both the element
name and the type. The element name is specified
because we do not want to embarrass recipients of gifts
by calling this function for the shipping address by
mistake. The type is specified because we would need to
use a different database to identify deadbeats in other
countries.</p>
<p>Also note that the XML file in this example has no
schema. We assume that the processor omits validation
or does lax validation.</p>
</div>
<div class="div4">
<h5><a id="strong-queries-results-q11" name=
"strong-queries-results-q11"></a>1.9.4.11 Q11</h5>
<p>Write a function that computes the total price for a
sequence of item elements.</p>
<p><em>Solution in XQuery:</em></p>
<div class="xquery">
<div class="exampleInner">
<pre>
module namespace c="http://www.example.com/calc";
import schema namespace ipo="http://www.example.com/IPO" at "ipo.xsd";
declare function c:total-price( $i as element(item)* )
as xs:decimal
{
let $subtotals := for $s in $i return $s/quantity * $s/USPrice
return sum( $subtotals )
};
</pre>
</div>
</div>
<p>Here is a query that calls the function we just
defined to get the total for an invoice (before
calculating taxes and shipping charges):</p>
<div class="xquery">
<div class="exampleInner">
<pre>
import schema namespace ipo="http://www.example.com/IPO" at "ipo.xsd";
import module namespace calc = "http://www.example.com/calc";
for $p in doc("ipo.xml")//element(ipo:purchaseOrder)
where $p/shipTo/name="Helen Zoe"
and $p/@orderDate = xs:date("1999-12-01")
return calc:total-price($p//item)
</pre>
</div>
</div>
<p>This query illustrates the need to be able to pass a
sequence as a parameter to a function.</p>
<p>If the input document contains more than one
purchase order for the given date and person, a total
will be computed for all purchase orders.</p>
</div>
<div class="div4">
<h5><a id="strong-queries-results-q12" name=
"strong-queries-results-q12"></a>1.9.4.12 Q12</h5>
<p>In <a href="#xmlschema-0">[XMLSchema0]</a>, a
Quarterly Report is created, summarizing the types of
products that have been billed on a per region basis.
It creates the following sample report:</p>
<div class="xquery">
<div class="exampleInner">
<pre>
&lt;purchaseReport
xmlns="http://www.example.com/Report"
period="P3M" periodEnding="1999-12-31"&gt;
&lt;regions&gt;
&lt;zip code="95819"&gt;
&lt;part number="872-AA" quantity="1"/&gt;
&lt;part number="926-AA" quantity="1"/&gt;
&lt;part number="833-AA" quantity="1"/&gt;
&lt;part number="455-BX" quantity="1"/&gt;
&lt;/zip&gt;
&lt;zip code="63143"&gt;
&lt;part number="455-BX" quantity="4"/&gt;
&lt;/zip&gt;
&lt;/regions&gt;
&lt;parts&gt;
&lt;part number="872-AA"&gt;Lawnmower&lt;/part&gt;
&lt;part number="926-AA"&gt;Baby Monitor&lt;/part&gt;
&lt;part number="833-AA"&gt;Lapis Necklace&lt;/part&gt;
&lt;part number="455-BX"&gt;Sturdy Shelves&lt;/part&gt;
&lt;/parts&gt;
&lt;/purchaseReport&gt;
</pre>
</div>
</div>
<p>This is the schema given for the above report:</p>
<div class="schema">
<div class="exampleInner">
<pre>
&lt;schema targetNamespace="http://www.example.com/Report"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:r="http://www.example.com/Report"
xmlns:xipo="http://www.example.com/IPO"
elementFormDefault="qualified"&gt;
&lt;!-- for SKU --&gt;
&lt;import namespace="http://www.example.com/IPO"/&gt;
&lt;annotation&gt;
&lt;documentation xml:lang="en"&gt;
Report schema for Example.com
Copyright 2000 Example.com. All rights reserved.
&lt;/documentation&gt;
&lt;/annotation&gt;
&lt;element name="purchaseReport"&gt;
&lt;complexType&gt;
&lt;sequence&gt;
&lt;element name="regions" type="r:RegionsType"&gt;
&lt;keyref name="dummy2" refer="r:pNumKey"&gt;
&lt;selector xpath="r:zip/r:part"/&gt;
&lt;field xpath="@number"/&gt;
&lt;/keyref&gt;
&lt;/element&gt;
&lt;element name="parts" type="r:PartsType"/&gt;
&lt;/sequence&gt;
&lt;attribute name="period" type="duration"/&gt;
&lt;attribute name="periodEnding" type="date"/&gt;
&lt;/complexType&gt;
&lt;unique name="dummy1"&gt;
&lt;selector xpath="r:regions/r:zip"/&gt;
&lt;field xpath="@code"/&gt;
&lt;/unique&gt;
&lt;key name="pNumKey"&gt;
&lt;selector xpath="r:parts/r:part"/&gt;
&lt;field xpath="@number"/&gt;
&lt;/key&gt;
&lt;/element&gt;
&lt;complexType name="RegionsType"&gt;
&lt;sequence&gt;
&lt;element name="zip" maxOccurs="unbounded"&gt;
&lt;complexType&gt;
&lt;sequence&gt;
&lt;element name="part" maxOccurs="unbounded"&gt;
&lt;complexType&gt;
&lt;complexContent&gt;
&lt;restriction base="anyType"&gt;
&lt;attribute name="number" type="xipo:SKU"/&gt;
&lt;attribute name="quantity" type="positiveInteger"/&gt;
&lt;/restriction&gt;
&lt;/complexContent&gt;
&lt;/complexType&gt;
&lt;/element&gt;
&lt;/sequence&gt;
&lt;attribute name="code" type="positiveInteger"/&gt;
&lt;/complexType&gt;
&lt;/element&gt;
&lt;/sequence&gt;
&lt;/complexType&gt;
&lt;complexType name="PartsType"&gt;
&lt;sequence&gt;
&lt;element name="part" maxOccurs="unbounded"&gt;
&lt;complexType&gt;
&lt;simpleContent&gt;
&lt;extension base="string"&gt;
&lt;attribute name="number" type="xipo:SKU"/&gt;
&lt;/extension&gt;
&lt;/simpleContent&gt;
&lt;/complexType&gt;
&lt;/element&gt;
&lt;/sequence&gt;
&lt;/complexType&gt;
&lt;/schema&gt;
</pre>
</div>
</div>
<p>This report, which lists products sold by zip code,
is based on the same international purchase report used
in previous queries.</p>
<p>Here is a query that generates the desired report
from a collection that contains US purchase orders:</p>
<div class="xquery">
<div class="exampleInner">
<pre>
declare namespace rpt="http://www.example.com/Report";
let $orders := doc('ipo.xml')/schema-element(ipo:purchaseOrder)
[@orderDate ge xs:date("1999-09-01")
and @orderDate le xs:date("1999-12-31")]
let $items := $orders/items/item
let $zips := distinct-values($orders/billTo/zip)
let $parts := distinct-values($items/@partNum)
return
&lt;rpt:purchaseReport&gt;
&lt;rpt:regions&gt;
{
for $zip in $zips
order by $zip
return
&lt;rpt:zip code="{$zip}"&gt;
{
for $part in $parts
let $hits := $orders[ billTo/zip = $zip and items/item/@partNum = $part]
let $quantity := sum($hits//item[@partNum=$part]/quantity)
where count($hits) &gt; 0
order by $part
return
&lt;rpt:part number="{$part}" quantity="{$quantity}"/&gt;
}
&lt;/rpt:zip&gt;
}
&lt;/rpt:regions&gt;
&lt;rpt:parts&gt;
{
for $part in $parts
return
&lt;rpt:part number="{$part}"&gt;
{
string($items[@partNum = $part]/productName)
}
&lt;/rpt:part&gt;
}
&lt;/rpt:parts&gt;
&lt;/rpt:purchaseReport&gt;
</pre>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="back">
<div class="div1">
<h2><a id="acknowledgements" name="acknowledgements"></a>A
Acknowledgements</h2>
<p>The editors thank the members of the XML Query Working
Group, which produced the material in this document.</p>
<p>The use cases in this paper were contributed by the
following individuals:</p>
<table border="1">
<tbody>
<tr>
<td>Use Case "R"</td>
<td>Don Chamberlin</td>
</tr>
<tr>
<td>Use Case "XMP"</td>
<td>Mary Fernandez, Jerome Simeon, Phil Wadler</td>
</tr>
<tr>
<td>Use Case "TREE"</td>
<td>Jonathan Robie</td>
</tr>
<tr>
<td>Use Case "PARTS"</td>
<td>Michael Rys</td>
</tr>
<tr>
<td>Use Case "NS"</td>
<td>Ingo Macherius</td>
</tr>
<tr>
<td>Use Case "STRING"</td>
<td>Umit Yalcinalp</td>
</tr>
<tr>
<td>Use Case "SEQ"</td>
<td>Jonathan Robie</td>
</tr>
<tr>
<td>Use Case "SGML"</td>
<td>Paula Angerstein</td>
</tr>
<tr>
<td>Use Case "STRONG"</td>
<td>Jonathan Robie and Phil Wadler. Schemas and data
taken from <a href=
"#xmlschema-0">[XMLSchema0]</a>.</td>
</tr>
</tbody>
</table>
<p>Use case "XMP" has been previously published in <a href=
"#Fernandez99">[Fernandez]</a>. Use cases "Tree" and "Seq"
have been previously published in <a href=
"#Robie99">[Robie99]</a>.</p>
<p>The editors also wish to thank the members of the other
W3C Working Groups who have commented on earlier drafts, and
Michael Dyck and for his critical reading and helpful
suggestions. Michael Wenger found several bugs, and also
found more elegant solutions to some of the use cases, which
are now included in this document.</p>
</div>
<div class="div1">
<h2><a id="ChangeLog" name="ChangeLog"></a>B Change Log
(Non-Normative)</h2>
<div class="div2">
<h3><a id="id-2006-05-08" name="id-2006-05-08"></a>B.1 8
May 2006</h3>
<ul>
<li>
<p>Updated status section.</p>
</li>
<li>
<p>Added note on static typing.</p>
</li>
</ul>
</div>
<div class="div2">
<h3><a id="id-2005-08-31" name="id-2005-08-31"></a>B.2 31
Aug 2005</h3>
<ul>
<li>
<p>Comments should now be directed to Bugzilla.</p>
</li>
<li>
<p>Massimo and Dana have each changed their
affiliation.</p>
</li>
<li>
<p>IPR statement has been changed to reflect that this
will progress to a Note rather than a
Recommendation.</p>
</li>
<li>
<p>Provided link to changes to Use Case Strong errors
in Mozilla.</p>
</li>
<li>
<p>Minor editorial changes.</p>
</li>
</ul>
</div>
<div class="div2">
<h3><a id="id-2005-07-11" name="id-2005-07-11"></a>B.3 11
July 2005</h3>
<p>Aligned with current XQuery Working Draft.</p>
<p>Fixed the following bugs.</p>
<ol class="enumar">
<li>
<p>rdb-queries-results-q9 now uses month-from-date()
and year-from-date(). The "get-" prefix, now obsolete,
has been removed. Fixes <a href=
"http://www.w3.org/Member/bugzilla/show_bug.cgi?id=119">
Bug 119</a>.</p>
</li>
<li>
<p>Removed whitespace from the price element in the
source document for Use Case "XMP" - it was
&lt;price&gt; 65.95&lt;/price&gt;, and is now
&lt;price&gt; 65.95&lt;/price&gt;. Fixes <a href=
"http://www.w3.org/Member/bugzilla/show_bug.cgi?id=121">
Bug 121</a>.</p>
</li>
<li>
<p>Removed trailing whitespace from the &lt;remark/&gt;
elements used in "auction.xml". Fixes bug <a href=
"http://www.w3.org/Member/bugzilla/show_bug.cgi?id=124">
Bug 124</a>.</p>
</li>
<li>
<p>Need to discuss <a href=
"http://www.w3.org/Member/bugzilla/show_bug.cgi?id=148">
Bug 148</a>. Did not see the problems with Q1 and Q2 in
the document. Have some questions about the best
treatment of Q4 and Q5.</p>
</li>
<li>
<p>Fixed many errors in Use Case "Strong" as proposed
<a href=
"http://www.w3.org/Bugs/Public/show_bug.cgi?id=1202">here</a>.</p>
</li>
</ol>
</div>
<div class="div2">
<h3><a id="id-2005-04-04" name="id-2005-04-04"></a>B.4 04
April 2005</h3>
<p>Alignment with 04 April 2005 Working Draft of XQuery.
This actually did not change the results of any of the
queries, so it was just a matter of tweaking the front
matter.</p>
</div>
<div class="div2">
<h3><a id="id-2005-01-30" name="id-2005-01-30"></a>B.5 30
Jan 2005</h3>
<p>Many changes have been made since the last release,
particularly:</p>
<ul>
<li>
<p>Moved to the 29 October XQuery Working Drafts.</p>
</li>
<li>
<p>Corrected numerous NS queries to reflect current
handling of namespaces.</p>
</li>
<li>
<p>Changed use of locally declared elements and
attributes in SequenceType to match current XQuery
language.</p>
</li>
<li>
<p>Fixed reported bugs, ran most queries using two
implementations.</p>
</li>
</ul>
</div>
</div>
<div class="div1">
<h2><a id="references" name="references"></a>C References
(Non-Normative)</h2>
<p>The following references are some of the works considered
by the WG in deriving its use cases.</p>
<dl>
<dt class="label"><span><a id="Cotton98" name=
"Cotton98"></a>Cotton98</span></dt>
<dd>
<div>
<a href=
"http://www.w3.org/TandS/QL/QL98/pp/queryreq.html"><cite>
Candidate Requirements for XML Query</cite></a>, Paul
Cotton and Ashok Malhotra, 1998. In Query Languages 98
(QL'98).
</div>
</dd>
<dt class="label"><span><a id="DOM" name=
"DOM"></a>DOM</span></dt>
<dd>
<div>
<a href=
"http://www.w3.org/TR/1999/CR-DOM-Level-2-19991210/"><cite>
Document Object Model (DOM), Level 2
Specification</cite></a>. W3C Candidate Recommendation.
</div>
</dd>
<dt class="label"><span><a id="xpath-functions" name=
"xpath-functions"></a>FandO</span></dt>
<dd>
<div>
<a href=
"http://www.w3.org/TR/xpath-functions/"><cite>XQuery
1.0 and XPath 2.0 Functions and Operators</cite></a>,
Ashok Malhotra, Jim Melton, and Norman Walsh, Editors.
World Wide Web Consortium, 23 Jan 2007. This version is
http://www.w3.org/TR/2007/REC-xpath-functions-20070123/.
The <a href=
"http://www.w3.org/TR/xpath-functions/">latest
version</a> is available at
http://www.w3.org/TR/xpath-functions/.
</div>
</dd>
<dt class="label"><span><a id="Fernandez99" name=
"Fernandez99"></a>Fernandez</span></dt>
<dd>
<div>
<a href=
"http://www.w3.org/1999/09/ql/docs/xquery.html"><cite>XML
Query Languages: Experiences and Exemplars</cite></a>,
Mary Fernandez, Jerome Simeon, Philip Wadler, 1999.
</div>
</dd>
<dt class="label"><span><a id="xml-infoset" name=
"xml-infoset"></a>Infoset</span></dt>
<dd>
<div>
<a href="http://www.w3.org/TR/xml-infoset"><cite>XML
Information Set (Second Edition)</cite></a>, John Cowan
and Richard Tobin, Editors. World Wide Web Consortium,
04&#160;Feb&#160;2004. This version is
http://www.w3.org/TR/2004/REC-xml-infoset-20040204. The
<a href="http://www.w3.org/TR/xml-infoset">latest
version</a> is available at
http://www.w3.org/TR/xml-infoset.
</div>
</dd>
<dt class="label"><span><a id="Maier98" name=
"Maier98"></a>Maier98</span></dt>
<dd>
<div>
<a href=
"http://www.w3.org/TandS/QL/QL98/pp/maier.html"><cite>Database
Desiderata for an XML Query Language</cite></a>, David
Maier, 1998. In Query Languages 98 (QL'98).
</div>
</dd>
<dt class="label"><span><a id="REC-xml-names" name=
"REC-xml-names"></a>Namespaces</span></dt>
<dd>
<div>
<a href=
"http://www.w3.org/TR/REC-xml-names"><cite>Namespaces
in XML</cite></a>, Andrew Layman, Dave Hollander, and
Tim Bray, Editors. World Wide Web Consortium,
14&#160;Jan&#160;1999. This version is
http://www.w3.org/TR/1999/REC-xml-names-19990114. The
<a href="http://www.w3.org/TR/REC-xml-names">latest
version</a> is available at
http://www.w3.org/TR/REC-xml-names.
</div>
</dd>
<dt class="label"><span><a id="Robie99" name=
"Robie99"></a>Robie99</span></dt>
<dd>
<div>
<a href=
"http://www.w3.org/XML/Group/1999/10/xquery-tree.html"><cite>
The Tree Structure of XML Queries</cite></a>, Jonathan
Robie.
</div>
</dd>
<dt class="label"><span><a id="REC-xml" name=
"REC-xml"></a>XML</span></dt>
<dd>
<div>
<a href="http://www.w3.org/TR/xml"><cite>Extensible
Markup Language (XML) 1.0 (Fourth Edition)</cite></a>,
Eve Maler, Jean Paoli, François Yergeau, <em>et.
al.</em>, Editors. World Wide Web Consortium,
16&#160;Aug&#160;2006. This version is
http://www.w3.org/TR/2006/REC-xml-20060816. The
<a href="http://www.w3.org/TR/xml">latest version</a>
is available at http://www.w3.org/TR/xml.
</div>
</dd>
<dt class="label"><span><a id="xmlschema-0" name=
"xmlschema-0"></a>XMLSchema0</span></dt>
<dd>
<div>
<a href="http://www.w3.org/TR/xmlschema-0/"><cite>XML
Schema Part 0: Primer Second Edition</cite></a>, David
C. Fallside and Priscilla Walmsley, Editors. World Wide
Web Consortium, 28&#160;Oct&#160;2004. This version is
http://www.w3.org/TR/2004/REC-xmlschema-0-20041028/.
The <a href="http://www.w3.org/TR/xmlschema-0/">latest
version</a> is available at
http://www.w3.org/TR/xmlschema-0/.
</div>
</dd>
<dt class="label"><span><a id="xmlschema-1" name=
"xmlschema-1"></a>XMLSchema1</span></dt>
<dd>
<div>
<a href="http://www.w3.org/TR/xmlschema-1/"><cite>XML
Schema Part 1: Structures Second Edition</cite></a>,
Henry S. Thompson, David Beech, Noah Mendelsohn, and
Murray Maloney, Editors. World Wide Web Consortium,
28&#160;Oct&#160;2004. This version is
http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/.
The <a href="http://www.w3.org/TR/xmlschema-1/">latest
version</a> is available at
http://www.w3.org/TR/xmlschema-1/.
</div>
</dd>
<dt class="label"><span><a id="xmlschema-2" name=
"xmlschema-2"></a>XMLSchema2</span></dt>
<dd>
<div>
<a href="http://www.w3.org/TR/xmlschema-2/"><cite>XML
Schema Part 2: Datatypes Second Edition</cite></a>,
Paul V. Biron and Ashok Malhotra, Editors. World Wide
Web Consortium, 28&#160;Oct&#160;2004. This version is
http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/.
The <a href="http://www.w3.org/TR/xmlschema-2/">latest
version</a> is available at
http://www.w3.org/TR/xmlschema-2/.
</div>
</dd>
<dt class="label"><span><a id="xpath20" name=
"xpath20"></a>XPath</span></dt>
<dd>
<div>
<a href="http://www.w3.org/TR/xpath20/"><cite>XML Path
Language (XPath) 2.0</cite></a>, Don Chamberlin ,
Anders Berglund, Scott Boag, <em>et. al.</em>, Editors.
World Wide Web Consortium, 23 Jan 2007. This version is
http://www.w3.org/TR/2007/REC-xpath20-20070123/. The
<a href="http://www.w3.org/TR/xpath20/">latest
version</a> is available at
http://www.w3.org/TR/xpath20/.
</div>
</dd>
<dt class="label"><span><a id="xquery" name=
"xquery"></a>XQuery</span></dt>
<dd>
<div>
<a href="http://www.w3.org/TR/xquery/"><cite>XQuery
1.0: An XML Query Language</cite></a>, Don Chamberlin ,
Anders Berglund, Scott Boag, <em>et. al.</em>, Editors.
World Wide Web Consortium, 23 Jan 2007. This version is
http://www.w3.org/TR/2007/REC-xquery-20070123/. The
<a href="http://www.w3.org/TR/xquery/">latest
version</a> is available at
http://www.w3.org/TR/xquery/.
</div>
</dd>
<dt class="label"><span><a id="xquery-requirements" name=
"xquery-requirements"></a>XQuery Requirements</span></dt>
<dd>
<div>
<a href=
"http://www.w3.org/TR/xquery-requirements/"><cite>XML
Query (XQuery) Requirements</cite></a>, Don Chamberlin,
Peter Fankhauser, Massimo Marchiori, and Jonathan
Robie, Editors. World Wide Web Consortium,
3&#160;Jun&#160;2005. This version is
http://www.w3.org/TR/2005/WD-xquery-requirements-20050603/.
The <a href=
"http://www.w3.org/TR/xquery-requirements">latest
version</a> is available at
http://www.w3.org/TR/xquery-requirements/.
</div>
</dd>
<dt class="label"><span><a id="xslt" name=
"xslt"></a>XSLT</span></dt>
<dd>
<div>
<a href="http://www.w3.org/TR/xslt"><cite>XSL
Transformations (XSLT) Version 1.0</cite></a>, James
Clark, Editor. World Wide Web Consortium,
16&#160;Nov&#160;1999. This version is
http://www.w3.org/TR/1999/REC-xslt-19991116. The
<a href="http://www.w3.org/TR/xslt">latest version</a>
is available at http://www.w3.org/TR/xslt.
</div>
</dd>
<dt class="label"><span><a id="UseCaseQueries" name=
"UseCaseQueries"></a>Use Case Sample Queries</span></dt>
<dd>
<div>
<a href=
"http://www.w3.org/TR/2007/NOTE-xquery-use-cases-20070323/xquery-use-case-queries.txt">
<cite>Queries from this document</cite></a>, presented
in a single file
</div>
</dd>
<dt class="label"><span><a id="XQueryQueries" name=
"XQueryQueries"></a>XQuery Sample Queries</span></dt>
<dd>
<div>
<a href=
"http://www.w3.org/TR/2007/NOTE-xquery-use-cases-20070323/xquery-wd-queries.txt">
<cite>Queries from the XQuery document</cite></a>,
presented in a single file.
</div>
</dd>
</dl>
</div>
</div>
</body>
</html>