>Home>Learn XQuery>XQuery Tutorial>XQuery Introduction>Data Model
The XQuery 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, name-space, processing instruction, and comment. Every node has a unique node identity that distinguishes it from other nodes-even from other nodes that are otherwise identical.
In addition to nodes, the data model allows atomic values, which are single values that correspond to the simple types defined in the W3C Recommendation, "XML Schema, Part 2" [SCHEMA], such as strings, Booleans, decimals, integers, floats and doubles, and dates. These simple types may occur in any document associated with a W3C XML Schema. As we will see later, we can also represent several simple types directly as literals in the XQuery language, including strings, integers, doubles, and decimals.
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 considered children of an element, but they have a defined position in document order: They occur after the element in which they are found, before the children of the element. The relative order of attribute nodes is implementation-dependent. In document order, each node occurs precisely once, so sorting nodes in document order removes duplicates.
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 name, processing instruction, comment, or text node. If the first character of one node occurs before the first character of another node, it will precede that node in document order. Let's explore this using the following small XML document:
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 characters found in the original document-these are identified by underlines in the example. The second node is the comment, followed by the book element, the year attribute, the title element, the text node containing TCP/IP Illustrated, the author element, the last element, the text node containing Stevens, the first element, and the text node containing W.