我不能编辑XML,我只是想改变一个XSLT文件的XML数据。
<xsl:value-of select="Name" disable-output-escaping="yes"/>
XML数据的价值是"Northfield Bancorp Inc.(MHC)"
,我想用来替换"Northfield Bancorp Inc."
(删除"MHC"
有什么功能XSLT可以搜索并替换了这个可用?
我不能编辑XML,我只是想改变一个XSLT文件的XML数据。
<xsl:value-of select="Name" disable-output-escaping="yes"/>
XML数据的价值是"Northfield Bancorp Inc.(MHC)"
,我想用来替换"Northfield Bancorp Inc."
(删除"MHC"
有什么功能XSLT可以搜索并替换了这个可用?
如果它仅仅是“(MHC)”你要删除的字符串的结尾,这将做到:
<xsl:value-of select="
substring-before(
concat(Name, '(MHC)'),
'(MHC)'
)
" />
如果你想动态替换,你可以写这样的功能:
<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 2.0具有替换()函数。
如果你坚持1.0,则存在可以站在为缺乏原生功能的标准库模板:
http://prdownloads.sourceforge.net/xsltsl/xsltsl-1.2.1.zip http://xsltsl.sourceforge.net/string.html#template.str:subst
从exslt.org另一个XSLT 1.0模板
http://www.exslt.org/str/functions/replace/str.replace.template.xsl