2011-11-15 50 views
9

我只想从xslt中的这个“aaa-bbb-ccc-ddd”字符串取出最后一个元素。从xslt中的字符串中选择最后一个字

输出应该是“ddd”而不考虑' - '。

+1

搜索“XSLT字符串分割” –

+0

嘿...我使用了tokenize函数,它工作了。非常感谢你... – Satoshi

+0

@Satoshi,plz接受答案,如果它有帮助。 –

回答

12

XSLT/Xpath的2.0 - 利用tokenize()功能分割的字符串 “ - ”,然后使用谓词过滤器来选择所述序列中的最后一个项目:

<?xml version="1.0"?> 
<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <xsl:value-of select="tokenize('aaa-bbb-ccc-ddd','-')[last()]"/> 
    </xsl:template> 
</xsl:stylesheet> 

XSLT/XPath的1.0 - 使用a recursive template寻找最后一次出现“ - ”,并选择以下子吧:

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <xsl:call-template name="substring-after-last"> 
      <xsl:with-param name="input" select="'aaa-bbb-ccc-ddd'" /> 
      <xsl:with-param name="marker" select="'-'" /> 
     </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="substring-after-last"> 
     <xsl:param name="input" /> 
     <xsl:param name="marker" /> 
     <xsl:choose> 
      <xsl:when test="contains($input,$marker)"> 
       <xsl:call-template name="substring-after-last"> 
        <xsl:with-param name="input" 
      select="substring-after($input,$marker)" /> 
        <xsl:with-param name="marker" select="$marker" /> 
       </xsl:call-template> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="$input" /> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 
相关问题