Sample Data: A Bibliography
This chapter uses bibliography data to illustrate the basic
features of XQuery. The data used is taken from the XML Query Use
Cases, Use Case "XMP," and originally appeared in [EXEMPLARS]. We
have modified the data slightly to
Listing 1.1 Bibliography Data for Use Case "XMP"
<bib>
<book year="1994">
<title>TCP/IP Illustrated</title>
<author><last>Stevens</last><first>W.</first></author>
<publisher>Addison-Wesley</publisher>
<price>65.95</price>
</book>
<book year="1992">
<title>Advanced Programming in the UNIX Environment</title>
<author><last>Stevens</last><first>W.</first></author>
<publisher>Addison-Wesley</publisher>
<price>65.95</price>
</book>
<book year="2000">
<title>Data on the Web</title>
<author><last>Abiteboul</last><first>Serge</first></author>
<author><last>Buneman</last><first>Peter</first></author>
<author><last>Suciu</last><first>Dan</first></author>
<publisher>Morgan Kaufmann Publishers</publisher>
<price>65.95</price>
</book>
<book year="1999">
<title>The Economics of Technology and Content
for Digital TV</title>
<editor>
<last>Gerbarg</last>
<first>Darcy</first>
<affiliation>CITI</affiliation>
</editor>
<publisher>Kluwer Academic Publishers</publisher>
<price>129.95</price>
</book>
</bib>
The data for this example was created using a DTD, which
specifies that a bibliography is a sequence of books, each book has
a title, publication year (as an
attribute
), an author or an
editor, a publisher, and a price, and each author or editor has a
first and a last
Listing 1.2 DTD for the Bibliography Data<!ELEMENT bib (book* )> <!ELEMENT book (title, (author+ editor+ ), publisher, price )> <!ATTLIST book year CDATA #REQUIRED > <!ELEMENT author (last, first )> <!ELEMENT editor (last, first, affiliation )> <!ELEMENT title (#PCDATA )> <!ELEMENT last (#PCDATA )> <!ELEMENT first (#PCDATA )> <!ELEMENT affiliation (#PCDATA )> <!ELEMENT publisher (#PCDATA )> <!ELEMENT price (#PCDATA )> |
Data Model
XQuery is defined in terms of a formal data model, not in terms of XML text. Every input to a query is an instance of the data model, and the output of every query is an instance of the data model. In the XQuery data model, every document is represented as a tree of nodes. The kinds of nodes that may occur are: document,
element
, attribute, text,
namespace
, processing instruction, and comment. Every node has a unique node identity that distinguishes it from other nodes ”even from other nodes that are
In addition to nodes, the data model allows
atomic values
, which are single values that
An item is a single node or atomic value. A series of items is known as a sequence. In XQuery, every value is a sequence, and there is no distinction between a single item and a sequence of length one. Sequences can only contain nodes or atomic values; they cannot contain other sequences.
The first node in any document is the document node, which contains the entire document. The document node does not correspond to anything visible in the document; it represents the document itself. Element nodes, comment nodes, and processing instruction nodes occur in the order in which they are found in the XML (after expansion of entities). Element nodes occur before their children ”the element nodes, text nodes, comment nodes, and processing instructions they contain. Attributes are not
An easy way to understand document order is to look at the text of an XML document and mark the first character of each element
start tag
, attribute
<! document order > < book y ear="1994"> < title>TCP/ I P Illustrated</title> < author> < last> S tevens</last> < first> W .</first></author> </book>
The first node of any document is the document node. After that, we can identify the sequence of nodes by looking at the sequence of start
|