如何使用 XSLT 从字符串中删除特定字符? [英] How to remove particular characters from a string using XSLT?

查看:38
本文介绍了如何使用 XSLT 从字符串中删除特定字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to check if a particular string contains a a particular word for example to check if, SultansOfSwing contains the word Swing.

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

Let me also mention that the value of the string in question is unknown. As in it can be any word so we do not know the length et cetera.

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

I understand I can do this by using the contains keyword.

但是一旦我知道这个词包含 Swing 关键字,我想显示没有这个Swing"词的字符串..从而有效地只显示SultansOf".

But once I know that this word contains the Swing keyword I want to display the string without this "Swing" word.. thus effectively displaying only "SultansOf".

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

I have been trying to explore how I can achieve this but not getting any break through.

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

Could somebody please advise which keyword or function will provide this facility ? How can I remove a particular word from within a string.

提前感谢您的帮助.

问候.

可怕

推荐答案

鉴于此为输入:

<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 的使用.

Can be gotten from this. Note the use of substring-before and substring-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 从字符串中删除特定字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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