XSLT有split()函数吗?

25

如何根据某个分隔符拆分字符串?

给定一个字符串 Topic1,Topic2,Topic3,我想根据 , 拆分字符串以生成:

Topic1 Topic2 Topic3

1
可能是Does XSLT have a Split() function?的重复问题。 - Isaac G Sivaa
6个回答

37
在XSLT 1.0中,您必须构建一个递归模板。这个样式表:
<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>

输入:

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

输出:

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

在XSLT 2.0中,你可以使用tokenize()核心函数。因此,这个样式表:
<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>

结果:

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

请问您能否描述一下第一个模板正在执行什么操作? - ziggy
@ziggy 第一个模板是身份转换,意味着它只是从XML源创建所有节点和属性的精确副本。 - skrtxao

3

6
仅适用于XSLT 2.0版本。 - ziggy

2

XSLT 1.0
相较于其他答案,我需要稍微修改一下。

输入:

1, 2, 3

输出:

1, 2和3

输入:

1

输出:

1

如果分隔符是空格而不是逗号,它仍然有效。

输入:

1 2 3

输出:

1, 2和3

我刚刚创建了一个稍微修改过的模板。

<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>

当分隔符为逗号时,可以按以下方式调用模板

<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>

当分隔符为空格时,可以按照以下方式调用模板:
<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>

2

虽然没有split函数,但是你可以使用带有substring-beforesubstring-after的递归模板来编写自己的函数。

请参阅此文章了解详情。


1

感谢用户357812。我使用了您的精美模板,并进行了少量定制,使其更加通用:

<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>

0

根据您使用的XSL处理器,您可能可以访问扩展函数str:tokenize()

因此,要在,上拆分Topic1、Topic2、Topic3,请执行以下操作;

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

这会给出结果;

<token>Topic1</token>
<token>Topic2</token>
<token>Topic3</token>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接