Chapter 4. XDM: the data model for XPath and XQuery

The XQuery Data Model (XDM) is more precisely a common data model for XQuery 1.0 and XPath 2.0 (extended with XSLT 2.0). It became a W3C recommendation in December of 2010. thus replacing its previous version appeared in 2007. The primary goal of the XDM is to provide the required information set from an input XML for an XSLT or XQuery engine. Moreover, it provides the acceptable expressions for the XSLT, XQuery and XPath languages.

By definition, a language is closed for its data model if any of the language’s expressions value remains inside the data model. The XSLT 2.0, XQuery 1.0 and the XPath 2.0 are all closed for the forthcoming data model which is based on the previously mentioned Infoset recommendation from 2004 extended with the type hierarchy used by XML Schema.

Figure 4.1. The XDM type hierarchy

The XDM type hierarchy.


Like the Infoset, the XDM defines all the information that could be collected from an XML document but does not impose any language binding or interface for reaching the data. The early 1.0 version was extended by the typed atomic values and the term of the sequences (replacing the set term).

A sequence is a collection of elements. In this new notation, an element could be a node or an atomic value.

Sequence type t (abstract)
t ::= empty-sequence() | item occ
occ ::= + | * | ? | ε
item ::= atomic | node | item()
node ::= element(name) | text() | node() | ...
name ::= * | QName
atomic ::= integer | string | double | ...

Important to know that during the parsing every sequence has a type always. In the following example the most general type comes first:

<x>foo</x> => element(x), item()
()         =>  empty-sequence(),integer*
("foo", "bar")   => string+, item()*
(<x/>, <y/>)     => element(*)+, node()*

Sequences cannot hold sequences inside. When we merge them the result always a flat sequence will be. Nested sequences cannot exists.

(0, (), (1, 2), (3)) ≡ (0, 1, 2, 3)
(0, 1) ≡ (1, 0)

Moreover, there is no difference between an element and a one sized sequence. An element is also a sequence! An other important difference form the 1.0 version - which originates for the set theory where one element could exist only one times in a set, that in 2.0 the sequences are allowing to include one element more than one times in the sequence and its identity is reserved.

An atomic value is derived from its domain where the atomic type could be a primitive type or its derivatives - created by restrictions. The root of the type hierarchy is the untypedAtomic, which is utilized when we wok with an unvalidated XML document. In that case, the runtime engine tries to cast it to the most specific type automatically (which could be an advantage but imposes risks too):

"42" + 1 ⇒ type error (compile time)
<x>42</x> + 1 ⇒ 43.0E0 (: double :)
<x>fortytwo</x> + 1 ⇒ conversion error (runtime)

Along with the sequences, the node identity play an important role in XDM. In every instance of the data model every node has its own identity. (Contrary with the atomic values whose identity is not definable.) The character '5' will mean the same five as a number everywhere in the document.

<x>foo</x> is <x>foo</x> ⇒ false()

<x>foo</x> = <x>foo</x> ⇒ true()

During the processing the most important term is thedocument order: its interpreted inside the nodes affected by a query or transformation and defines the order of the elements in the serialized version of the document. It is corresponding to the order in which the first character of the XML representation of each node occurs in the XML representation of the document after expansion of general entities. Further characteristics:

The instances of the data model could be created basically from two types of XML documents:

In the first case, the data model is based on the InfoSet where general entities are resolved. In the second case, the above mentioned PSVI is used fo it. In the XDM the XML document is modeled by tress with the following node types only:

Its good to know what are the available properties for these nodes:

The following example show how data types are changing:

<x>
  04<!--  unexpected  comment -->2
</x>

The attributes of the element before validation:

The attributes of the element after validation:

Differences from the DOM

It is prohibited to follow text nodes each other and attribute nodes have not got a parent. CDATA sections are appearing as a text node. Finally, all the entity references are resolved, so they cannot appear as nodes.

Namespaces are always present at each node, not like in DOM where only explicit namespaces are in use.