Bad wording on the question, sorry for that. I will try to explain what I'm trying to do. Basically I have a search result like Xml, and in this Xml there is a node like this:
<FIELD NAME="body">
Somebody named
<key>Doris</key>
and
<key>Arnie</key>
</FIELD>
In short, I need to replace "<key>" with "<strong>"; i.e. highlight the search queries (key node values ββare what the user was looking for). In Xslt, I donβt know what the user was looking for, except for the Xml β FIELD [@ name = 'body'] / key request.
Right now I have some kind of crazy code that will extract everything that is in front of the search term ("Doris"), but this works for 1 search term. We need to do this for several terms. The code we use looks like this:
<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>