我有以下 XML:
<t>a_35345_0_234_345_666_888</t>
Run Code Online (Sandbox Code Playgroud)
我想用固定数字 234 替换“_”之后第一次出现的数字。所以结果应该是这样的:
<t>a_234_0_234_345_666_888</t>
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下方法但它不起作用:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="/">
<xsl:value-of select='replace(., "(.*)_\d+_(.*)", "$1_234_$2")'/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
更新
以下对我有用(感谢@Chris85)。只需删除下划线并添加“?”以使其不贪婪。
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="/">
<xsl:value-of select='replace(., "(.*?)_\d+(.*)", "$1_234$2")'/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)