Replace all [br] inside a node

Joined
May 13, 2004
Messages
4
Reaction score
0
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....
 

Alex Shirshov

Usenet Archive
Joined
Feb 25, 2004
Messages
19
Reaction score
0
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:eek:therwise> <xsl:value-of select="$str"/> </xsl:eek:therwise> </xsl:choose> </xsl:template> [/xslt]

With best regards, Alex Shirshov.
 

Ben Edgington

Usenet Archive
Joined
Mar 18, 2004
Messages
45
Reaction score
0
Miguel J. Jiménez said:
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:eek:utput 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:eek:therwise>

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

</xsl:eek:therwise>
</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:eek:utput 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:eek:therwise>

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

</xsl:eek:therwise>
</xsl:choose>

</xsl:template>

</xsl:stylesheet>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,042
Messages
3,104,223
Members
248,131
Latest member
Tonypshaw1

Latest Threads

Top