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

July 22nd, 2003, 12:22 PM
|
Registered User
|
|
Join Date: Jul 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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"
|

July 23rd, 2003, 12:40 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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 "<br>".
Regards,
Armen
|

July 23rd, 2003, 03:47 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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>
|

July 23rd, 2003, 03:51 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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
|

July 23rd, 2003, 09:52 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|

July 29th, 2003, 04:46 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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
|

July 30th, 2003, 12:17 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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)...
|

July 30th, 2003, 03:09 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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
|
|
 |