Does xslt have split() function?

In XSLT 1.0 you have to built a recursive template. This stylesheet:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="text/text()" name="tokenize">
        <xsl:param name="text" select="."/>
        <xsl:param name="separator" select="','"/>
        <xsl:choose>
            <xsl:when test="not(contains($text, $separator))">
                <item>
                    <xsl:value-of select="normalize-space($text)"/>
                </item>
            </xsl:when>
            <xsl:otherwise>
                <item>
                    <xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
                </item>
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="text" select="substring-after($text, $separator)"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

Input:

<root>
<text>Item1, Item2, Item3</text>
</root>

Output:

<root>
    <text>
        <item>Item1</item>
        <item>Item2</item>
        <item>Item3</item>
    </text>
</root>

In XSLT 2.0 you have the tokenize() core function. So, this stylesheet:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="text/text()" name="tokenize">
        <xsl:param name="separator" select="','"/>
        <xsl:for-each select="tokenize(.,$separator)">
                <item>
                    <xsl:value-of select="normalize-space(.)"/>
                </item>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Result:

<root>
    <text>
        <item>Item1</item>
        <item>Item2</item>
        <item>Item3</item>
    </text>
</root>

Use fn:tokenize


There is no split function, but you can use a recursive template with substring-before and substring-after to write your own.

See this article for details.


Thank you user357812. I use your nice template with little customization to make it generic :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <!-- Main template -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" mode="tokenize-children" />
        </xsl:copy>
    </xsl:template>

    <!-- Split child nodes -->
    <xsl:template match="*" mode="tokenize-children">
        <xsl:copy>
            <xsl:apply-templates select="@*" />
            <xsl:apply-templates select="*" mode="tokenize" />
        </xsl:copy>
    </xsl:template>

    <!-- Tokenize text node of child nodes -->
    <xsl:template match="*/text()" name="tokenize" mode="tokenize">
        <xsl:param name="text" select="."/>
        <xsl:param name="separator" select="','"/>
        <xsl:variable name="item"   select="name(..)" />
        <xsl:choose>
            <xsl:when test="not(contains($text, $separator))">
                <xsl:element name="{$item}">
                    <xsl:value-of select="normalize-space($text)"/>
                </xsl:element>
            </xsl:when>
            <xsl:otherwise>
                <xsl:element name="{$item}">
                    <xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
                </xsl:element>
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="text" select="substring-after($text, $separator)"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>