首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xslt有split()函数吗?

xslt有split()函数吗?
EN

Stack Overflow用户
提问于 2010-07-26 23:39:46
回答 6查看 64.7K关注 0票数 25

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

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

代码语言:javascript
复制
Topic1 Topic2 Topic3
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2010-07-27 00:00:18

在XSLT 1.0中,您必须构建一个递归模板。此样式表:

代码语言:javascript
复制
<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>

输入:

代码语言:javascript
复制
<root>
<text>Item1, Item2, Item3</text>
</root>

输出:

代码语言:javascript
复制
<root>
    <text>
        <item>Item1</item>
        <item>Item2</item>
        <item>Item3</item>
    </text>
</root>

在XSLT2.0中,您拥有tokenize()核心函数。所以,这个样式表:

代码语言:javascript
复制
<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>

结果:

代码语言:javascript
复制
<root>
    <text>
        <item>Item1</item>
        <item>Item2</item>
        <item>Item3</item>
    </text>
</root>
票数 37
EN

Stack Overflow用户

发布于 2010-07-27 12:48:36

票数 3
EN

Stack Overflow用户

发布于 2010-07-26 23:41:55

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

有关详细信息,请参阅this文章。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3336424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档