XSLT 1.0字符串替换功能
问题描述:
我有一个字符串 “AA :: BB :: AA”XSLT 1.0字符串替换功能
,需要把它在为 “AA,BB,AA”
我已经试过
translate(string,':',', ')
但是这返回“aa ,, bb,aa”
这怎么能做到。
答
一个非常简单的解决方案(将工作,只要你的字符串值没有空格):
translate(normalize-space(translate('aa::bb::cc',':',' ')),' ',',')
- 翻译“:”进 “”
-
normalize-space()
塌陷多将空白字符合并为一个空格“” - 将单个空格“”转换为“,”
一个更强大的解决方案是使用recursive template:
<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="with"/>
<xsl:choose>
<xsl:when test="contains($text,$replace)">
<xsl:value-of select="substring-before($text,$replace)"/>
<xsl:value-of select="$with"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text"
select="substring-after($text,$replace)"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="with" select="$with"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
您可以使用它像这样:
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="'aa::bb::cc'"/>
<xsl:with-param name="replace" select="'::'" />
<xsl:with-param name="with" select="','"/>
</xsl:call-template>
答
您可以使用此
语法: - fn:tokenize(string,pattern)
Exa mple:tokenize("XPath is fun", "\s+")
结果:(“XPath”,“is”,“fun”)
此问题标记为XSLT 1.0。您的答案需要XSLT 2.0。 –