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/xpath-string-join.md
T
alexandrev-tibco b9d58ff7e0 feat(blog): XSLT/XPath function reference section (Phase 2)
- 19 reference pages under /xslt/functions/ covering:
  xsl:value-of, xsl:for-each, xsl:for-each-group, xsl:function,
  xsl:iterate, last(), position(), count(), concat(), contains(),
  substring(), sum(), key(), current(), string-join(), tokenize(),
  matches(), replace(), format-date()
- Each page: description, parameters table, return value, 2 runnable
  examples (input XML + stylesheet + output), notes, cross-links
- New Hugo section with reference-specific layout (version badges,
  syntax block, "Try in Playground" CTA, parameters table styles)
- Menu entry "Reference" → /xslt/functions/
- CSS: .ref-badge, .ref-syntax, .ref-index-list, .ref-try + table styles

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-18 14:38:55 +02:00

3.2 KiB

title, description, date, version, versionLabel, category, syntax, tags
title description date version versionLabel category syntax tags
string-join() Joins a sequence of strings into a single string with a specified separator between each item. 2026-04-18T00:00:00Z 2.0 XSLT 2.0 string function string-join(sequence, separator?)
xslt
reference
xslt2
xpath

Description

string-join() takes a sequence of strings and concatenates them into a single string, placing the separator between each consecutive pair of items. If the sequence contains only one item, no separator is added. If the sequence is empty, an empty string is returned.

The separator argument is optional in XPath 3.1; when omitted it defaults to an empty string (items are concatenated with no separator). In XPath 2.0 the separator argument is required.

string-join() is the idiomatic XSLT 2.0+ replacement for the XSLT 1.0 pattern of iterating with xsl:for-each and manually appending separators using position() != last(). It works on any sequence of atomic values (each is converted to a string) and on element text content via paths like element/string().

Parameters

Parameter Type Required Description
sequence xs:string* Yes The sequence of strings to join. Non-string items are converted to strings.
separator xs:string No (XPath 3.1+) / Yes (XPath 2.0) The string inserted between each pair of adjacent items.

Return value

xs:string — all items in the sequence joined by the separator.

Examples

Join element values with a comma

Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<tags>
  <tag>xslt</tag>
  <tag>xml</tag>
  <tag>xpath</tag>
  <tag>transformation</tag>
</tags>

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>

  <xsl:template match="/tags">
    <xsl:value-of select="string-join(tag, ', ')"/>
  </xsl:template>
</xsl:stylesheet>

Output:

xslt, xml, xpath, transformation

Build a pipe-delimited list in an attribute

Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/tags">
    <index keywords="{string-join(tag, '|')}"/>
  </xsl:template>
</xsl:stylesheet>

Output:

<index keywords="xslt|xml|xpath|transformation"/>

Join string values from a sequence expression

<!-- Join all unique category values found in a product catalog -->
<xsl:value-of select="string-join(distinct-values(//product/category), ' / ')"/>

Notes

  • string-join() requires XSLT 2.0 or later. In XSLT 1.0, replicate the behaviour with xsl:for-each and a position() != last() conditional separator.
  • The function does not add a leading or trailing separator — only between items.
  • Non-string items in the sequence (numbers, booleans) are cast to xs:string before joining.
  • To join node string values, use the path expression directly: string-join(item, ', ') extracts each item element's text content automatically.

See also