473,588 Members | 2,471 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 4762
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,$re pl),$target,sub string-after($str,$re
pl))"/>
<xsl:with-param name="repl" select="$repl"/>
<xsl:with-param name="target" select="$target "/>
</xsl:call-template>
</xsl:when>
<xsl:otherwis e>
<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:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="ye s"/>

<!-- 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="substri ng-before($text,$f rom)"/>
<xsl:value-of disable-output-escaping="yes" select="$with"/>

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

</xsl:when>
<xsl:otherwis e>

<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:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="ye s"/>

<!-- 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="substri ng-before($text,$f rom)"/>
<!-- here we create the new element -->
<xsl:element name="{$element }"/>

<xsl:call-template name="replace-element">
<xsl:with-param name="text" select="substri ng-after($text,$fr om)"/>
<xsl:with-param name="element" select="$elemen t"/>
</xsl:call-template>

</xsl:when>
<xsl:otherwis e>

<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,$re pl),$target,sub string-after($str,$re
pl))"/>
<xsl:with-param name="repl" select="$repl"/>
<xsl:with-param name="target" select="$target "/>
</xsl:call-template>
</xsl:when>
<xsl:otherwis e>
<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
12147
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 you and see if there are any improvments that i should make ;-) It should be fast and if possible compatible with todays modern browser-standards. It should be activated by the onload-event. This is my code, free for all to use:
9
1944
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
16082
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 characters with <br>.
12
2555
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 marked up by CSS to be invisible or hidden. So... If I want to replace headings with images (because my client wants to use his specific font for headings where possible), what's the best way to do it?
3
2476
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 code below it replace the <, and > with &lt; and &gt;. Thus it replaces IBM with: &lt;acronym title="International Business Machines"&gt;IBM&lt;/acronym&gt;
8
10015
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 content in a .xml document and then making a stylesheet for it. At the moment I'm working on a lot of prose-style text, and having to enclose everything in double-sided <xyz></xyz> tags in order to get any
4
18708
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 title</span> <br/> <span>link: the link</span> </div>
3
3183
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: https://bugzilla.mozilla.org/show_bug.cgi?id=316063 Boris suggests doing something else to get what I believe most users are really after, but doesn't say what. Here's a possible approach, with initial
0
8357
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...
1
7987
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6634
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
5729
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
5398
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
3847
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...
1
2372
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
1
1459
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1196
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.