XML technologies / XPath / XPath 2.0 – Language reference / Sequence expressions

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

Besides the usual generation of sequences through XPath expressions, XPath 2.0 allows arbitrary sequences to be created as literals using a corresponding constructor (sequence expression). Sequence types and corresponding operators check whether an expression’s result sequence has the required composition.

Creating sequences

Operators: , (comma), to

XPath provides two operators for directly creating sequences:

  • the comma operator (,), grouping arbitrary XPath expressions into sequences

  • the range operator to, generating sequences of consecutive integers by specifying their lower and upper bounds

The comma operator

The simplest way to create a sequence is the comma operator. All operands to evaluate are placed in parentheses, separated by commas.

Each operand is a separate expression, evaluated in the order given by the sequence expression. The resulting succession of items forms the result sequence:

(1, 2, 3, 4, 5)

creates a sequence of numbers 1, 2, 3, 4 and 5.

Since numbers, atomic values, were supplied here, the operands need no evaluation and appear unchanged in the result sequence.

The comma operator has the lowest precedence in XPath. Expressions it separates can therefore contain subexpressions joined by higher-precedence operators, meaning all other XPath operators.

Caution – Comma operator not used at top level:
Usually the comma operator is not a top-level operator, but joins expressions inside parentheses.

Simple example:

(1, 2 + 3, 3) yields the sequence 1 5 3.

Parentheses delimit the sequence expression. They are required when the comma operator is used in a context requiring a single XPath expression, such as function calls or attributes of XSLT instructions containing XPath expressions. Formally, expression (1, 2, 3, 4, 5) is thus not the sequence itself, but generates it.

This becomes clearer with a node test instead of atomic values as above. Expression (test) creates a sequence of all element nodes <test> that are children of the current node, in document order.

Unlike an XPath 1.0 node-set, a node sequence can contain duplicate nodes. Thus expression (test, test) produces a sequence containing all <test> children of the current node twice, corresponding to two identical concatenated sequences generated by expression (test).

Sequences cannot be nested: a sequence cannot itself serve as an item of another sequence. Combining input sequences therefore produces a sequence whose item count is the sum of all input sequences’ item counts.

Example:

If $seq1 is sequence expression (1, 2, 3) and $seq2 is sequence expression ('a', 'b', 'c'), then sequence expression ($seq1, $seq2) does not produce two sequence items, but six items in the succession 1, 2, 3, 'a', 'b' and 'c'.

This also means expressions ((1, 2, 3), ('a', 'b', 'c')) and (1, 2, 3, 'a', 'b', 'c') are identical.

The 'to' range operator in integer sequences

To create a sequence of consecutive integers, individual values can be replaced by a RangeExpr (range expression), using operator to:

Expression (1 to 5) yields integer sequence 1, 2, 3, 4, 5.

Such a consecutive sequence can be filtered with a predicate to generate other regular number series. This expression returns numbers divisible by 5:

(1 to 50)[. mod 5 eq 0]

The result is 5, 10, 15, 20, 25, 30, 35, 40, 45, 50. Numbers whose remainder modulo 5 is not zero, hence not divisible by 5 without remainder, are filtered out of the original series.

Range expressions can also be used within for expressions. To process the first five nodes of node sequence $meine_sequenz, simply write:

for $i in 1 to 5      return ($meine_sequenz)[$i]

Temporary variable $i successively takes values 1 through 5 and filters the input sequence through a corresponding numeric predicate.

Describing sequences: sequence types

During processing, checking a sequence’s composition can be useful. XPath 2.0 therefore allows its composition of nodes or atomic values to be described with arbitrary precision, as a sequence type (Sequence Type). Checking a sequence against a sequence type is called Sequence Type Matching.

XSLT uses sequence types with the as attribute of various instructions to specify the required composition of a sequence to process.

Caution – Runtime error for sequence-type violations:
If an XSLT instruction’s result sequence does not meet the type, composition, quantity or schema-type requirements specified in its as attribute, a runtime error occurs, on the assumption that further processing of invalid data would be pointless.

The as attribute is defined from XSLT 2.0 for xsl:template, xsl:variable, xsl:param and for the new instruction xsl:function. No comparable concept exists in XSLT 1.0.

Sequence-type syntax largely corresponds to node tests (KindTests) in path-expression steps. Sequence types nevertheless have specific extensions that need to be discussed here.

  • More test expressions:
    Additional sequence-type tests describe both the empty sequence and general items:

    • empty() describes the empty sequence

    • item() describes an item of any kind, node or atomic

  • Describing cardinality:
    A node test, better called an item test here, within a sequence-type expression can be given a cardinality indicator by appending ?, + or *. This expresses the number of items in the sequence.

    • item()?
      Item occurs once or not at all, optional

    • item()+
      Item occurs one or more times, unbounded

    • item()*
      Item occurs any number of times or not at all

    • item()
      Without an indicator: item occurs exactly once, required

Instead of the item() test above, any other valid test expression can be used, except empty().

Sequence-type expressions can also contain all “KindTests” known from location steps, namely those already available in XPath 1.0: node(), text(), comment() and processing-instruc­tion(). The two tests introduced in XPath 2.0, element() and attribute(), are also possible. Their optional arguments can specify the QName and data type of the named node, or a schema path. A third new expression is test document-node(), which can describe a document node’s content as a sequence-type expression.

For the last three test expressions:

  • attribute(mein:wurzelelement/mein:bezeichner/@mein:attribut)
    denotes an attribute with QName mein:attribut, declared in a schema within element declarations mein:wurzelelement and mein:bezeichner. The first element declaration must be a top-level declaration in scope. The attribute name appears last in the path, preceded by @.

  • element(mein:bezeichner, user:datentyp)
    denotes an element with QName mein:bezeichner, expanded during comparison, and a user-defined type user:datentyp. Both must match a corresponding schema declaration in scope.

  • document-node(element(mein:wurzelelement))
    denotes a document node with root element mein:wurzelele­ment as child, described by an element test. Root element mein:wurzelelement must match a top-level declaration in an in-scope schema and the type specified there.

   

back to top