小编M T*_*ker的帖子

XSLT 2.0 正则表达式替换

我有以下 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)

regex xml xslt xslt-2.0

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

regex ×1

xml ×1

xslt ×1

xslt-2.0 ×1