获得一个子串字符的XSLT最后发生后 [英] Getting a substring AFTER the last occurrence of a character in XSLT

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

问题描述

我有一个XML文件中的字符串,类似于这样:

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

L:Namespace.Class.Method(东西,东西二)

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

周期的(。),字符数是abritrary,这意味着它可以是只有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:

方法(东西,东西二)

我不能使用标准的子/子串的后功能实现这一点。

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中,可以使用的标记化()函数 并只需选择序列中的最后一个项目:

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天全站免登陆