【发布时间】:2011-11-15 06:18:58
【问题描述】:
我只想从一个字符串中取出最后一个元素,就像 xslt 中的“aaa-bbb-ccc-ddd”。
无论'-'如何,输出都应该是“ddd”。
【问题讨论】:
-
搜索“XSLT 字符串拆分”
-
嘿...我使用了标记化功能,它工作了。非常感谢...
-
@Satoshi,如果有帮助,请接受答案。
我只想从一个字符串中取出最后一个元素,就像 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>
【讨论】: