这个问题措辞不好,对不起。我会尽力解释我想做什么。
基本上,我的搜索结果是XML,在XML中有一个像这样的节点:
<FIELD NAME="body">
Somebody named
<key>Doris</key>
and
<key>Arnie</key>
</FIELD>
简而言之,我需要将“<key>”替换为“<strong>”,即突出显示搜索结果(关键节点值是用户搜索的结果)。在XSLT中,除了查询xml->字段[@name='body']/key之外,我不知道用户从中搜索了什么。
现在我有一些疯狂的代码可以提取搜索词(“Doris”)前面的任何内容,但是这个ony只适用于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>