XSLT - Creating Sequences and Sequence Types in XSLT 2.0
Introduction
Sequences are one of the important features introduced in XSLT 2.0. A sequence is an ordered collection of items that can contain atomic values, nodes, or a combination of both. Unlike traditional XSLT 1.0 processing, where variables generally contained a single value or a node-set, XSLT 2.0 provides much more flexibility by allowing a variable or expression to contain a sequence of multiple items.
For example, a sequence can contain several numbers such as 10, 20, 30, several strings such as "Apple", "Mango", "Orange", or a collection of XML nodes. Sequences are useful when processing multiple values together, passing collections to functions, filtering information, and performing calculations. They form an important foundation for many of the advanced features of XSLT 2.0 and XPath 2.0.
Creating a Sequence
A sequence can be created using expressions separated by commas. For example:
<xsl:variable name="numbers" select="(10, 20, 30, 40, 50)"/>
Here, the variable numbers contains five items. The parentheses indicate that the expression represents a sequence. The individual values are called items in the sequence.
A sequence can also contain strings:
<xsl:variable name="cities" select="('Bengaluru', 'Mysuru', 'Kodagu')"/>
The variable contains three string values. These values can later be processed individually using XPath expressions or XSLT instructions.
Sequences can also be created from XML nodes. For example:
<xsl:variable name="products" select="/catalog/product"/>
If the XML document contains several <product> elements, the variable represents a sequence of those nodes.
Sequence Items
Every sequence consists of zero or more items. An item can be either a node or an atomic value.
Atomic values include data such as strings, numbers, dates, and Boolean values. For example:
<xsl:variable name="data" select="('John', 25, true())"/>
This sequence contains three different atomic values: a string, an integer, and a Boolean value.
A sequence can also be empty:
<xsl:variable name="empty" select="()"/>
The empty sequence is useful when an expression may return no result.
Accessing Items in a Sequence
Items in a sequence are accessed using positional indexes. XPath sequence positions begin with 1, not 0.
For example:
<xsl:variable name="colors" select="('Red', 'Green', 'Blue')"/>
<xsl:value-of select="$colors[1]"/>
The result is:
Red
Similarly:
<xsl:value-of select="$colors[2]"/>
produces:
Green
The expression $colors[1] selects the first item, while $colors[2] selects the second item.
The last item can be selected using:
<xsl:value-of select="$colors[last()]"/>
This returns:
Blue
Processing a Sequence with xsl:for-each
Sequences are particularly useful when multiple values need to be processed individually. The xsl:for-each instruction can iterate through a sequence.
For example:
<xsl:variable name="cities"
select="('Bengaluru', 'Mysuru', 'Madikeri')"/>
<xsl:for-each select="$cities">
<city>
<xsl:value-of select="."/>
</city>
</xsl:for-each>
The output can be:
<city>Bengaluru</city>
<city>Mysuru</city>
<city>Madikeri</city>
The dot (.) represents the current item being processed.
Sequence Types
A sequence type describes what kind of items a sequence is expected to contain and how many items it can contain. Sequence types are especially useful in XSLT 2.0 functions, variables, parameters, and other expressions where the expected data type needs to be specified.
A sequence type has two main parts:
-
The type of item.
-
The allowed occurrence of that item.
For example:
xs:string
means a single string value.
The notation:
xs:string*
means zero or more strings.
The notation:
xs:string+
means one or more strings.
The notation:
xs:string?
means zero or one string.
The notation:
xs:string
normally represents exactly one item of type xs:string.
Occurrence Indicators
XSLT 2.0 provides occurrence indicators to describe how many items are allowed.
The ? indicator means zero or one item.
xs:string?
This means the sequence can contain either no string or one string.
The * indicator means zero or more items.
xs:string*
This allows an empty sequence or any number of strings.
The + indicator means one or more items.
xs:string+
This requires at least one string.
These indicators make it possible to clearly describe the expected structure of data.
Sequence Types in Functions
Sequence types are particularly important when defining functions. They allow the stylesheet developer to specify what type of input a function expects and what type of result it returns.
For example:
<xsl:function name="my:total" as="xs:integer">
<xsl:param name="values" as="xs:integer*"/>
<xsl:sequence select="sum($values)"/>
</xsl:function>
In this example, the parameter values is declared as:
xs:integer*
This means that it can contain zero or more integer values.
The as attribute of xsl:function specifies:
xs:integer
Therefore, the function is expected to return an integer.
The function could be called with:
<xsl:value-of select="my:total((10, 20, 30, 40))"/>
The result is:
100
The xsl:sequence Instruction
The xsl:sequence instruction is used to construct or return a sequence. It is especially useful in functions and templates.
For example:
<xsl:sequence select="(10, 20, 30)"/>
This returns a sequence containing three integers.
Another example is:
<xsl:sequence select="$cities"/>
If $cities contains several values, all of those values are returned as a sequence.
This makes xsl:sequence different from simply generating text output. It returns actual XSLT/XPath values that can continue to be processed by other expressions.
Sequence Concatenation
Multiple sequences can be combined using the comma operator.
For example:
<xsl:variable name="first" select="('A', 'B')"/>
<xsl:variable name="second" select="('C', 'D')"/>
<xsl:variable name="combined"
select="($first, $second)"/>
The resulting sequence is:
A, B, C, D
This is useful when information comes from different sources and needs to be processed as one sequence.
Filtering a Sequence
Sequences can also be filtered using predicates.
For example:
<xsl:variable name="numbers"
select="(10, 15, 20, 25, 30)"/>
<xsl:value-of select="$numbers[. > 20]"/>
The expression selects values greater than 20.
The resulting values are:
25 30
This demonstrates how XPath expressions can be applied directly to sequences.
Importance of Sequences in XSLT 2.0
Sequences make XSLT 2.0 considerably more powerful for handling collections of information. They allow developers to work with multiple values without having to create unnecessary XML structures. They can contain nodes, strings, numbers, dates, and other atomic values.
Sequence types additionally provide a way to describe the expected data structure. This improves clarity and allows XSLT processors to detect certain type-related problems more effectively.
Sequences are also heavily used by XPath 2.0 and XSLT 2.0 functions. Understanding them makes advanced concepts such as user-defined functions, type checking, aggregation, filtering, and transformation of complex XML data much easier to understand.
Conclusion
Creating sequences and defining sequence types is a fundamental feature of XSLT 2.0. A sequence can contain zero or more items, including XML nodes and atomic values. Developers can create sequences using XPath expressions, access individual items by position, filter them, combine them, and process them with XSLT instructions.
Sequence types add another level of control by specifying the expected type and number of items using indicators such as ?, *, and +. Together, sequences and sequence types provide a flexible and strongly structured way to handle collections of data in XSLT 2.0.