Replace all [br] inside a node

Discussion in 'XML' started by =?ISO-8859-15?Q?=22Miguel_J=2E_Jim=E9nez=22?=, May 13, 2004.

  1. 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
    =?ISO-8859-15?Q?=22Miguel_J=2E_Jim=E9nez=22?=, May 13, 2004
    #1
    1. Advertising

  2. 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.
    Alex Shirshov, May 13, 2004
    #2
    1. Advertising

  3. "Miguel J. Jiménez" <> 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: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>

    --
    Ben Edgington
    Mail to the address above is discarded.
    Mail to ben at that address might be read.
    http://www.edginet.org/
    Ben Edgington, May 13, 2004
    #3
  4. 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:eek:therwise>
    > <xsl:value-of select="$str"/>
    > </xsl:eek:therwise>
    > </xsl:choose>
    > </xsl:template>
    > [/xslt]
    =?ISO-8859-15?Q?=22Miguel_J=2E_Jim=E9nez=22?=, May 13, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,375
  2. Tjerk Wolterink
    Replies:
    2
    Views:
    1,392
    Dimitre Novatchev
    Aug 24, 2006
  3. thecolour
    Replies:
    0
    Views:
    363
    thecolour
    Jun 26, 2007
  4. thecolour
    Replies:
    2
    Views:
    404
    thecolour
    Jun 27, 2007
  5. Geagleeye

    how to creating new node inside a xml node

    Geagleeye, Aug 2, 2006, in forum: ASP General
    Replies:
    2
    Views:
    183
    Geagleeye
    Aug 3, 2006
Loading...

Share This Page