如何使用XSLT从字符串中删除特定字符?

时间:2021-09-12 21:40:13

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

我需要检查特定字符串是否包含特定单词,例如检查SultansOfSwing是否包含单词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.

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

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

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

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".

但是一旦我知道这个单词包含Swing关键字,我想显示没有这个“Swing”字样的字符串..因此只能有效地显示“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.

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

Thanks for your help in advance.

感谢您的帮助。

Regards.

Dire

2 个解决方案

#1


Given this for input:

鉴于此输入:

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

The output of this:

这个输出:

<?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>

Can be gotten from this. Note the use of substring-before and substring-after.

可以从中得到。注意使用substring-before和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>

#2


I think this string replacement function is quite exhaustive:

我认为这个字符串替换功能非常详尽:

EDIT - needed to change $string to $string2. Should work now

编辑 - 需要将$ 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>

It's case-sensitive, mind you. In your case:

它是区分大小写的,请注意。在你的情况下:

<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>

#1


Given this for input:

鉴于此输入:

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

The output of this:

这个输出:

<?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>

Can be gotten from this. Note the use of substring-before and substring-after.

可以从中得到。注意使用substring-before和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>

#2


I think this string replacement function is quite exhaustive:

我认为这个字符串替换功能非常详尽:

EDIT - needed to change $string to $string2. Should work now

编辑 - 需要将$ 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>

It's case-sensitive, mind you. In your case:

它是区分大小写的,请注意。在你的情况下:

<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>