在字符串中反向查找 [英] Reverse Find in a string

查看:40
本文介绍了在字符串中反向查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够找到元素中最后一次出现的字符.

I need to be able to find the last occurrence of a character within an element.

例如:

<mediaurl>http://www.blah.com/path/to/file/media.jpg</mediaurl>

如果我尝试通过使用 substring-before(mediaurl, '.')substring-after(mediaurl, '.') 来定位它,那么它会,当然,匹配第一个点.

If I try to locate it through using substring-before(mediaurl, '.') and substring-after(mediaurl, '.') then it will, of course, match on the first dot.

我如何获得文件扩展名?从本质上讲,我需要从这样的路径中获取文件名和扩展名,但我对如何使用 XSLT 感到非常困惑.

How would I get the file extension? Essentially, I need to get the file name and the extension from a path like this, but I am quite stumped as to how to do it using XSLT.

推荐答案

以下是在 XSLT 1.0 中生成所需输出的模板示例:

The following is an example of a template that would produce the required output in XSLT 1.0:

<xsl:template name="getExtension">
<xsl:param name="filename"/>

  <xsl:choose>
    <xsl:when test="contains($filename, '.')">
    <xsl:call-template name="getExtension">
      <xsl:with-param name="filename" select="substring-after($filename, '.')"/>
    </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$filename"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="/">
    <xsl:call-template name="getExtension">
        <xsl:with-param name="filename" select="'http://www.blah.com/path/to/file/media.jpg'"/>
    </xsl:call-template>
</xsl:template>

这篇关于在字符串中反向查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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