下面是一个 XSLT 2.0 示例,可以使用 Saxon 9 或 AltovaXML 或 XmlPrime 等 XSLT 2.0 处理器运行:
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="funding-group"/>
<xsl:template match="body/p//text()">
<xsl:variable name="pattern1" select="string-join(//award-id, '|')"/>
<xsl:variable name="pattern2" select="string-join(//funding-source, '|')"/>
<xsl:analyze-string select="." regex="(^|\s|[,.!?])({$pattern1})($|\s|[,.!?])">
<xsl:matching-substring>
<xsl:value-of select="regex-group(1)"/>
<grant-num><xsl:value-of select="regex-group(2)"/></grant-num>
<xsl:value-of select="regex-group(3)"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:analyze-string select="." regex="(^|\s|[,.!?])({$pattern2})($|\s|[,.!?])">
<xsl:matching-substring>
<xsl:value-of select="regex-group(1)"/>
<grant-sponsor><xsl:value-of select="regex-group(2)"/></grant-sponsor>
<xsl:value-of select="regex-group(3)"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>
它转换输入
<root>
<front>
<funding-group>
<award-group id="award1">
<funding-source id="GS1">A</funding-source>
<award-id rid="GS1">1</award-id>
</award-group>
<award-group id="award2">
<funding-source id="GS2">B</funding-source>
<funding-source id="GS3">C</funding-source>
<award-id rid="GS2 GS3">2</award-id>
</award-group>
</funding-group>
</front>
<body>
<p>This work was supported by the 9 Program Grant 1, A 309 and 311, and B, C Grant 2.</p>
</body>
</root>
进入结果
<root>
<front>
</front>
<body>
<p>This work was supported by the 9 Program Grant <grant-num>1</grant-num>, <grant-sponsor>A</grant-sponsor> 309 and 311, and <grant-sponsor>B</grant-sponsor>, <grant-sponsor>C</grant-sponsor> Grant <grant-num>2</grant-num>.</p>
</body>
</root>