xslt - 从 xslt 中的字符串中选择最后一个单词

标签 xslt xpath

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

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

最佳答案

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>

关于xslt - 从 xslt 中的字符串中选择最后一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8132257/

相关文章:

xml - 使用 XSLT <xsl :when>? 时如何指定 "equals to”

xml - BaseX数据库: How to specify namespace context for query using xpath?

html - 抓取 CSS 以批量检查响应能力

javascript - 我想要跨度和类的正确 xpath

XSLT 选择除一个子节点文本之外的所有文本

javascript - 未定义 : undefined error when calling XSLTProcessor. 原型(prototype).importStylesheet

javascript - 使用 Greasemonkey 脚本将 javascript 添加到使用 XSLT 转换的 XML 文件

c# - 如何使用 xsl 拆分 html 文件?

java - jaxb-bindingx.xml “results in too many target nodes”

c# - 从 XPathExpressions 构建 XML 文件