XML technologies / XPath / XPath 2.0 – Language reference / XPath expressions / The sequence as the result of an expression

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 4.)

In XPath 2.0, evaluating an expression always yields a sequence, a succession of distinct, distinguishable units called items. Rather than using a translation such as “unit of observation” or the potentially misleading “element”, the English term “Item” is retained below. A sequence is ordered, meaning that the order of its items matters.

Parentheses in the following example clarify a literal sequence. They are expressly not part of the output:

XPath-Ausdruck   ->         (Item 1, Item 2, Item 3, ..., Item N)

An item in a sequence can represent either a node in an XML document, such as an element, attribute or text node, or an atomic value that cannot be subdivided further. Numbers and strings are simple examples of atomic values.

Types of sequences

Sequences containing both nodes and atomic values are called mixed sequences. Sequences of atomic values alone can be homogeneous, containing values of just one type, such as only strings, integers or dates, or heterogeneous, containing atomic values of different types.

Sequences consisting exclusively of nodes (nodes) are also called node sequences. A node sequence can contain different kinds of nodes simultaneously. XPath distinguishes seven kinds, called node types, which must not be confused here with data types.

An XPath expression can also generate a single value, for example a digit used for numbering. By definition, this output is also a sequence, but one consisting of a single item. Such a sequence is called a singleton sequence.

A sequence with no items is empty. The empty sequence is the return value of an XPath expression that produces no actual result, formally ensuring a sequence even in this case. The empty sequence can in certain contexts, though not generally, be equated with an empty string. Unlike an empty string, however, it is type-neutral and never causes a type error. It therefore does not matter whether a function returns an empty sequence in a numeric or string context. XPath string functions must, however, explicitly return an empty string instead of an empty sequence.

Complex and simple types

Nodes of complex type, meaning element and document nodes, are considered non-atomic and must sometimes be atomized, that is, converted into a sequence of atomic values.

Also non-atomic are list types, for example attribute values of type IDREFS or NMTOKENS. However, these are not considered complex.

A document node always has a complex type. Element nodes can, but need not, have a complex type. All other node kinds have a simple type, which need not also be atomic, as with the list-typed attribute nodes mentioned above.

Node kind

Data type (value)

Individual name

Document node

complex

no

Element node

complex or simple

yes

Attribute node

simple

yes

Text node

simple

no

Comment node

simple

no

Processing Instruction

simple

yes (target)

Namespace node

simple

no* (arbitrary prefix)

Table: the seven node kinds in XPath.

Node names

Element or attribute node instances must have individual names. For processing-instruction nodes, the target name is treated as a node name. A namespace node can be bound to several prefixes and thus has no fixed name; the processor decides which bound prefix to return for the node.

Node identity

Every node, whether named or unnamed, has an identity (unique node identity), meaning it is identical only to itself. Even two nodes with the same value remain distinguishable by identity. A sequence produced by an XPath expression may legitimately contain two or more occurrences of the same node. Such duplicates can be removed before processing if needed. XPath provides functions for determining whether two nodes are identical.

Comparing node sequences and node-sets
A node sequence resembles a node-set produced by XPath 1.0, but unlike a node-set can contain the same node multiple times. By definition, a node-set cannot do so.

   

back to top