首页 > 解决方案 > XSLT - 用特定字符分隔的分割字符串

问题描述

我有一个这样的xml,

<doc>
    <para>A brief 23 spell$of hea#vy rain$forc^%ed an early+ lunch$98@with nine</para>
</doc>

我需要从文本中存在的每个 $ 中断开文本字符串。

所以预期的输出应该是,

<doc>
    <para>A brief 23 spell</para>
    <para>of hea#vy rain</para>
    <para>forc^%ed an early+ lunch</para>
    <para>98@with nine</para>
</doc>

谁能建议我如何使用 XSLT 1.0 做到这一点?

标签: xsltsubstringxslt-1.0

解决方案


尝试这个

<xsl:template match="para">
    <xsl:call-template name="spilit">
        <xsl:with-param name="text" select="."/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="spilit">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="contains($text, '$')">
            <para><xsl:value-of select="substring-before($text, '$')"/></para>
            <xsl:call-template name="spilit">
                <xsl:with-param name="text" select="substring-after($text, '$')"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <para><xsl:value-of select="$text"/></para>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

请参阅https://xsltfiddle.liberty-development.net/bEJbVrg上的转换


推荐阅读