xsl-list
[Top] [All Lists]

Re: Angle brackets and string manipulation

2003-03-09 22:45:08

Basic problem is that I had to resort to the always infamous
disable-output-escaping to substitute a <br /> for a marker character(s)
in a text() node string.  Did I happen upon an actual valid use of
d-o-e, or am I missing better way to do this?

There is a better way.


<xsl:template match="text()">

  <!-- use a variable to allow additional substring substitions
       not shown to simplify -->
  <xsl:variable name="t1">
    <xsl:call-template name="insert_breaks">
      <xsl:with-param name="source_string" select="."/>
      <xsl:with-param name="marker" select="'!br;'"/>
    </xsl:call-template>
  </xsl:variable>

The "insert_breaks" template could create <br /> ***nodes*** -- not simply
insert text.


  <xsl:value-of disable-output-escaping="yes" select="$t1"/>

Because the result is immediately copied, the above instruction is not
necessary at all -- simply remove it. Also do not define the xsl:variable
"t1", but simply call the template to produce its result.

     <xsl:call-template name="insert_breaks">
       <xsl:with-param name="source_string" select="."/>
       <xsl:with-param name="marker" select="'!br;'"/>
     </xsl:call-template>


<xsl:template name="insert_breaks">

  <xsl:param name="source_string" select="."/>
  <xsl:param name="marker"/>

  <!-- If $marker is in the $source_string -->
  <xsl:if test="contains($source_string,$marker)">
    <!-- then replace it with "&lt;br /&gt;" and look for another -->
    <xsl:call-template name="insert_breaks">
      <xsl:with-param name="source_string">
        <!-- make the substitution by taking everything before the
             marker, insert the br element, and finish with everything
             after the break marker -->
        <xsl:value-of select="substring-before( $source_string,
                                                $marker )"/>
        <xsl:text>&lt;br /&gt;</xsl:text>
        <xsl:value-of select="substring-after( $source_string,
                                               $marker )"/>
      </xsl:with-param>
      <xsl:with-param name="marker" select="$marker"/>
    </xsl:call-template>
  </xsl:if>

This is creating just text -- should create a sequence of text nodes and
"br" elements:

   <xsl:if test="contains($source_string,$marker)">
    <xsl:value-of select="substring-before( $source_string, $marker )"/>
    <br />
     <xsl:call-template name="insert_breaks">
       <xsl:with-param name="source_string" select="substring-after(
$source_string, $marker )"/">
       <xsl:with-param name="marker" select="$marker"/>
     </xsl:call-template>


  <!-- terminating case where the $marker is not found in the
       source string -->
  <xsl:if test="not(contains($source_string,$marker))">
    <xsl:value-of select="$source_string"/>
  </xsl:if>

</xsl:template>


Hope this helped.



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL





 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>