Abstract

This document specifies usage scenarios for XQuery.

Status of this Document

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 W3C technical reports index at http://www.w3.org/TR/.

This version of the Use Cases document corresponds to the XQuery Working Draft released on 4 April 2005. The queries in this document have been parsed using a parser generated from the same grammar used to create the documentation for the XQuery Working Draft. See B Change Log for more information on changes.

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

Appendices

1 Use Cases for XML Queries

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 W3C XML Query Requirements document, without solutions in concrete query languages. Now it is being republished with solutions for XQuery.

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:

doc("http://bstore1.example.com/bib.xml")//author

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:

$b//author

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:

//author

Previous versions of this document accessed implicit documents using the input() function, which no longer exists. The input() 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 input() have been rewritten to use explicit file names.

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 [Use Case Sample Queries]. Also, the queries from the XQuery specification itself have been made available in [XQuery Sample Queries].

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.

These use cases represent a snapshot of an ongoing work. Some important application areas are not yet adequately covered by a use case. The XML Query Working Group reserves the right to add, delete, or modify individual queries or whole use cases as the work progresses. The presence of a query in this set of use cases does not necessarily indicate that the query will be expressible in the XML Query Language(s) to be created by the XML Query Working Group.

1.1 Use Case "XMP": Experiences and Exemplars

This use case contains several example queries that illustrate requirements gathered from the database and document communities.

1.1.1 Document Type Definitions (DTD)

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:

1.1.9.8 Q8

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.

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.

1.2 Use Case "TREE": Queries that preserve hierarchy

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.

1.2.1 Description

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.

1.2.2 Document Type Definition (DTD)

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":

1.2.4.4 Q4

1.2.4.5 Q5

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.

1.2.4.6 Q6

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.

1.3 Use Case "SEQ" - Queries based on Sequence

This use case illustrates queries based on the sequence in which elements appear in a document.

1.3.1 Description

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.

1.3.2 Document Type Definition (DTD)

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.

1.3.3 Sample Data

The queries in this use case are based on the following sample data.

<report>
<section>
<section.title>Procedure</section.title>
<section.content>
The patient was taken to the operating room where she was placed
in supine position and
<anesthesia>induced under general anesthesia.</anesthesia>
<prep>
<action>A Foley catheter was placed to decompress the bladder</action>
and the abdomen was then prepped and draped in sterile fashion.
</prep>
<incision>
A curvilinear incision was made
<geography>in the midline immediately infraumbilical</geography>
and the subcutaneous tissue was divided
<instrument>using electrocautery.</instrument>
</incision>
The fascia was identified and
<action>#2 0 Maxon stay sutures were placed on each side of the midline.
</action>
<incision>
The fascia was divided using
<instrument>electrocautery</instrument>
and the peritoneum was entered.
</incision>
<observation>The small bowel was identified.</observation>
and
<action>
the
<instrument>Hasson trocar</instrument>
was placed under direct visualization.
</action>
<action>
The
<instrument>trocar</instrument>
was secured to the fascia using the stay sutures.
</action>
</section.content>
</section>
</report>

1.3.4 Queries and Results

1.3.4.1 Q1

In the Procedure section of Report1, what Instruments were used in the second Incision?

<critical_sequence>
The fascia was identified and
<action>#2 0 Maxon stay sutures were placed on each side of the midline.
</action>#2 0 Maxon stay sutures were placed on each side of the midline.
</critical_sequence>

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:

$c except $c//node()

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:

<critical_sequence>
The fascia was identified and
<action>#2 0 Maxon stay sutures were placed on each side of the midline.
</action>
</critical_sequence>

1.4 Use Case "R" - Access to Relational Data

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.

1.4.1 Description

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.

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.

The three tables used by the online auction are below, with their column-names indicated in parentheses.

1.4.2 Document Type Definition (DTD)

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:

The above query does several joins, and requires the results in a particular order. If there were no order by clause, results would be reported in document order. If you do not care about the order, you can use the unordered 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.

1.4.4.16 Q16

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).

1.5 Use Case "SGML": Standard Generalized Markup Language

1.5.1 Description

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.

1.5.3 Sample Data

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.

