This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Substituting substrings in an element's text for HTML output


| I have an element that looks like this:
| 
| <xyz>
|   this is the first line
|   this is the second line
|   this is the third line
| </xyz>
| 
| I'd like to transform it so that it could be outputted to html with
| the same line breaks, i.e. change all \n to <br />

Here are a couple of templates I use for this
purpose. The "br-replace" replaces carriage returns
with <br/> tags. The "sp-replace" replaces *pairs*
of leading spaces with *pairs* of leading non-breaking
spaces. By combining the two in series, you can achieve
the affect of keeping code listings (e.g. XML or Java
source code examples in a book) properly formatted without
using the <pre> tag which tends to mess up the formatting
of table cells (often pushing them wider than you'd like).

To use the templates, add them (or import them) into 
the stylesheet you're building, then at the right
moment, just do:

    <!-- Call the "br-replace" template -->
    <xsl:call-template name="br-replace">

      <!-- 
       | Passing the result of calling the sp-replace template
       | as the value of the parameter named "text"
       +-->
      <xsl:with-param name="text">

        <!-- Call the "sp-replace" template -->
        <xsl:call-template name="sp-replace">

          <!-- Passing the value of the current node -->
          <xsl:with-param name="text" select="."/>
        </xsl:call-template>
      </xsl:with-param>
    </xsl:call-template>

Here are the templates...

  <!-- Replace new lines with html <br> tags -->
  <xsl:template name="br-replace">
    <xsl:param name="text"/>
    <xsl:variable name="cr" select="'&#xa;'"/>
    <xsl:choose>
      <!-- If the value of the $text parameter contains carriage ret -->
      <xsl:when test="contains($text,$cr)">
        <!-- Return the substring of $text before the carriage return -->
        <xsl:value-of select="substring-before($text,$cr)"/>
        <!-- And construct a <br/> element -->
        <br/>
        <!--
         | Then invoke this same br-replace template again, passing the
         | substring *after* the carriage return as the new "$text" to
         | consider for replacement
         +-->
        <xsl:call-template name="br-replace">
          <xsl:with-param name="text" select="substring-after($text,$cr)"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
   </xsl:choose>
  </xsl:template>

  <!-- Replace two consecutive spaces w/ 2 non-breaking spaces -->
  <xsl:template name="sp-replace">
    <xsl:param name="text"/>
    <!-- NOTE: There are two spaces   ** here below -->
    <xsl:variable name="sp"><xsl:text>  </xsl:text></xsl:variable>
    <xsl:choose>
      <xsl:when test="contains($text,$sp)">
        <xsl:value-of select="substring-before($text,$sp)"/>
        <xsl:text>&#160;&#160;</xsl:text>
        <xsl:call-template name="sp-replace">
          <xsl:with-param name="text" select="substring-after($text,$sp)"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$text"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
 
______________________________________________________________
Steve Muench, Lead XML Evangelist & Consulting Product Manager
BC4J & XSQL Servlet Development Teams, Oracle Rep to XSL WG
Author "Building Oracle XML Applications", O'Reilly
http://www.oreilly.com/catalog/orxmlapp/



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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]