【问题标题】:Selecting a last word from a string in xslt从xslt中的字符串中选择最后一个单词
【发布时间】:2011-11-15 06:18:58
【问题描述】:

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

无论'-'如何,输出都应该是“ddd”。

【问题讨论】:

  • 搜索“XSLT 字符串拆分”
  • 嘿...我使用了标记化功能,它工作了。非常感谢...
  • @Satoshi,如果有帮助,请接受答案。

标签: xslt xpath


【解决方案1】:

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 2016-01-21
    相关资源
    最近更新 更多