472,145 Members | 1,475 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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:stylesheet 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="substring-before($text, $from)"/>
<xsl:variable name="after" select="substring-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:otherwise>
<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 3764
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="substring-before($text, $from)"/>
<xsl:variable name="after" select="substring-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:otherwise>
<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="substring-before($text,
$from)"/>
<xsl:variable name="after" select="substring-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:otherwise>
<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:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
<xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</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="substring-before(translate($text,
$ucletters, $lcletters), translate($from, $ucletters, $lcletters))"/>
<xsl:variable name="after" select="substring-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:otherwise>
<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: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="substring-before($text,
$from)"/>
<xsl:variable name="after" select="substring-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:otherwise>
<xsl:value-of select="$text"/ </xsl:otherwise>
</xsl:choose </xsl:template>
then I think you get what you want.

Feb 13 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by higabe | last post: by
22 posts views Thread by Phlip | last post: by
9 posts views Thread by Crirus | last post: by
5 posts views Thread by djc | last post: by
10 posts views Thread by pamelafluente | last post: by
1 post views Thread by NvrBst | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

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.