我有一个这样的示例 xsl,
<doc>
<para>text . . .text</para>
<para>text . . .text. . . . . .text</para>
</doc>
如您所见,xml 中有一些模式,例如 。 . .
我需要的是用 *
替换点之间存在的空间。所以输出应该是这样的,
<doc>
<para>text .*.*.text</para>
<para>text .*.*.text.*.*.*.*.*.text</para>
</doc>
我为此编写了以下 xslt,
<xsl:template match="text()">
<xsl:analyze-string select="." regex="(\.)( )(\.)">
<xsl:matching-substring>
<xsl:value-of select="replace(.,regex-group(2),'*')"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
但它消除了所有其他空间并给我以下结果,
<doc>
<para>text .*. .text</para>
<para>text .*. .text.*. .*. .*.text</para>
</doc>
如何修改我的 XSLT 以获得正确的输出..
最佳答案
我觉得
<xsl:template match="text()">
<xsl:analyze-string select="." regex="(\.)( )(\.)( \.)*">
<xsl:matching-substring>
<xsl:value-of select="replace(., ' ','*')"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
完成任务。正如 LukStorms 指出的那样,这可以简化为
<xsl:template match="text()">
<xsl:analyze-string select="." regex="\.( \.)+">
<xsl:matching-substring>
<xsl:value-of select="replace(., ' ','*')"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
关于regex - XSLT - 正则表达式替换字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39163307/