[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Substituting substrings in an element's text for H

Subject: Re: Substituting substrings in an element's text for HTML output
From: "Steve Muench" <Steve.Muench@xxxxxxxxxx>
Date: Wed, 27 Dec 2000 09:45:26 -0800
br replace
| 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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.