Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 22nd, 2003, 12:22 PM
Registered User
 
Join Date: Jul 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default problems with line breaks

I have this in my XSL:

  <xsl:value-of select="@detail"/>


"detail" is a String, with vbCrLf's in it. When I try to display it in a browser, the vbCrLf's diseppear. I tried to replace every vbCrLf with a "<br>" (I did this in VB), but didn't work. The result was:
"This is my first paragraph<br>Here's a second paragraph"

What can I do to display my string properly? I mean:
"This is my first paragraph
Here's a second paragraph"
 
Old July 23rd, 2003, 12:40 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

It seems that you have to use "disable-output-escaping" attribute. When it's not specified or has the value "no", the XSLT processor escapes some characters(including "<"). So, you have to prevent the XSLT processor to do it:

Code:
  <xsl:value-of select="@detail" disable-output-escaping="yes"/>
It's assumed of course that in the attribute "detail" you already replaced all vbCrLf with "&lt;br>".

Regards,
Armen
 
Old July 23rd, 2003, 03:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

Instead of replacing the CrLf with <br>, have you tried displaying the text inside <pre> tags? Like this:
Code:
<pre>

  <xsl:value-of select="@detail"/>

</pre>
 
Old July 23rd, 2003, 03:51 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Never use disable-output-escaping if possible, it's a kludge and not always supported.
Two solutions, you could wrap the html text with 'pre' tags to preserve line feeds or use the following xslt. You would need to modify it to suit your source but basically you pass the node you want to change to a template using call-template. E.g. if your source was:
Code:
<root>
<data textWithCR="This is the first line.
This is the second line.
This is the third line."/>
<data textWithCR="This is the fourth line.
This is the fifth line.
This is the sixth line."/>
</root>
XSLT would be:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html"/>
 <xsl:template match="/root">
   <xsl:for-each select="data/@textWithCR">
     <xsl:call-template name="line-breaks">
      <xsl:with-param name="text" select="."/>
     </xsl:call-template>
   </xsl:for-each>  
 </xsl:template>
 <xsl:template name="line-breaks">
  <xsl:param name="text"/>
  <xsl:choose>
   <xsl:when test="contains($text,'#10;')">
    <xsl:value-of select="substring-before($text,'#10;')"/>
    <br/>
    <xsl:call-template name="line-breaks">
     <xsl:with-param name="text" select="substring-after($text,'#10;')"/>
    </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="$text"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>
There might be a problem if the text is very long as it's a recursive approach.

--

Joe
 
Old July 23rd, 2003, 09:52 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Quote:
quote:Originally posted by joefawcett
 Never use disable-output-escaping if possible, it's a kludge and not always supported.
Two solutions, you could wrap the html text with 'pre' tags to preserve line feeds or use the following xslt. You would need to modify it to suit your source but basically you pass the node you want to change to a template using call-template. E.g. if your source was:
Code:
<root>
<data textWithCR="This is the first line.
This is the second line.
This is the third line."/>
<data textWithCR="This is the fourth line.
This is the fifth line.
This is the sixth line."/>
</root>
XSLT would be:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html"/>
 <xsl:template match="/root">
   <xsl:for-each select="data/@textWithCR">
    Ã‚ <xsl:call-template name="line-breaks">
    Ã‚  <xsl:with-param name="text" select="."/>
    Ã‚ </xsl:call-template>
   </xsl:for-each>  
 </xsl:template>
 <xsl:template name="line-breaks">
  <xsl:param name="text"/>
  <xsl:choose>
   <xsl:when test="contains($text,'#10;')">
    <xsl:value-of select="substring-before($text,'#10;')"/>
    <br/>
    <xsl:call-template name="line-breaks">
    Ã‚ <xsl:with-param name="text" select="substring-after($text,'#10;')"/>
    </xsl:call-template>
   </xsl:when>
   <xsl:otherwise>
    <xsl:value-of select="$text"/>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>
There might be a problem if the text is very long as it's a recursive approach.

--

Joe
In this concrete situation I don't see any problem with using DOE. There are circumstances, of course, where it's more desirable to avoid using DOE(to output matching start/end tag pairs, for example, which, by the way, is not a good idea in any case). From sanchezal's example it's clear to me that the obvious and simple solution would be using DOE(the problem was that "This is my first paragraph<br>Here's a second paragraph" displayed instead of 2 lines).
Please note: "<br>", not "<br/>" :)

If DOE is not supported by XSLT processor, then the processor is not conformant to the XSLT specification.

Regards,
Armen
 
Old July 29th, 2003, 04:46 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

http://<br /> <a href="http://www.w...ping</a><br />
states that
Quote:
quote:An XSLT processor is not required to support disabling output escaping
--

Joe
 
Old July 30th, 2003, 12:17 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

Agree, well spotted: an XSLT processor is not required to support d-o-e. By the way, I prefer to express my ideas in a natural language (although sometimes with grammatical errors)...
 
Old July 30th, 2003, 03:09 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I haven't quite mastered the techinique of showing a url, I escaped it with {URL}, in brackets not braces, the board then double escaped it for me...:)

--

Joe





Similar Threads
Thread Thread Starter Forum Replies Last Post
line breaks mister_mister XSLT 14 March 17th, 2008 12:55 PM
How to do line breaks! Apocolypse2005 Beginning VB 6 2 December 11th, 2006 03:20 PM
Line breaks question gkirk Beginning PHP 2 February 27th, 2005 10:40 AM
Want to detect line breaks conundrum Classic ASP Basics 1 October 3rd, 2003 04:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.