在 XSLT 中最后一次出现字符后获取子字符串 [英] Getting a substring AFTER the last occurrence of a character in XSLT

查看:29
本文介绍了在 XSLT 中最后一次出现字符后获取子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 XML 文件中有一个字符串,看起来与此类似:

I have a string in an XML file that looks similar to this:

M:Namespace.Class.Method(Something a,Something b)

M:Namespace.Class.Method(Something a, Something b)

句点 (.) 字符的数量是随机的,这意味着它在本例中只能是 2 个,但可以更多.

The number of period (.) characters is abritrary, meaning it can be only 2 as in this example, but can be more.

我想使用 XSLT 从最后一个 '.' 获取此字符串的子字符串字符,所以我只会留下:

I would like to use XSLT to get a substring of this string from the last '.' character, so that i will only be left with:

方法(某事a,某事b)

Method(Something a, Something b)

我无法使用标准的 substring/substring-after 函数来实现这一点.

I could not achieve this using the standard substring/substring-after functions.

有没有简单的方法可以做到这一点?

Is there an easy way to do this?

推荐答案

在 XSLT 1.0 中,您将需要使用递归模板,如下所示:

  <xsl:template name="substring-after-last">
    <xsl:param name="string" />
    <xsl:param name="delimiter" />
    <xsl:choose>
      <xsl:when test="contains($string, $delimiter)">
        <xsl:call-template name="substring-after-last">
          <xsl:with-param name="string"
            select="substring-after($string, $delimiter)" />
          <xsl:with-param name="delimiter" select="$delimiter" />
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise><xsl:value-of 
                  select="$string" /></xsl:otherwise>
    </xsl:choose>
  </xsl:template>

并像这样调用它:

<xsl:call-template name="substring-after-last">
  <xsl:with-param name="string" select="'M:Namespace.Class.Method(Something a, Something b)'" />
  <xsl:with-param name="delimiter" select="'.'" />
</xsl:call-template>

在 XSLT 2.0 中,您可以使用 tokenize() 函数 并简单地选择序列中的最后一项:

In XSLT 2.0, you can use the tokenize() function and simply select the last item in the sequence:

tokenize('M:Namespace.Class.Method(Something a, Something b)','.')[last()]

这篇关于在 XSLT 中最后一次出现字符后获取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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