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: Word Wrap and PRE


Here's an example from my soon-to-be-released book 
"Building Oracle XML Applications" where we work
through building an XSLT-powered online discussion forum
application. I use a combination of the following two 
templates to format code listings in the application.

You can use the routines by importing "UtilText.xsl" into
your stylesheet. Then, presuming your code example is selected by
the expression "programlisting", you could use this
with the code:

       <span style="font-family: monospace">
         <xsl:call-template name="br-replace">
           <xsl:with-param name="text">
             <xsl:call-template name="sp-replace">
               <xsl:with-param select="programlisting"/>
             </xsl:call-template>
           </xsl:with-param>
         </xsl:call-template>
       </span>

the inner call-template replaces occurrences of pairs of 
consecutive spaces with a pair of non-breaking spaces
represented by &#160;. The outer call-template replaces
line breaks with a <br/> tag.

<!-- UtilText.xsl: Common text formatting routines -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <!-- 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 a carriage return... -->
      <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>
</xsl:stylesheet>

______________________________________________________________
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/

----- Original Message ----- 
From: "Richard Saunders" <richards@greenstone.com.au>
To: <XSL-List@mulberrytech.com>
Sent: Tuesday, September 26, 2000 5:56 PM
Subject: Word Wrap and PRE


| 
| I have a block of text from XML I wish to display on a web page. I wish to
| keep the same carriage returns and line spaces as the in the block of text
| in the XML. This I can do by using <PRE></PRE> in the XSL. However, the
| space I have on the HTML page is only 300 pix wide and <PRE> simply over
| runs this. How can I have the text maintain its format and word wrap at the
| same time?
| 
| Thanks
| 
| 
| Richard 
| 
| 
| 
|  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
| 


 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]