0: <!DOCTYPE report SYSTEM "report.dtd">
1: <report>
2: <title>Getting started with SGML</title>
3: <chapter>
4: <title>The business challenge</title>
5: <intro>
6: <para>With the ever-changing and growing global market, companies and
7: large organizations are searching for ways to become more viable and
8: competitive. Downsizing and other cost-cutting measures demand more
9: efficient use of corporate resources. One very important resource is
10: an organization's information.</para>
11: <para>As part of the move toward integrated information management,
12: whole industries are developing and implementing standards for
13: exchanging technical information. This report describes how one such
14: standard, the Standard Generalized Markup Language (SGML), works as
15: part of an overall information management strategy.</para>
16: <graphic graphname="infoflow"/></intro></chapter>
17: <chapter>
18: <title>Getting to know SGML</title>
19: <intro>
20: <para>While SGML is a fairly recent technology, the use of
21: <emph>markup</emph> in computer-generated documents has existed for a
22: while.</para></intro>
23: <section shorttitle="What is markup?">
24: <title>What is markup, or everything you always wanted to know about
25: document preparation but were afraid to ask?</title>
26: <intro>
27: <para>Markup is everything in a document that is not content. The
28: traditional meaning of markup is the manual <emph>marking</emph> up
29: of typewritten text to give instructions for a typesetter or
30: compositor about how to fit the text on a page and what typefaces to
31: use. This kind of markup is known as <emph>procedural markup</emph>.</para></intro>
32: <topic topicid="top1">
33: <title>Procedural markup</title>
34: <para>Most electronic publishing systems today use some form of
35: procedural markup. Procedural markup codes are good for one
36: presentation of the information.</para></topic>
37: <topic topicid="top2">
38: <title>Generic markup</title>
39: <para>Generic markup (also known as descriptive markup) describes the
40: <emph>purpose</emph> of the text in a document. A basic concept of
41: generic markup is that the content of a document must be separate from
42: the style. Generic markup allows for multiple presentations of the
43: information.</para></topic>
44: <topic topicid="top3">
45: <title>Drawbacks of procedural markup</title>
46: <para>Industries involved in technical documentation increasingly
47: prefer generic over procedural markup schemes. When a company changes
48: software or hardware systems, enormous data translation tasks arise,
49: often resulting in errors.</para></topic></section>
50: <section shorttitle="What is SGML?">
51: <title>What <emph>is</emph> SGML in the grand scheme of the universe, anyway?</title>
52: <intro>
53: <para>SGML defines a strict markup scheme with a syntax for defining
54: document data elements and an overall framework for marking up
55: documents.</para>
56: <para>SGML can describe and create documents that are not dependent on
57: any hardware, software, formatter, or operating system. Since SGML documents
58: conform to an international standard, they are portable.</para></intro></section>
59: <section shorttitle="How does SGML work?">
60: <title>How is SGML and would you recommend it to your grandmother?</title>
61: <intro>
62: <para>You can break a typical document into three layers: structure,
63: content, and style. SGML works by separating these three aspects and
64: deals mainly with the relationship between structure and content.</para></intro>
65: <topic topicid="top4">
66: <title>Structure</title>
67: <para>At the heart of an SGML application is a file called the DTD, or
68: Document Type Definition. The DTD sets up the structure of a document,
69: much like a database schema describes the types of information it
70: handles.</para>
71: <para>A database schema also defines the relationships between the
72: various types of data. Similarly, a DTD specifies <emph>rules</emph>
73: to help ensure documents have a consistent, logical structure.</para></topic>
74: <topic topicid="top5">
75: <title>Content</title>
76: <para>Content is the information itself. The method for identifying
77: the information and its meaning within this framework is called
78: <emph>tagging</emph>. Tagging must
79: conform to the rules established in the DTD (see <xref xrefid="top4"/>).</para>
80: <graphic graphname="tagexamp"/></topic>
81: <topic topicid="top6">
82: <title>Style</title>
83: <para>SGML does not standardize style or other processing methods for
84: information stored in SGML.</para></topic></section></chapter>
85: <chapter>
86: <title>Resources</title>
87: <section>
88: <title>Conferences, tutorials, and training</title>
89: <intro>
90: <para>The Graphic Communications Association has been
91: instrumental in the development of SGML. GCA provides conferences,
92: tutorials, newsletters, and publication sales for both members and
93: non-members.</para>
94: <para security="c">Exiled members of the former Soviet Union's secret
95: police, the KGB, have infiltrated the upper ranks of the GCA and are
96: planning the Final Revolution as soon as DSSSL is completed.</para>
97: </intro>
98: </section>
99: </chapter>
100:</report>

1.5.4 Queries and Results

1.5.4.1 Q1

Locate all paragraphs in the report (all "para" elements occurring anywhere within the "report" element).

1.5.4.3 Q3

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).

1.5.4.4 Q4

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").

1.5.4.7 Q7

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).

1.5.4.10 Q9

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).

1.5.4.11 Q10

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).

1.6 Use Case "STRING": String Search

1.6.1 Description

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.

In this use case, the contains 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.

1.6.3 Sample Data

The queries in this use case are based on the following input data, which is found in the file "string.xml".

