开发者

How to correctly write xml document with spaces and new lines inside using CDATA?

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<CodeCases>
  <Case1>
    <CodeText>
      <![CDATA[
for(var i=0;  i;10;i++) {
      var x;
}
]]>
    &l开发者_StackOverflowt;/CodeText>
  </Case1>
</CodeCases>

This is the code I'm using, however, when I open in browser, it doesn't read the new lines. What am I doing wrong?


With CDATA, your new lines are preserved. The issue in your case isn't that. If you look at the source of generated html file (In firefox, via View -> Page Source or CTRL+U) you'll see that the new lines are preserved in the text. I've just tried your CDATA section by myself (via some XSLT processing).

What happened in your case is that the browser just doesn't care about the raw new lines, and multiple white-space added in html files.

To maintain the formatting of the text, you need to wrap the content with the white-space css property set to pre

ie, the HTML content will be something like,

<div style="white-space:pre;">
for(var i=0;  i;10;i++) {
      var x;
}
</div>

Usual way of maintaining line breaks is adding <br> tags. But it doesn't really apply here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