When your xml is parsed it gives the following DOM tree:
Original xml:
<comment>
sdagsg
fag
fdhfdhgf
</comment>
Generated DOM tree:
|- NODE_DOCUMENT #document ""
|- NODE_ELEMENT comment ""
|- NODE_TEXT #text "\n sdagsg\n fag\n fdhfdhgf\n "
So the carriage returns are in the XML document.
When i use an MXXMLWriter
to convert the XML back into text, i get following XML fragment:
<comment>
sdagsg
fag
fdhfdhgf
</comment>
So you can get your original XML, with all the embedded whitespace and linebreaks.
The issue you're having is that you want to display the XML as HTML. There are a number of issues with that.
If you blindly try to output the XML inside HTML:
<P><comment>
sdagsg
fag
fdhfdhgf
</comment></P>
Then nothing appears. The html parser doesn't recognize the <comment>
element. Browsers are required to not render any elements they don't recognize.
The first fix one might try, is escape the <
and >
characters, as you are required to do, with <
and >
:
<P><comment>
sdagsg
fag
fdhfdhgf
</comment></P>
This now renders in a browser, but renders as:
<comment> sdagsg fag fdhfdhgf </comment>
The reason for this is that the browser is required to collapse whitespace (e.g, tab, space, linebreak) into a single space.
There are two ways you can handle this. First is to put the "pre-formatted" xml into a preformatted (PRE
) html element:
<PRE><comment>
sdagsg
fag
fdhfdhgf
</comment></PRE>
This gives the output in HTML:
<comment>
sdagsg
fag
fdhfdhgf
</comment>
The other, more complicated, solution is to work with HTML, and present the HTML you wish. If you want to maintain all the whitespace (and not have the browser collapse it), then you must convert it explicitly into non-breaking space characters, by replacing "
" with "
":
<P><comment>
sdagsg
fag
fdhfdhgf
</comment></P>
And you also must expliditely have line breaks by converting every "CRLF
" into "<BR>
":
<P><comment><BR>
sdagsg<BR>
fag<BR>
fdhfdhgf<BR>
And now you have HTML that preserves your spaces and carriage returns:
<comment>
sdagsg
fag
fdhfdhgf
</comment>
But can XSL do it?
i don't know.
XSL may be an NP Turing-complete language, but i don't know if it can:
String html = DocumentToString(xmlDocument);
html = StringReplace(html, "<", "<");
html = StringReplace(html, ">", ">");
html = StringReplace(html, " ", " ");
html = StringReplace(html, "\r\n", "<BR>");