Does xslt have split() function?

2019-01-04 12:33发布

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

6条回答
Luminary・发光体
2楼-- · 2019-01-04 12:47

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>
查看更多
倾城 Initia
3楼-- · 2019-01-04 12:50

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>
查看更多
三岁会撩人
4楼-- · 2019-01-04 12:56
我想做一个坏孩纸
5楼-- · 2019-01-04 12:56

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>
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-04 12:57

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.

查看更多
你好瞎i
7楼-- · 2019-01-04 13:05

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>
查看更多
登录 后发表回答