473,624 Members | 2,119 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Replace String

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 below and it works fine as long
as I'm not adding tags around the to parameter. Can anyone explain to me
why it doesn't work with tags? And it needs to be XSLT 1.0.

This works: X<xsl:value-of select="'little steak'"/>X
This doesn't work: <b><xsl:value-of select="'little steak'"/></b>

And the code:

<?xml version="1.0"?>
<xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform" version="1.0">

<!-- reusable replace-string function -->
<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="from"/>
<xsl:param name="to"/>

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

<xsl:variable name="before" select="substri ng-before($text, $from)"/>
<xsl:variable name="after" select="substri ng-after($text, $from)"/>
<xsl:variable name="prefix" select="concat( $before, $to)"/>

<xsl:value-of select="$before "/>
<xsl:value-of select="$to"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$after"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwis e>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!-- test the function -->
<xsl:template match="/">
<xsl:call-template name="replace-string">
<xsl:with-param name="text"
select="'Mary had a little lamb, little lamb, little lamb.'"/>
<xsl:with-param name="from" select="'little lamb'"/>
<xsl:with-param name="to">
<!-- X<xsl:value-of select="'little steak'"/>X -->
<b><xsl:value-of select="'little steak'"/></b>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
Feb 13 '08 #1
3 3950
Martin Honnen wrote:
Hvid Hat wrote:
>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 below and it works
fine as long as I'm not adding tags around the to parameter. Can
anyone explain to me why it doesn't work with tags? And it needs to be
XSLT 1.0.

You are using <xsl:value-of select="$to"/>, that outputs a text node
with the string value, it does not create elements.
If you use xsl:copy-of as in

<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="from"/>
<xsl:param name="to"/>

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

<xsl:variable name="before" select="substri ng-before($text, $from)"/>
<xsl:variable name="after" select="substri ng-after($text, $from)"/>
<xsl:variable name="prefix" select="concat( $before, $to)"/>

<xsl:value-of select="$before "/>
<xsl:copy-of select="$to"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$after"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:when <xsl:otherwis e>
<xsl:value-of select="$text"/ </xsl:otherwise>
</xsl:choose </xsl:template>

then I think you get what you want.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Feb 13 '08 #2
Thanks. Both your suggestions worked. Now I'm working on making the search
case-insensitive. Is there an easy way? Or can you guide me in the right
direction?

Hello Martin,
Martin Honnen wrote:
>Hvid Hat wrote:
>>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 below and it
works fine as long as I'm not adding tags around the to parameter.
Can anyone explain to me why it doesn't work with tags? And it needs
to be XSLT 1.0.
You are using <xsl:value-of select="$to"/>, that outputs a text node
with the string value, it does not create elements.
If you use xsl:copy-of as in

<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="from"/>
<xsl:param name="to"/>
<xsl:choose>
<xsl:when test="contains( $text, $from)">
<xsl:variable name="before" select="substri ng-before($text,
$from)"/>
<xsl:variable name="after" select="substri ng-after($text,
$from)"/>
<xsl:variable name="prefix" select="concat( $before, $to)"/>
<xsl:value-of select="$before "/>
<xsl:copy-of select="$to"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$after"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:when <xsl:otherwis e>
<xsl:value-of select="$text"/ </xsl:otherwise>
</xsl:choose </xsl:template>
then I think you get what you want.

Feb 13 '08 #3
Hello Hvid,

Ok. Now I can replace the search string no matter what case it's in. Unfortunately
I haven't found a way to keep the original case of the search sting in the
text. Anyone? Here's my code:

<xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:variable name="lcletters ">abcdefghijklm nopqrstuvwxyz</xsl:variable>
<xsl:variable name="ucletters ">ABCDEFGHIJKLM NOPQRSTUVWXYZ</xsl:variable>

<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="from"/>
<xsl:param name="to"/>
<xsl:choose>
<xsl:when test="contains( translate($text , $ucletters, $lcletters), translate($from ,
$ucletters, $lcletters))">
<xsl:variable name="before" select="substri ng-before(translat e($text,
$ucletters, $lcletters), translate($from , $ucletters, $lcletters))"/>
<xsl:variable name="after" select="substri ng-after(translate ($text, $ucletters,
$lcletters), translate($from , $ucletters, $lcletters))"/>
<xsl:variable name="prefix" select="concat( $before, $to)"/>
<xsl:value-of select="$before "/>
<xsl:copy-of select="$to"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$after"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwis e>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="/">
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="'Mary had a little lamb, LITTLE LAMB,
x little lamb.'"/>
<xsl:with-param name="from" select="'little lamb'"/>
<xsl:with-param name="to">
<!-- X<xsl:value-of select="'little steak'"/>X -->
<b>
<xsl:value-of select="'little steak'"/>
</b>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>

