12 xslt
我需要检查一个特定的字符串是否包含一个特定的单词,例如检查SultansOfSwing是否包含单词Swing.
我还要提一下,所讨论的字符串的值是未知的.因为它可以是任何单词所以我们不知道长度等等.
我知道我可以使用contains关键字来做到这一点.
但是一旦我知道这个单词包含Swing关键字,我想显示没有这个"Swing"字样的字符串..因此只能有效地显示"SultansOf".
我一直试图探索如何实现这一点,但没有得到任何突破.
有人可以建议哪个关键字或功能将提供此功能?如何从字符串中删除特定单词.
感谢您的帮助.
问候.
巨熊
我认为这个字符串替换函数非常详尽:
编辑 - 需要将 $string 更改为 $string2。现在应该可以工作了
<xsl:template name="string-replace">
<xsl:param name="string1" select="''" />
<xsl:param name="string2" select="''" />
<xsl:param name="replacement" select="''" />
<xsl:param name="global" select="true()" />
<xsl:choose>
<xsl:when test="contains($string1, $string2)">
<xsl:value-of select="substring-before($string1, $string2)" />
<xsl:value-of select="$replacement" />
<xsl:variable name="rest" select="substring-after($string1, $string2)" />
<xsl:choose>
<xsl:when test="$global">
<xsl:call-template name="string-replace">
<xsl:with-param name="string1" select="$rest" />
<xsl:with-param name="string2" select="$string2" />
<xsl:with-param name="replacement" select="$replacement" />
<xsl:with-param name="global" select="$global" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$rest" />
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string1" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
请注意,它区分大小写。在你的情况下:
<xsl:call-template name="string-replace">
<xsl:with-param name="string1" select="'SultansOfSwing'" />
<xsl:with-param name="string2" select="'Swing'" />
<xsl:with-param name="replacement" select="''" />
</xsl:call-template>
Run Code Online (Sandbox Code Playgroud)