xslt 中最后一个字符后的子字符串 [英] Substring after last character in xslt
本文介绍了xslt 中最后一个字符后的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我找不到这个问题的确切答案,所以我希望有人能在这里帮助我.
I can't find the exact answer for this question so I hope someone will help me here.
我有一个字符串,我想获取最后一个."之后的子字符串.我正在使用 xslt 1.0.
I have a string and I want get the substring after the last '.'. I'm using xslt 1.0.
这是怎么做到的?这是我的代码.
How is this done? This is my code.
<xsl:choose>
<xsl:otherwise>
<xsl:attribute name="class">method txt-align-left case-names</xsl:attribute> 
<xsl:value-of select="./@name"/> // this prints a string eg: 'something1.something2.something3'
</xsl:otherwise>
</xsl:choose>
当我粘贴建议的代码时,我收到一条错误消息.解析 XSLT 样式表失败."
When i paste the suggested code I get an error message. "Parsing an XSLT stylesheet failed."
推荐答案
我想不出在 XSLT 1.0 中使用单个表达式的方法,但您可以使用递归模板:
I can't think of a way to do this with a single expression in XSLT 1.0, but you can do it with a recursive template:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<n>
<xsl:call-template name="GetLastSegment">
<xsl:with-param name="value" select="'something1.something2.something3'" />
<xsl:with-param name="separator" select="'.'" />
</xsl:call-template>
</n>
</xsl:template>
<xsl:template name="GetLastSegment">
<xsl:param name="value" />
<xsl:param name="separator" select="'.'" />
<xsl:choose>
<xsl:when test="contains($value, $separator)">
<xsl:call-template name="GetLastSegment">
<xsl:with-param name="value" select="substring-after($value, $separator)" />
<xsl:with-param name="separator" select="$separator" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$value" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
结果:
<n>something3</n>
这篇关于xslt 中最后一个字符后的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文