<?xml version="1.0" encoding="ISO-8859-1"?>
<news>
<news_item>
<title> Gorilla Corporation acquires YouNameItWeIntegrateIt.com </title>
<content>
<par> Today, Gorilla Corporation announced that it will purchase
YouNameItWeIntegrateIt.com. The shares of
YouNameItWeIntegrateIt.com dropped $3.00 as a result of this
announcement.
</par>
<par> As a result of this acquisition, the CEO of
YouNameItWeIntegrateIt.com Bill Smarts resigned. He did not
announce what he will do next. Sources close to
YouNameItWeIntegrateIt.com hint that Bill Smarts might be
taking a position in Foobar Corporation.
</par>
<par>YouNameItWeIntegrateIt.com is a leading systems integrator
that enables <quote>brick and mortar</quote> companies to
have a presence on the web.
</par>
</content>
<date>1-20-2000</date>
<author>Mark Davis</author>
<news_agent>News Online</news_agent>
</news_item>
<news_item>
<title>Foobar Corporation releases its new line of Foo products
today</title>
<content>
<par> Foobar Corporation releases the 20.9 version of its Foo
products. The new version of Foo products solve known
performance problems which existed in 20.8 line and
increases the speed of Foo based products tenfold. It also
allows wireless clients to be connected to the Foobar
servers.
</par>
<par> The President of Foobar Corporation announced that they
were proud to release 20.9 version of Foo products and
they will upgrade existing customers <footnote>where
service agreements exist</footnote>
promptly. TheAppCompany Inc. immediately announced that it
will release the new version of its products to utilize
the 20.9 architecture within the next three months.
</par>
<figure>
<title>Presidents of Foobar Corporation and TheAppCompany
Inc. Shake Hands</title> <image source="handshake.jpg"/>
</figure>
</content>
<date>1-20-2000</date>
<news_agent>Foobar Corporation</news_agent>
</news_item>
<news_item> <title>Foobar Corporation is suing Gorilla Corporation for
patent infringement </title>
<content>
<par> In surprising developments today, Foobar Corporation
announced that it is suing Gorilla Corporation for patent
infringement. The patents that were mentioned as part of the
lawsuit are considered to be the basis of Foobar
Corporation's <quote>Wireless Foo</quote> line of products.
</par>
<par>The tension between Foobar and Gorilla Corporations has
been increasing ever since the Gorilla Corporation acquired
more than 40 engineers who have left Foobar Corporation,
TheAppCompany Inc. and YouNameItWeIntegrateIt.com over the
past 3 months. The engineers who have left the Foobar
corporation and its partners were rumored to be working on
the next generation of server products and applications which
will directly compete with Foobar's Foo 20.9 servers. Most of
the engineers have relocated to Hawaii where the Gorilla
Corporation's server development is located.
</par>
</content>
<date>1-20-2000</date>
<news_agent>Reliable News Corporation</news_agent>
</news_item>
</news>

In addition, the following data, listing the partners and competitors of companies, is found in the file "company-data.xml".

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE company SYSTEM "company.dtd">
<company>
<name>Foobar Corporation</name>
<ticker_symbol>FOO</ticker_symbol>
<description>Foobar Corporation is a maker of Foo(TM) and
Foobar(TM) products and a leading software company with a 300
Billion dollar revenue in 1999. It is located in Alaska.
</description>
<business_code>Software</business_code>
<partners>
<partner>YouNameItWeIntegrateIt.com</partner>
<partner>TheAppCompany Inc.</partner>
</partners>
<competitors>
<competitor>Gorilla Corporation</competitor>
</competitors>
</company>

1.6.4 Queries and Results

1.6.4.1 Q1

Find the titles of all news items where the string "Foobar Corporation" appears in the title.

Solution in XQuery:

doc("string.xml")//news_item/title[contains(., "Foobar Corporation")]

Expected Results

<title>Foobar Corporation releases its new line of Foo
products today</title>
<title>Foobar Corporation is suing Gorilla Corporation for patent
infringement </title>

1.6.4.2 Q2

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.

1.6.4.5 Q5

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.

<item_summary>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.</item_summary>
<item_summary>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.</item_summary>

1.7 Use Case "NS" - Queries Using Namespaces

This use case performs a variety of queries on namespace-qualified names.

1.7.1 Description

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:

Syntactic disambiguation when combining XML data from different sources

Re-use of predefined modules, such as XLinks or XML Schema

Support for global classification schemas, such as the Dublin Core

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.

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.

1.7.2 Document Type Definition (DTD)

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.

1.8 Use Case "PARTS" - Recursive Parts Explosion

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.

1.8.1 Description

This use case is based on a "parts explosion" database that contains information about how parts are used in other parts.

The input to the use case is a "flat" document in which each different part is represented by a <part> 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.

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.

1.8.2 Document Type Definitions (DTD)

