1
0
mirror of https://github.com/alexandrev/xslt-lab.git synced 2026-09-18 19:43:16 +00:00
Files
xslt-lab/site/content/xslt/functions/xsl-sequence.md
T
alexandrev-tibco 53e90ef86e feat(blog): complete XSLT/XPath reference — 229 function pages
Full coverage of XSLT 1.0, 2.0 and 3.0 elements and XPath functions:
- 59 XSLT elements (xsl:stylesheet → xsl:use-accumulators)
- 170 XPath functions (1.0 node/string/numeric/boolean, 2.0 sequence/
  date/QName/string, 3.0 HOF/map/array/JSON/streaming)
Each page: description, parameters table, return value, 2 runnable
Saxon examples with input XML + stylesheet + output, notes, cross-links.
xsltCompletions.js: all 229 entries now have blogSlug for hover links.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-19 09:28:05 +02:00

3.7 KiB

title, description, date, version, versionLabel, category, syntax, tags
title description date version versionLabel category syntax tags
xsl:sequence Returns a sequence of items; the XSLT 2.0 equivalent of xsl:value-of for typed values and node sequences. 2026-04-18T00:00:00Z 2.0 XSLT 2.0 element <xsl:sequence select="expression"/>
xslt
reference
xslt2

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: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