1

This is what is displayed in my XML output (note the line breaks)

<root>
<Item type="Comment">
<comment>this is a test 
this is a test 
this is a test</comment>
</Item>
</root>

In my stylesheet

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:user-scripts">
<xsl:output method="html" omit-xml-declaration="yes" standalone="yes" indent="yes" cdata-section-elements="script msxsl:script"/>


<xsl:template match="/">
    <xsl:apply-templates/>
</xsl:template>
<xsl:template match="/Root">
    <xsl:apply-templates select="Item"/>
</xsl:template>
<xsl:template match="Item[@type='Comment']"> 

    <html>
        <head>
            <title>
                Comment: <xsl:value-of select="comment_no"/>
            </title>
        </head>
        <style>
            * {
            font-family: "Arial", sans-serif;
            font-size: 10pt;
            }
            body {
            min-width: 775px;
            max-width: 775px;
            margin-left: auto;
            margin-right: auto;
            }
            td {
            padding: 0px;
            }
            .layoutTable {
            width: 100%;
            }
            .fieldValue {
            width: 99%;
            padding-left: 7px;
            }
            .layoutTable {
            width: 100%;
            }
            div.layoutBin {
            page-break-inside: avoid;
            overflow: hidden;
            border: solid;
            border-width: 1px;
            margin-bottom: 10px;
            padding-left: 3px;
            padding-right: 3px;
            }
        </style>

        <body>
            <div class="layoutBin" id="keyInfo">
                <div class="left" style="width: 25%">
                    <table class="layoutTable">
                        <tr>
                            <td class="fieldValue">
                                <xsl:value-of select="comment"/>
                            </td>
                        </tr>
                    </table>
                </div>    
            </div>
        </body>
    </html>
</xsl:template>
</xsl:stylesheet>

The line breaks are removed and my browser displays the result in a single line instead.

How do I preserve/display the line breaks?

User1218
  • 27
  • 3
  • Please edit your Q to say what xslt engine and version are you using. Good luck. – shellter Dec 29 '15 at 23:27
  • 2
    Please post a **reproducible** example, including a **complete** stylesheet. Line breaks will **not** be removed using your example - but if you are outputting HTML, they will not be rendered by a browser. – michael.hor257k Dec 29 '15 at 23:31
  • Same question was already asked: http://stackoverflow.com/questions/3309746/how-to-convert-newline-into-br-with-xslt – Über Lem Dec 30 '15 at 16:14
  • Can you show me how that can be applied to my example? I am completely lost looking at the suggested solution from that post (total rookie here) – User1218 Dec 30 '15 at 17:25

1 Answers1

0

In HTML, line breaks are not normally significant. They're treated the same way as other white space - turned into a single word space. As such, you can't tell from looking at the page whether there's a literal line break in the DOM.

You have several options:

  1. Use CSS' white-space: pre; or white-space: pre-wrap;. (this is the easiest, given you're already writing your own CSS). Beware: This will also make all the other text you're inserting significant, so you might want to use xsl:text tags.
  2. Process text nodes to insert a (self-closing) HTML br tag in place of line-break characters.
  3. Process text nodes to split them into HTML divs (or paragraphs or similar)

Which you choose depends on what you want to do with the text.

The xsl:preserve-space element might initially appear to be what you're looking for. Put it at the top of your stylesheet with the space-separated list of elements whose whitespace you want preserved verbatim - however it's only necessary if you used xsl:strip-space, which it looks like you haven't.

user52889
  • 1,501
  • 8
  • 16
  • `xsl:strip-space` and `xsl:preserve-space` deal with text nodes that contain **only** whitespace characters - therefore are irrelevant here. – michael.hor257k Dec 30 '15 at 17:27
  • The CSS option didn't seem to change anything. Not sure how to execute #2 and #3 - how do I write the xsl code for that? – User1218 Dec 30 '15 at 19:52