Although the partid and partof 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 partof attribute matches exactly one partid. Parts having no partof attribute are not contained in any other part.

1.8.4 Queries and Results

1.8.4.1 Q1

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 <part> 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.

1.9 Use Case "STRONG" - queries that exploit strongly typed data

1.9.1 Description

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 [XMLSchema0].

Note:

We intend to provide further use cases for our type system in future versions of this document. These use cases will illustrate other aspects of the type system, such as static type checking.

1.9.2 Schema

The schema for this example is the International Purchase Order schema taken from the XML Schema Primer, which imports a schema for addresses. Here is the main schema:

1.9.4.1 Q1

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.

1.9.4.2 Q2

Write a function that tests an American address to check if it has the right zip code.

In our solution, we will assume zip code data is stored in a file called "zips.xml", which looks like this.

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.

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.

1.9.4.3 Q3

Write a function that tests a UK address to see if it has the right postal code.

For England, we store the information needed to test postal codes in a file called "postals.xml", which looks like this:

Note that the function 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.

1.9.4.5 Q5

Write a query that returns purchase orders where the name in the shipping address does not match the name in the billing address.

In this function, note that the function uses the element names rather than the type names to specify the parameters. The query should return an error if the function is called with two ipo:billTo elements by mistake, but it is not a mistake to have the shipping address and billing address have different types.

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.

1.9.4.6 Q6

Find purchase orders where the billing address is not a USAddress, but at least one price is a USPrice.

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.

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.

1.9.4.7 Q7

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.

The example in the XML Schema Primer uses the following example for substitution groups:

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:

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.

1.9.4.10 Q10

Write a function that determines whether the person listed in a billTo element is known to be a deadbeat, using a US database.

This query assumes that "http://www.usa-deadbeats.com" maintains a database of deadbeats in the following format:

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.

1.9.4.11 Q11

Write a function that computes the total price for a sequence of item elements.

This report, which lists products sold by zip code, is based on US purchase orders, not on the international purchase report used in previous queries. In the XML Schema Primer, these US purchase orders are defined without a namespace. Here is an example of such a purchase order, taken from the XML Schema Primer:

A Acknowledgements

The editors thank the members of the XML Query Working Group, which produced the material in this document.

The use cases in this paper were contributed by the following individuals:

Use Case "R"

Don Chamberlin

Use Case "XMP"

Mary Fernandez, Jerome Simeon, Phil Wadler

Use Case "TREE"

Jonathan Robie

Use Case "PARTS"

Michael Rys

Use Case "NS"

Ingo Macherius

Use Case "STRING"

Umit Yalcinalp

Use Case "SEQ"

Jonathan Robie

Use Case "SGML"

Paula Angerstein

Use Case "STRONG"

Jonathan Robie and Phil Wadler. Schemas and data taken from [XMLSchema0].

Use case "XMP" has been previously published in [Fernandez]. Use cases "Tree" and "Seq" have been previously published in [Robie99].

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.

B Change Log (Non-Normative)

B.1 04 April 2005

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.

B.2 30 Jan 2005

Many changes have been made since the last release, particularly:

Moved to the 29 October XQuery Working Drafts.

Corrected numerous NS queries to reflect current handling of namespaces.

Changed use of locally declared elements and attributes in SequenceType to match current XQuery language.

Fixed reported bugs, ran most queries using two implementations.

C References (Non-Normative)

The following references are some of the works considered by the WG in deriving its use cases.

XML Information Set (Second Edition), John Cowan and Richard Tobin, Editors. World Wide Web Consortium, 04 Feb 2004. This version is http://www.w3.org/TR/2004/REC-xml-infoset-20040204. The latest version is available at http://www.w3.org/TR/xml-infoset.

Namespaces in XML, Tim Bray, Dave Hollander, and Andrew Layman, Editors. World Wide Web Consortium, 14 Jan 1999. This version is http://www.w3.org/TR/1999/REC-xml-names-19990114. The latest version is available at http://www.w3.org/TR/REC-xml-names.

XML Schema Part 0: Primer Second Edition, David C. Fallside and Priscilla Walmsley, Editors. World Wide Web Consortium, 28 Oct 2004. This version is http://www.w3.org/TR/2004/REC-xmlschema-0-20041028/. The latest version is available at http://www.w3.org/TR/xmlschema-0/.

XMLSchema1

XML Schema Part 1: Structures Second Edition, David Beech, Noah Mendelsohn, Murray Maloney, and Henry S. Thompson, Editors. World Wide Web Consortium, 28 Oct 2004. This version is http://www.w3.org/TR/2004/REC-xmlschema-1-20041028/. The latest version is available at http://www.w3.org/TR/xmlschema-1/.