473,225 Members | 1,283 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,225 software developers and data experts.

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 4731
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
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
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
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
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: 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
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
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
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:...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.