我需要在 xml 中搜索一个单词及其上下文。例如
<line>hello world, my name is farhad and i'm having trouble with xslt</line>
寻找“和”,三个词的上下文:
<line>hello world, my <span class="context">name is farhad <span class="word">and</span> i'm having</span> trouble with xslt</line>
我能怎么做?我写了一些 xslt 来查找单词,但我无法返回 3 个单词来设置跨度。这是我的 xslt:
<xsl:variable name="delimiters">[,.;!?\s"()]+</xsl:variable>
<xsl:template match="/">
<xsl:apply-templates select="//line"/>
</xsl:template>
<xsl:template match="line">
<line>
<xsl:for-each select="tokenize(.,'\s')">
<xsl:choose>
<!-- se l'ultimo carattere è di punteggiatura, prendo la sottostringa senza la punteggiatura -->
<xsl:when test="compare(replace(.,$delimiters,'$1'),'red') = 0">
<span class="word">
<xsl:value-of select="."/>
</span>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
<xsl:choose>
<xsl:when test="position()=last()">
<xsl:text></xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</line><xsl:text>
</xsl:text>
</xsl:template>
这是一个示例 xml: http: //pastebin.com/eAVM9CDQ。
我还必须在前面的标签上搜索上下文,例如:
<line>hello world,</line>
<line>my name</line>
<line>is farhad </line>
<line>and i'm having</line>
<line>trouble with xslt</line>
所以,寻找“和”,三个词的上下文:
<line>hello world,</line>
<line>my <span class="context">name</line>
<line>is farhad </line>
<line><span class="word">and</span> i'm having</span></line>
<line>trouble with xslt</line>
有重叠的问题,但现在这不是问题(我想我知道如何管理它)。如何搜索单词及其上下文?非常感谢。