Sponsored

XSLT Reference

xsl:sequence

XSLT 2.0 element

Returns a sequence of items; the XSLT 2.0 equivalent of xsl:value-of for typed values and node sequences.

Syntax
<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-ofxsl:sequence
OutputAlways a text nodeItems of any type
Types preservedNo (stringified)Yes
NodesCopies as textAdds as reference or copy

Parameters

ParameterTypeRequiredDescription
selectXPath expressionYesThe 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:sequence is the only correct way to return typed values (integers, dates, booleans) from xsl:function. Using xsl:value-of inside a function always produces a text node.
  • When select returns nodes, they are added by reference to the sequence, not deep-copied. If you need independent copies, wrap with copy-of().
  • xsl:sequence can appear in any context where items are allowed, including inside xsl:choose, xsl:if, and xsl:for-each.
  • An empty xsl:sequence select="()" adds nothing — useful as a no-op branch in a conditional.

See also