Re: [xsl] Formatting CDATA information

Subject: Re: [xsl] Formatting CDATA information
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Sat, 27 Jan 2001 12:00:43 +0000
Hi Tina,

> The problem is that when this gets converted to HTML all of the line
> breaks in the CDATA section are lost, and it becomes one long line.
> Is there a way to format what's inside the CDATA tag to have <br>
> after each line?

Sure.  The following template uses recursion to go through a string
and substitute all the line breaks (&#xA;) with 'br' elements.  (These
are the default substitutions - you can substitute other things by
passing different values for the $from and $to parameters.)

<xsl:template name="substitute">
   <xsl:param name="string" />
   <xsl:param name="from" select="'&#xA;'" />
   <xsl:param name="to">
      <br />
      <xsl:when test="contains($string, $from)">
         <xsl:value-of select="substring-before($string, $from)" />
         <xsl:copy-of select="$to" />
         <xsl:call-template name="substitute">
            <xsl:with-param name="string"
                            select="substring-after($string, $from)" />
            <xsl:with-param name="from" select="$from" />
            <xsl:with-param name="to" select="$to" />
         <xsl:value-of select="$string" />

So, for your example you can call the above template on the failure
information using:

<xsl:template match="failure">
      <p>FAILURE INFO:</p>
      <xsl:call-template name="substitute">
         <xsl:with-param name="string" select="." />

The other option, of course, is to use the HTML 'pre' element or to
use the CSS white-space property set to 'pre' to tell the browser to
take notice of the whitespace in the HTML that you're producing.
These might be easier to handle than recursing through the string.

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread