473,506 Members | 17,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Replace all [br] inside a node

Hi, I have the following node:

<node>
Some text[br] here with[br] lots of [br] inside it...
</node>

and I would like it to transfrom it using XSLT to the following:
Some text<br/> here with</br> lots of</br> inside it...

Being <br/> HTML tags and not simple text like &lt;br/&gt;

Any idea? Thanks....

---
Miguel J. Jiménez
mjjimenez AT isotrol DOT com
Jul 20 '05 #1
3 4750
Hello, Miguel!
You wrote on Thu, 13 May 2004 13:35:54 +0200:

MJJ> <node>
MJJ> Some text[br] here with[br] lots of [br] inside it...
MJJ> </node>

MJJ> and I would like it to transfrom it using XSLT to the following:
MJJ> Some text<br/> here with</br> lots of</br> inside it...

MJJ> Being <br/> HTML tags and not simple text like &lt;br/&gt;

MJJ> Any idea? Thanks....

Here is my idea.
[xslt]
<xsl:template match="node">
<xsl:variable name="str">
<xsl:call-template name="replace">
<xsl:with-param name="str" select="."/>
<xsl:with-param name="repl">[br]</xsl:with-param>
<xsl:with-param name="target">&lt;br/></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$str" disable-output-escaping="yes"/>
</xsl:template>

<xsl:template name="replace">
<xsl:param name="str"/>
<xsl:param name="repl"/>
<xsl:param name="target"/>
<xsl:choose>
<xsl:when test="contains($str,$repl)">
<xsl:call-template name="replace">
<xsl:with-param name="str"
select="concat(substring-before($str,$repl),$target,substring-after($str,$re
pl))"/>
<xsl:with-param name="repl" select="$repl"/>
<xsl:with-param name="target" select="$target"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$str"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
[/xslt]

With best regards, Alex Shirshov.
Jul 20 '05 #2
"Miguel J. Jiménez" <no*****@thanks.com> writes:
Hi, I have the following node:

<node>
Some text[br] here with[br] lots of [br] inside it...
</node>

and I would like it to transfrom it using XSLT to the following:
Some text<br/> here with</br> lots of</br> inside it...

Being <br/> HTML tags and not simple text like &lt;br/&gt;


My trusty recursive search-and-replace will do this, but you
have to make judicious use of the disable-output-escaping
attribute:

- - -

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"/>

<!-- The main template -->
<xsl:template match="/">

<xsl:call-template name="replace">
<xsl:with-param name="text" select="node/text()"/>
<xsl:with-param name="from" select="'[br]'"/>
<xsl:with-param name="with" select="'&lt;br/>'"/>
</xsl:call-template>

</xsl:template>

<!-- This is a recursive named template for search and replace. -->
<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>

- - -

A less general purpose template, but one that does not use
disable-output escaping for generating the new element
(which some might consider a nasty thing to do) is as
follows:

- - -

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"/>

<!-- The main template -->
<xsl:template match="/">

<xsl:call-template name="replace-element">
<xsl:with-param name="text" select="node/text()"/>
<xsl:with-param name="element" select="'br'"/>
</xsl:call-template>

</xsl:template>

<!-- Replace [element] with <element/> -->
<xsl:template name="replace-element">

<xsl:param name="text"/>
<xsl:param name="element"/>

<xsl:variable name="from" select="concat('[',$element,']')"/>

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

<xsl:value-of select="substring-before($text,$from)"/>
<!-- here we create the new element -->
<xsl:element name="{$element}"/>

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

</xsl:when>
<xsl:otherwise>

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

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

</xsl:template>

</xsl:stylesheet>

--
Ben Edgington
Mail to the address above is discarded.
Mail to ben at that address might be read.
http://www.edginet.org/
Jul 20 '05 #3
Thanks a lot, it worked fine

---
Miguel J. Jiménez
mjjimenez AT isotrol DOT com
Alex Shirshov wrote:
Here is my idea.
[xslt]
<xsl:template match="node">
<xsl:variable name="str">
<xsl:call-template name="replace">
<xsl:with-param name="str" select="."/>
<xsl:with-param name="repl">[br]</xsl:with-param>
<xsl:with-param name="target">&lt;br/></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$str" disable-output-escaping="yes"/>
</xsl:template>

<xsl:template name="replace">
<xsl:param name="str"/>
<xsl:param name="repl"/>
<xsl:param name="target"/>
<xsl:choose>
<xsl:when test="contains($str,$repl)">
<xsl:call-template name="replace">
<xsl:with-param name="str"
select="concat(substring-before($str,$repl),$target,substring-after($str,$re
pl))"/>
<xsl:with-param name="repl" select="$repl"/>
<xsl:with-param name="target" select="$target"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$str"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
[/xslt]

Jul 20 '05 #4

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

Similar topics

6
12138
by: Lasse | last post by:
I have done this simple function, it seems to work as intended, to solve a problem i have had for a while. I couldnt find any sample around that was working for me. I would like to test it with...
9
1939
by: Bernd.Moos | last post by:
Given the following XML document: <text> <p> <w>Ronaldo</w> <w>scoredw> <w>the</w> <w>1</w> <c>:</c> <w>1</w>
8
16045
by: Guy | last post by:
Hi, I'm trying to run this code : strFileContentsHTML.Replace(vbLf, "<br>") strFileContentsHTML.Replace(vbCrLf, "<br>") strFileContentsHTML.Replace(vbCr, "<br>") It doesn't replace newline...
12
2539
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
2470
by: gregpinero | last post by:
Hi guys, What I'm trying to do is find all instances of an acronymn such as IBM on a webpage and replace it with <acronym title="International Business Machines">IBM</acronym>. However in my...
8
10000
by: tintagel | last post by:
Hi everyone, I've just joined your group! I'm pretty new to HTML etc. Here's my problem: I'm attached to the idea of keeping content separate from formatting information, so I like putting...
4
18700
by: 0m4r | last post by:
Good Morning everybody I'm gonna to be crazy about, maybe, a stupid Javascript problem. What I would like to code is a javascript script to obtain something like this: <div> <span>title: the...
3
3178
by: Csaba Gabor | last post by:
In my Firefox 1.5 (on Win XP Pro) a <BR> element's .textContent returns the empty string. I have no idea why they (W3C) construed this to be reasonable, but it was contested, and denied here:...
0
7308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7371
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
7479
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
5617
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,...
1
5037
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
4702
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...
0
3188
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...
0
1534
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 ...
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.