我把链接到主文本的词汇表。
这里是一个身份模板变换:
<xsl:template match="page/p/text()">
<xsl:analyze-string select="." regex="\s(beat)\s" flags="i">
<xsl:matching-substring>
<xsl:text> </xsl:text>
<term keyref="beat">
<xsl:value-of select="regex-group(1)"/>
</term>
<xsl:text> </xsl:text>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
下面是事与愿违的结果:
<page>
<p>1. Using a whip, <term keyref="beat">beat</term> egg whites..</p>
<p>2. Add sugar; <term keyref="beat">beat</term> on medium.</p>
<p>3. <term keyref="beat">Beat</term> at high speed until..</p>
</page>
理想情况下,你只希望在任期的第一次出现的链接该页面(本例中为第1行)。
我所有解决这个问题的尝试都很复杂且不成功。通常使用XSLT,解决方案非常简单。但目前它躲过了我。
UPDATE:这似乎是工作(我还没有对所有使用情况虽然测试(类似于蒂姆·C的)
<xsl:template match="page/p/text()">
<xsl:choose>
<!--
count how times the word has occurred up to now,
if 0 times, wrap it in <term>
-->
<xsl:when test="count(tokenize(preceding::*[1]/text(), 'beat')) = 0">
<xsl:analyze-string select="." regex="\s(beat)\s" flags="i">
<xsl:matching-substring>
<xsl:text> </xsl:text>
<term keref="beat">
<xsl:value-of select="regex-group(1)"/>
</term>
<xsl:text> </xsl:text>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:when>
<!-- the word previously occurred; leave it as is -->
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
UPDATE:我用迈克尔·凯的建议,并简化这是通过两遍和一遍来完成的。多遍是非常正确的选择,因为正则表达式变得非常复杂。为了防止术语链接被插入到不适当的地方,必须使用i
标志来启用Java语言,然后用了很多预见's。后视会有帮助,但我无法弄清楚如何让它们工作。正则表达式语法中的<
似乎会让事情发生。
哦,那可能是票。使用简单模板进行两次传递,而不是使用复杂模板进行一次传递,对其具有吸引力。 – Paulb