XSLT Reference
xsl:sequence
Returns a sequence of items; the XSLT 2.0 equivalent of xsl:value-of for typed values and node sequences.
<xsl:sequence select="expression"/>Description
xsl:sequence evaluates an XPath expression and adds the resulting items — nodes, atomic values, or mixed sequences — directly to the result sequence of the current template or function. Unlike xsl:value-of, it preserves the type of each item: integers stay integers, nodes stay nodes, and sequences remain sequences.
It is the idiomatic way to return a value from an xsl:function, and the correct instruction to use when you want to pass typed data rather than a string representation.
Key differences from xsl:value-of:
xsl:value-of | xsl:sequence | |
|---|---|---|
| Output | Always a text node | Items of any type |
| Types preserved | No (stringified) | Yes |
| Nodes | Copies as text | Adds as reference or copy |
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
select | XPath expression | Yes | The expression whose result is added to the output sequence. |
Return value
The items produced by the select expression, added to the current output sequence. No wrapper node is created.
Examples
Returning a typed value from a function
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:my="http://example.com/my">
<xsl:output method="xml" indent="yes"/>
<xsl:function name="my:double" as="xs:integer">
<xsl:param name="n" as="xs:integer"/>
<xsl:sequence select="$n * 2"/>
</xsl:function>
<xsl:template match="/">
<result>
<xsl:value-of select="my:double(21)"/>
</result>
</xsl:template>
</xsl:stylesheet>
Output:
<result>42</result>
Returning a node sequence
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<item status="active">Alpha</item>
<item status="inactive">Beta</item>
<item status="active">Gamma</item>
</catalog>
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://example.com/my">
<xsl:output method="xml" indent="yes"/>
<xsl:function name="my:active-items" as="element()*">
<xsl:param name="items" as="element()*"/>
<xsl:sequence select="$items[@status='active']"/>
</xsl:function>
<xsl:template match="/catalog">
<active>
<xsl:for-each select="my:active-items(item)">
<entry><xsl:value-of select="."/></entry>
</xsl:for-each>
</active>
</xsl:template>
</xsl:stylesheet>
Output:
<active>
<entry>Alpha</entry>
<entry>Gamma</entry>
</active>
Notes
xsl:sequenceis the only correct way to return typed values (integers, dates, booleans) fromxsl:function. Usingxsl:value-ofinside a function always produces a text node.- When
selectreturns nodes, they are added by reference to the sequence, not deep-copied. If you need independent copies, wrap withcopy-of(). xsl:sequencecan appear in any context where items are allowed, including insidexsl:choose,xsl:if, andxsl:for-each.- An empty
xsl:sequence select="()"adds nothing — useful as a no-op branch in a conditional.