本文介绍了替换 XSLT 中的 XML 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法编辑 XML,我只想更改 XSLT 文件中的 XML 数据.
I can't edit XML, I just want to change XML data in an XSLT file.
<xsl:value-of select="Name" disable-output-escaping="yes"/>
XML 数据的值是 "Northfield Bancorp Inc.(MHC)"
我想用 "Northfield Bancorp Inc."
替换它(删除 MHC"
).
The value of XML data is "Northfield Bancorp Inc.(MHC)"
and I want to replace it with "Northfield Bancorp Inc."
(remove "MHC"
).
XSLT 中是否有可以搜索和替换 this 的函数?
Is there any function available in XSLT which can search and replace the this?
推荐答案
如果只是要删除字符串末尾的(MHC)",则这样做:
If it is just the "(MHC)" at the end of the string you want to remove, this would do:
<xsl:value-of select="
substring-before(
concat(Name, '(MHC)'),
'(MHC)'
)
" />
如果你想动态替换,你可以写一个这样的函数:
If you want to replace dynamically, you could write a function like this:
<xsl:template name="string-replace">
<xsl:param name="subject" select="''" />
<xsl:param name="search" select="''" />
<xsl:param name="replacement" select="''" />
<xsl:param name="global" select="false()" />
<xsl:choose>
<xsl:when test="contains($subject, $search)">
<xsl:value-of select="substring-before($subject, $search)" />
<xsl:value-of select="$replacement" />
<xsl:variable name="rest" select="substring-after($subject, $search)" />
<xsl:choose>
<xsl:when test="$global">
<xsl:call-template name="string-replace">
<xsl:with-param name="subject" select="$rest" />
<xsl:with-param name="search" select="$search" />
<xsl:with-param name="replacement" select="$replacement" />
<xsl:with-param name="global" select="$global" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$rest" />
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$subject" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
可调用为:
<xsl:call-template name="string-replace">
<xsl:with-param name="subject" select="Name" />
<xsl:with-param name="search" select="'(MHC)'" />
<xsl:with-param name="replacement" select="''" />
<xsl:with-param name="global" select="true()" />
</xsl:call-template>
这篇关于替换 XSLT 中的 XML 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!