【发布时间】:2009-12-18 14:04:31
【问题描述】:
这个问题的措辞不好,对此感到抱歉。将尝试解释我正在尝试做的事情。 基本上,我将搜索的输出作为 Xml,在该 Xml 中有一个像这样的节点:
<FIELD NAME="body">
Somebody named
<key>Doris</key>
and
<key>Arnie</key>
</FIELD>
简而言之,我需要将“
现在我有一些疯狂的代码可以提取搜索词(“Doris”)前面的任何内容,但仅适用于 1 个搜索词。我们需要它来为多个术语执行此操作。我们使用的代码如下所示:
<xsl:template name="highlighter">
<xsl:param name="text"/>
<xsl:param name="what"/>
<xsl:choose>
<xsl:when test="contains($text, $what) and string-length($what) > 0">
<xsl:variable name="before" select="substring-before($text, $what)"/>
<xsl:variable name="after" select="substring-after($text, $what)"/>
<xsl:variable name="real-before" select="substring($text, 1, string-length($before))"/>
<xsl:variable name="real-what" select="substring($text, string-length($before) + 1, string-length($what))"/>
<xsl:variable name="real-after" select="substring($text, string-length($before) + string-length($what) + 1)"/>
<xsl:value-of select="$real-before"/>
<strong>
<xsl:value-of select="$real-what"/>
</strong>
<xsl:call-template name="highlighter">
<xsl:with-param name="text" select="$real-after"/>
<xsl:with-param name="what" select="$what"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
我一直在尝试使用不同的搜索词多次调用此代码,但我正在努力研究如何将调用模板的输出用作下一次调用的输入。在代码中会是这样的:
string body = doc.SelectSingleNode("FIELD[@NAME='body']");
NodeCollection nodes = doc.SelectNodes("FIELD[@NAME='body']/key");
foreach (var node in nodes) {
body = hightlighter(body, node.InnerText);
}
到目前为止,我一直无法在 XSLT 中做这样的事情,但我还是个菜鸟,所以...... ;)
编辑:只是为了澄清;我正在寻找的输出是这样的:
Somebody named <strong>Doris</strong> and <strong>Arnie</strong>
【问题讨论】: