25

How do you split a string based on some separator?

Given a string Topic1,Topic2,Topic3, I want to split the string based on , to generate:

Topic1 Topic2 Topic3
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Ketan
  • 293
  • 1
  • 5
  • 9
  • 1
    possible duplicate of [Does XSLT have a Split() function?](http://stackoverflow.com/questions/136500/does-xslt-have-a-split-function) – Isaac G Sivaa Aug 14 '14 at 17:25

6 Answers6

37

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>
  • Please could you describe what the first template is doing – ziggy Feb 10 '14 at 18:53
  • @ziggy The first template is an identity transformation, meaning it just creates an exact copy of all the nodes and attributes from the XML source. – skrtxao Apr 18 '16 at 04:27
3

Use fn:tokenize

Max Toro
  • 28,282
  • 11
  • 76
  • 114
2

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.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
2

XSLT 1.0
I needed a slight variant compared to other answers given here.

Input:

1, 2, 3

Ouput:

1, 2 and 3

Input:

1

Output

1

If the delimiter is space instead of comma, it would still work.

Input:

1 2 3

Ouput:

1, 2 and 3

I have just created a slightly modified template.

<xsl:template name="tokenizeString">
<xsl:param name="list"/>
<xsl:param name="delimiter"/>
<xsl:choose>
    <xsl:when test="contains($list, $delimiter)">      
        <xsl:variable name="listLength" select="string-length($list)" />
        <xsl:variable name="listLengthWithoutDelimiters" select="string-length(translate($list, $delimiter,''))" />
        <xsl:variable name="noOfDelimiters" select="($listLength - $listLengthWithoutDelimiters)" />

        <xsl:value-of select="substring-before($list,$delimiter)"/>
        <xsl:if test="$noOfDelimiters > 1">, </xsl:if>
        <xsl:if test="$noOfDelimiters = 1"> and </xsl:if>
        <xsl:call-template name="tokenizeString">
            <xsl:with-param name="list" select="substring-after($list,$delimiter)"/>
            <xsl:with-param name="delimiter" select="$delimiter"/>
        </xsl:call-template>
    </xsl:when>
     <xsl:otherwise>
        <xsl:choose>
            <xsl:when test="$list = ''">
                <xsl:text/>
            </xsl:when>
            <xsl:otherwise>
                 <xsl:value-of select="$list"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:otherwise>
</xsl:choose>

The template can be called as below when the delimiter is comma

<xsl:call-template name="tokenizeString">
    <xsl:with-param name="list">1, 2, 3</xsl:with-param>
    <xsl:with-param name="delimiter">
        <xsl:value-of select="','" />
    </xsl:with-param>
</xsl:call-template>

The template can be called as below when the delimiter is space

<xsl:call-template name="tokenizeString">
    <xsl:with-param name="list">1 2 3</xsl:with-param>
    <xsl:with-param name="delimiter">
        <xsl:value-of select="' '" />
    </xsl:with-param>
</xsl:call-template>
Kamal Soni
  • 1,522
  • 13
  • 15
1

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>
Bludwarf
  • 824
  • 9
  • 21
0

Depending on what XSL processor you are using, you may have access to the extension function str:tokenize().

So to split Topic1,Topic2,Topic3 on , do;

<xsl:copy-of select="str:tokenize('Topic1,Topic2,Topic3', ',')"/>

which will give the result;

<token>Topic1</token>
<token>Topic2</token>
<token>Topic3</token>
Nigel Alderton
  • 2,265
  • 2
  • 24
  • 55