XSLT:如何匹配元素中的精确文本值并替换



我需要在文档中找到一个特定的文本值,'method'并且对于每个实例,将该文本值'method'替换为以下内容:

元素替换方法

此"方法"值可以在整个文档中多次出现。问题是我还需要保留元素中的剩余文本,除了将被替换的"方法"。

<section id="1">
<title>Methods</title>
<p>The test method blah has 6 types of methods available</p>
<p>With the exception of a specific method<p
</section>
<section id="2">
<title>Organisations</title>
<p>The organisation has a method</p>
</section>

我不确定使用 fn:replace 是否是最好的方法,以及我是否还需要使用正则表达式(我目前不熟悉的东西(。如有任何关于此处方法的建议,将不胜感激。

预期输出仅将确切的文本"方法"替换为内容元素,但保留"方法":

<section id="1">
<title>Methods</title>
<p>The test <content type="description" xlink:href="linktodescription">method</named-content> blah has 6 types of methods available</p>
</section>     
<section id="2">
<title>Organisations</title>
<p>The organisation has a <content type="description" xlink:href="linktodescription">method</named-content></p>
</section>

假设 Saxon 9 是您可以使用的 XSLT 处理器(基于 http://saxonica.com/html/documentation/xsl-elements/analyze-string.html(

<xsl:template match="section/p">
<xsl:copy>
<xsl:analyze-string select="." regex="bmethodb" flags=";j">
<xsl:matching-substring>
<content type="description" xlink:href="linktodescription">
<xsl:value-of select="."/>
</content>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:copy>
</xsl:template>

最新更新