469,579 Members | 1,181 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

Replace hithighlight tags

Hi all,

Give an xml document that looks something like this:

<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl"
href="http://localhost/archiefassistent/xsl/fulldoc.xsl"?>
<result>
<currentpage>1</currentpage>
<document id="5">
<fulltext>This module is the <searchhit>third</searchhit> in the
OPPS series. More text...</fulltext>
</document>
</result>

I'm transforming this document to html to display in a browser. The xml
document is a searchresult and the <searchhit></searchhit> tags
surround the terms that were searched for. Now I'd like to transform
the <searchhit> and </searchhit> tags into something like <span
style="background: red;"> and </span>, to highlight the searchterms in
the resultview.

Is this possible? And if yes, could you give me some pointers as to
how?
Thanks.

Oct 4 '05 #1
8 1383
Peter van Schie wrote:
Hi all,

Give an xml document that looks something like this:

<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl"
href="http://localhost/archiefassistent/xsl/fulldoc.xsl"?>
<result>
<currentpage>1</currentpage>
<document id="5">
<fulltext>This module is the <searchhit>third</searchhit> in the
OPPS series. More text...</fulltext>
</document>
</result>

I'm transforming this document to html to display in a browser. The xml
document is a searchresult and the <searchhit></searchhit> tags
surround the terms that were searched for. Now I'd like to transform
the <searchhit> and </searchhit> tags into something like <span
style="background: red;"> and </span>, to highlight the searchterms in
the resultview.

Is this possible? And if yes, could you give me some pointers as to
how?


Yes, it's possible. Presumably you are parsing the XML to convert it to
a DOM document fragment. When you get to a searchhit element, insert a
span with the required attributes.

If you show how you are parsing the XML, you may get more help...
--
Rob
Oct 7 '05 #2
Peter van Schie wrote:
I'm transforming this document to html to display in a browser.
Try some XSLT
<xsl:template match="searchhit" mode="search-results-with-highlight" >
<span style="background: red; padding: 0.125em 0.5em;"<xsl:apply-templates mode="search-results-with-highlight" /></span>

</xsl:template>

<xsl:template match="*" mode="search-results-with-highlight" >
<!--
This is needed in case of more element structure within <fulltext>, so
as to preserve the mode
It might even copy some elements (embedded HTML?) to the output
-->
<xsl:apply-templates mode="search-results-with-highlight" />
</xsl:template>
[...]

<xsl:apply-templates select="./document/fulltext"
mode="search-results-with-highlight" />

Oct 7 '05 #3
Hi Rob and Andy,

Thank you both for the reply.
I tried Andy's template, but it won't do what I want still. The
relevant part of my xslt looks like this:

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" cdata-section-elements="fulltext" />
<xsl:template match="/">

[... html header stuff ...]

<xsl:if test="/result/document/fulltext!=''">
<tr>
<td class="label">Text</td>
<td class="label">:</td>
<td class="fulldoccontent">
<xsl:apply-templates select="/result/document/fulltext"
mode="search-results-with-highlight" />

</td>
</tr>
</xsl:if>

[... html footer stuff ...]
</xsl:template>

<xsl:template match="searchhit" mode="search-results-with-highlight" >
<span style="background: red; padding: 0.125em 0.5em;">
<xsl:apply-templates mode="search-results-with-highlight" /></span>
</xsl:template>

<xsl:template match="*" mode="search-results-with-highlight" >
<!--
This is needed in case of more element structure within <fulltext>, so
as to preserve the mode
It might even copy some elements (embedded HTML?) to the output
-->
<xsl:apply-templates mode="search-results-with-highlight" />
</xsl:template>

</xsl:stylesheet>

I think the problem is that I insert the <searchhit> and </searchhit>
tags from PHP. So those tags are treated as CDATA within the fulltext
element. Could this be the case?
Thanks.

Kind regards,
Peter.

Oct 7 '05 #4
Edit: Sorry I forgot to mention what the output is and what I want it
to be.
In the output the <searchhit> tags are being output literally as text.

Oct 7 '05 #5
Ixa
> I'm transforming this document to html to display in a browser.

Maybe I've missed the point, but I think this should not be that hard
with XSLT:
<xsl:apply-templates select="/result/document/fulltext"
mode="search-results-with-highlight" />
Just apply without modes ...

---8<---8<---
<xsl:apply-templates select="/result/document/fulltext"/>
---8<---8<---
<xsl:template match="searchhit" mode="search-results-with-highlight" >
<span style="background: red; padding: 0.125em 0.5em;">
<xsl:apply-templates mode="search-results-with-highlight" /></span>
</xsl:template>


.... and wrap <searchhit> with <span> ...

