xslt 1.0 字符串替换功能 [英] xslt 1.0 string replace function
本文介绍了xslt 1.0 字符串替换功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串aa::bb::aa"
I have a string "aa::bb::aa"
并且需要把它变成aa, bb, aa"
and need to turn it in to "aa, bb, aa"
我试过了
translate(string,':',', ')
但这会返回aa,,bb,,aa"
but this returns "aa,,bb,,aa"
如何做到这一点.
推荐答案
一个非常简单的解决方案(只要你的字符串值没有空格,它就可以工作):
translate(normalize-space(translate('aa::bb::cc',':',' ')),' ',',')
- 将:"翻译成"
normalize-space()
将多个空白字符合并为一个空格"- 将单个空格"翻译成,"
- translate ":' into " "
normalize-space()
to collapse multiple whitespace characters into a single space " "- translate single spaces " " into ","
更强大的解决方案是使用 递归模板:
<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>
这篇关于xslt 1.0 字符串替换功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文