XSLT Reference
string-join()
Joins a sequence of strings into a single string with a specified separator between each item.
string-join(sequence, separator?)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 withxsl:for-eachand aposition() != 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:stringbefore joining. - To join node string values, use the path expression directly:
string-join(item, ', ')extracts eachitemelement’s text content automatically.