How to remove particular characters from a string

2019-01-26 05:26发布

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.

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

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.

Thanks for your help in advance.

Regards.

Dire

标签: xslt
2条回答
神经病院院长
2楼-- · 2019-01-26 06:05

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.

<?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>
查看更多
ら.Afraid
3楼-- · 2019-01-26 06:06

I think this string replacement function is quite exhaustive:

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

<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>
查看更多
登录 后发表回答