xsl-list
[Top] [All Lists]

RE: line breaks in XML data

2005-10-21 05:41:18
I used to have the same problem I think.
Tell me if I'm wrong : what you want is to preserve line breaks on the final
HTML output.
In html, lines breaks in the source are ignored : you need a <br/> element
so that the break can been seen in your browser (only textarea preserve the
source's line breaks ).
Then you need a template which replace every line break characters ("&#xA;")
by a <br/> element.

This couple of templates make this. (I don't remembre who I have it from)

        <xsl:template name="lf2br">
                <!-- import $StringToTransform -->
                <xsl:param name="StringToTransform"/>
                <xsl:choose>
                        <!-- string contains linefeed -->
                        <xsl:when test="contains($StringToTransform,'&#xA;')">
                                <!-- output substring that comes before the 
first linefeed -->
                                <!-- note: use of substring-before() function 
means        -->
                                <!-- $StringToTransform will be treated as a 
string,       -->
                                <!-- even if it is a node-set or result tree 
fragment.     -->
                                <!-- So hopefully $StringToTransform is really 
a string!   -->
                                <xsl:value-of 
select="substring-before($StringToTransform,'&#xA;')"/>
                                <!-- by putting a 'br' element in the result 
tree instead  -->
                                <!-- of the linefeed character, a <br> will be 
output at   -->
                                <!-- that point in the HTML                     
           -->
                                <br/>
                                <!-- repeat for the remainder of the original 
string -->
                                <xsl:call-template name="lf2br">
                                        <xsl:with-param 
name="StringToTransform">
                                                <xsl:value-of 
select="substring-after($StringToTransform,'&#xA;')"/>
                                        </xsl:with-param>
                                </xsl:call-template>
                        </xsl:when>
                        <!-- string does not contain newline, so just output it 
-->
                        <xsl:otherwise>
                                <xsl:value-of select="$StringToTransform"/>
                        </xsl:otherwise>
                </xsl:choose>
        </xsl:template>

I defined another template to call like this :
                        <xsl:call-template name="CopyWithLineBreaks">
                                <xsl:with-param name="string" 
select="some_Xpath"/>
                        </xsl:call-template>

        <xsl:template name="CopyWithLineBreaks">
                <xsl:param name="string"/>
                <xsl:variable name="Result">
                        <xsl:call-template name="lf2br">
                                <xsl:with-param name="StringToTransform" 
select="$string"/>
                        </xsl:call-template>
                </xsl:variable>
                <xsl:copy-of select="$Result"/>
        </xsl:template>

Hope this help !
Matthieu.
-----Message d'origine-----
De : Emmanouil Batsis [mailto:Emmanouil(_dot_)Batsis(_at_)eurodyn(_dot_)com]
Envoyé : vendredi 21 octobre 2005 14:18
À : xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Objet : Re: [xsl] line breaks in XML data


Jim Gay wrote:

forgive my novice ineptitude.

I am working on a project where we would like to preserve line breaks
that are entered into our XML database.


In the root of your XML documents add xml:space="preserve". That should
force the XML parser to keep whitespace.


We are doing our XSL transformations with version 1.
If I use my XSLT to output/display this data into a <textarea>
element, the line breaks found in the database are there when displayed.
If I output to a simple <p> element, the line breaks are gone.


Use a <pre> instead of a <p>. You got me all confused now :-P

Manos



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>