xslt 有 split() 功能吗? [英] Does xslt have split() function?

查看:34
本文介绍了xslt 有 split() 功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do you split a string based on some separator?

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

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

Topic1 Topic2 Topic3

推荐答案

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

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>

输入:

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

输出:

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

在 XSLT 2.0 中,您有 tokenize() 核心功能.所以,这个样式表:

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>

结果:

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

这篇关于xslt 有 split() 功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