XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – getting started / A comparison: expression versus instruction / A side topic: constructing sequences

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

We have already created sequences in other cases: every XPath expression can potentially do this. For example, expression adres­sen/adresse, starting from the document node of the file considered, produced a sequence of all <adresse> elements in document order.

The present case is less simple. The result must be a sequence of different elements in an order other than document order. This sequence must be constructed.

In XPath 2.0, a sequence can be created by joining comma-separated XPath expressions or literals. To identify it as a sequence, the entire expression is enclosed in parentheses:

(ausdruck_1, ausdruck_2, ausdruck_3)

Both parentheses and the comma are XPath operators, see XPath expressions: operators and keywords. They should therefore not be mistaken for mere punctuation. The sequence defined this way could then be passed to an instruction such as xsl:value-of:

<xsl:value-of select="(ausdruck_1, ausdruck_2, ausdruck_3)"/>

Evaluation works from the inside out. First, the sequence’s expressions are evaluated, then the instruction processes their results in order. But what if one expression itself generates a sequence? This is harmless. Suppose ausdruck_1 generates value wert_1, the second expression a sequence s2_a, s2_b, s2_c and the third another value wert_3. The instruction then “sees” this resulting input sequence:

<xsl:value-of select="(wert_1, s2_a, s2_b, s2_c, wert_3)"/>

You could imagine additional parentheses around the enclosed sequence generated from the second item. This makes no difference, however, because sequences effectively cannot be nested. (Note: This matters when counting items. If an item produces another sequence, it contributes the number of items in that sequence rather than a single item. Both compared sequences therefore contain six items!):

(a, (b, c, d), e, f) is the same as (a, b, c, d, e, f)

Rule of thumb:
If an item in a sequence is itself a sequence, it expands into a series of individual items in the containing sequence.

   

back to top