473,414 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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 1518
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Marco Leist | last post by:
hello, i´d like to replace certain strings in a XML-Document with other ones. Thereby the structure (tags)of the source-xml should be preserved completly, the strings that should be replaced are...
4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
4
by: Hardy Wang | last post by:
Hi: I have a XML like <?xml version="1.0" ?> <object> <comments>www.site.com/page.aspx?param1=value1&param2=value2</comments> </object> Since "&" is invalid in XML, I need to replace all "&"...
3
by: DDK | last post by:
I am trying to figure out how to Replace tags such as ... with the correct HTML <b>...</b> tags in C#. The code below works however only if one set of tags are found, if you have more than two...
12
by: Charlie King | last post by:
As I understand it, the use of FIR* to replace heading tags with images in visually enabled CSS browsers is now frowned upon on the basis that some screen readers will *nor* read back text that is...
3
by: MB | last post by:
I need to replace all occurances of a certain character located between two tags. I have included an example of what i have come up with so far, but it doesn't work they way I want it to: str =...
5
by: nithicorent | last post by:
Hi, How to replace a tag by space. Example: var str="<b>abcd</b>"; i need a str with "abcd"; replace <band </bby ""
3
by: Hvid Hat | last post by:
Hi I want to highlight (make it bold) a word in some text I'm getting in XML format. My plan was to replace the word with a bold (or span) tag with the word within the tag. I've found the code...
9
Jezternz
by: Jezternz | last post by:
Okay! I want to make a function where you can input bascly the body of an html page. So basicly formatted html. Then I want my function to go through it and add some code to the Open Tags Only...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.