我需要检查一个特定的字符串是否包含一个特定的单词,例如,检查是否,
SultansOfSwing包含单词Swing。

我还要提到,所讨论的字符串的值是未知的。因为它可以是任何单词,所以我们不知道长度等。

我知道我可以通过使用contains关键字来做到这一点。

但是,一旦我知道该单词包含Swing关键字,便想显示不带此“Swing”单词的字符串..从而有效地仅显示“SultansOf”。

我一直在尝试探索如何实现这一目标,但没有取得任何突破。

有人可以建议哪个关键字或函数将提供此功能吗?如何从字符串中删除特定单词。

感谢您的帮助。

问候。

可怕的

最佳答案

鉴于此输入:

<root>
    <song>SultansOfSwing</song>
    <song>SwingOfSultans</song>
    <song>SultansSwingOf</song>
</root>

此输出:
<?xml version='1.0' ?>
<root>
    <swing-less-long>SultansOf</swing-less-long>
    <swing-less-long>OfSultans</swing-less-long>
    <swing-less-long>SultansOf</swing-less-long>
</root>

可以从中得到。注意substring-beforesubstring-after的使用。
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:template match="/">
        <root>
            <xsl:apply-templates select="root/song"/>
        </root>
    </xsl:template>
    <xsl:template match="song">
        <swing-less-long>
            <xsl:if test="contains(., 'Swing')">
                <xsl:call-template name="remove">
                    <xsl:with-param name="value" select="."/>
                </xsl:call-template>
            </xsl:if>
        </swing-less-long>
    </xsl:template>
    <xsl:template name="remove">
        <xsl:param name="value"/>
        <xsl:value-of select="concat(substring-before($value, 'Swing'), substring-after($value, 'Swing'))"/>
    </xsl:template>
</xsl:stylesheet>

关于xslt - 如何使用XSLT从字符串中删除特定字符?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1153304/

10-11 22:35
查看更多