Thanks. Both your suggestions worked. Now I'm working on making the
search case-insensitive. Is there an easy way? Or can you guide me in
the right direction?

Hello Martin,
>Martin Honnen wrote:
>>Hvid Hat wrote:

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 below
and it works fine as long as I'm not adding tags around the to
parameter. Can anyone explain to me why it doesn't work with tags?
And it needs to be XSLT 1.0.

You are using <xsl:value-of select="$to"/>, that outputs a text node
with the string value, it does not create elements.
If you use xsl:copy-of as in

<xsl:templat e name="replace-string">
<xsl:param name="text"/>
<xsl:param name="from"/>
<xsl:param name="to"/>
<xsl:choose>
<xsl:when test="contains( $text, $from)">
<xsl:variabl e name="before" select="substri ng-before($text,
$from)"/>
<xsl:variabl e name="after" select="substri ng-after($text,
$from)"/>
<xsl:variabl e name="prefix" select="concat( $before, $to)"/>
<xsl:value-of select="$before "/>
<xsl:copy-of select="$to"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$after"/>
<xsl:with-param name="from" select="$from"/>
<xsl:with-param name="to" select="$to"/>
</xsl:call-template>
</xsl:when <xsl:otherwis e>
<xsl:value-of select="$text"/ </xsl:otherwise>
</xsl:choose </xsl:template>
then I think you get what you want.

Feb 13 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
62095
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 supposed to write this function? String.replace(/</g,'&lt;');
13
11007
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code below would work... string gsub(const string & sData, const string & sFrom,
22
11109
by: Phlip | last post by:
C++ers: Here's an open ended STL question. What's the smarmiest most templated way to use <string>, <algorithms> etc. to turn this: " able search baker search charlie " into this: " able replace baker replace charlie "
9
2149
by: Crirus | last post by:
dim pp as string pp="{X=356, Y=256}{X=356, Y=311.2285}{X=311.2285, Y=356}{X=256, Y=356}{X=200.7715, Y=356}{X=156, Y=311.2285}{X=156, Y=256}{X=156, Y=200.7715}{X=200.7715, Y=156}{X=256, Y=156}{X=311.2285, Y=156}{X=356, Y=200.7715}{X=356, Y=256}{X=200, Y=150}{X=200, Y=177.6142}{X=177.6142, Y=200}{X=150, Y=200}{X=122.3858, Y=200}{X=100, Y=177.6142}{X=100, Y=150}{X=100, Y=122.3858}{X=122.3858, Y=100}{X=150, Y=100}{X=177.6142, Y=100}{X=200,...
9
9144
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a StringBuilder. At present I am porting a VB6 webclass app to VB.NET and therefore I am trying to make it as efficent as possible via the new functionality of VB.NET.
4
3838
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said, that the split method and the regex.replace method was better than the string.replace method and replace function. I did not believe that.
5
2435
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able to replace TAB's with "&nbsp;"'s to preserve the user's indentation. My questions are: 1) even though I'll probably look it up before I get a reply.... whats the vb code for TAB character? 2) more importantly; do I have to run this large field...
10
9588
by: pamelafluente | last post by:
I need to replace all the occurences of a string within another string (or stringbuilder): Function ReplaceInsensitive(ByVal InputString As String, _ ByVal SubstringReplaced As String, _ ByVal Replacement As String) As String '... End Function
5
9247
by: V S Rawat | last post by:
I was trying to use back-to-back replace functions to convert a url: str1 = str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace("%2 6","&"); It didn't replace all 4 types of strings. Then, I googled and found this suggestion of some JavaScript Tutorials, so I used replace with a regex with a global switch:
1
3386
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "": --------------------------------- myStringVar = myStringVar.replace("^" + iName + "=,($|&)", ""); --------------------------------- The following DOES replace it though: --------------------------------- var match = myStringVar.match("^" + iName + "=,($|&)");
0
8234
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8620
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6110
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4079
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.