---8<---8<---
<xsl:template match="searchhit">
<span style="background: red; padding: 0.125em 0.5em;">
<xsl:apply-templates/>
</span>
</xsl:template>
---8<---8<---

--
Ixa

Oct 8 '05 #6
Hi Ixa,

Thanks for the suggestion.
But I think: <xsl:template match="searchhit">
won't work because of <searchhit> and </searchhit> being CDATA within
the fulltext element.
I think I need some sort of replacement template to replace the
<searchhit> tags.
I've already been experimenting with it, but didn't reach the goal yet.
I can replace the opening <searchhit> tags now, but when I store the
result of that replacement in a variable and then use that variable as
the input of the replacement of the closing tags it won't work anymore.

Here's what I tried:

================================================== ======
<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />

<xsl:template match="/result/document/fulltext">

<xsl:variable name="openTags">
<xsl:call-template name="replace">
<xsl:with-param name="text" select="." />
<xsl:with-param name="from" select="'&lt;searchhit&gt;'"/>
<xsl:with-param name="with" select="'&lt;span
style="background: red;"&gt;'"/>
</xsl:call-template>
</xsl:variable>

<xsl:call-template name="replace">
<xsl:with-param name="text"><xsl:copy-of
select="$openTags"/></xsl:with-param>
<xsl:with-param name="from" select="'&lt;/searchhit&gt;'"/>
<xsl:with-param name="with" select="'&lt;/span&gt;'"/>
</xsl:call-template>

</xsl:template>

<xsl:template name="replace">
<xsl:param name="text"/>
<xsl:param name="from"/>
<xsl:param name="with"/>

<xsl:choose>
<xsl:when test="$from and contains($text,$from)">

<xsl:value-of select="substring-before($text,$from)"/>
<xsl:value-of disable-output-escaping="yes" select="$with"/>

<xsl:call-template name="replace">
<xsl:with-param name="text"
select="substring-after($text,$from)"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="with" select="$with"/>
</xsl:call-template>

</xsl:when>
<xsl:otherwise>

<xsl:value-of select="$text"/>

</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

================================================== ======

The output of this is that the <searchhit> tags get replaced by
<span style="background: red;"> and the closing </searchhit> tags get
replaced by: &lt;/searchhit&gt;
So in the output I get: <span style="background:
red;">mixing&lt;/searchhit&gt;

However, when I leave out the second call to the replace template and
just do this:
================================================== ====
<xsl:variable name="openTags">
<xsl:call-template name="replace">
<xsl:with-param name="text" select="." />
<xsl:with-param name="from" select="'&lt;searchhit&gt;'"/>
<xsl:with-param name="with" select="'&lt;span
style="background: red;"&gt;'"/>
</xsl:call-template>
</xsl:variable>

<xsl:copy-of select="$openTags"/>
================================================== ====

The <span background: "red";> tags are being parsed by the browser and
everything after it gets a red background.

Any ideas?
Thanks.

Kind regards,
Peter.

Oct 8 '05 #7
Ixa
> But I think: <xsl:template match="searchhit"> won't work because of
<searchhit> and </searchhit> being CDATA within the fulltext element.
Right, so you have something like:

---8<---8<---
<fulltext><![CDATA[This module is the <searchhit>third</searchhit> in the
OPPS series. More text...]]></fulltext>
---8<---8<---
I think I need some sort of replacement template to replace the
<searchhit> tags.


Would this kind of template do the trick?

---8<---8<---
<xsl:template name="wrapper">
<xsl:param name="content"/>
<xsl:choose>
<xsl:when test="contains($content, '&lt;searchhit&gt;')">
<xsl:value-of select="substring-before($content,
'&lt;searchhit&gt;')"/>
<span style="background: red; padding: 0.125em 0.5em;">
<xsl:value-of
select="substring-before(substring-after($content,
'&lt;searchhit&gt;'), '&lt;/searchhit&gt;')"/>
</span>
<xsl:call-template name="wrapper">
<xsl:with-param name="content"><xsl:value-of
select="substring-after($content,
'&lt;/searchhit&gt;')"/></xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$content"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="/result/document/fulltext">
<xsl:call-template name="wrapper">
<xsl:with-param name="content"><xsl:value-of
select="text()"/></xsl:with-param>
</xsl:call-template>
</xsl:template>
---8<---8<---

--
Ixa

Oct 8 '05 #8
Hi Ixa,

Your template works indeed! You have no idea how grateful I am. This
took me hours of hairpulling to solve.
Thanks a lot!

Kind regards,
Peter.

Oct 8 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by higabe | last post: by
4 posts views Thread by Hardy Wang | last post: by
3 posts views Thread by DDK | last post: by
12 posts views Thread by Charlie King | last post: by
5 posts views Thread by nithicorent | last post: by
3 posts views Thread by Hvid Hat | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.