By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,414 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,414 IT Pros & Developers. It's quick & easy.

Replace all [br] inside a node

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.