在 XML 中添加新的 line/break 标记

我一直试图在 XML 中为代码添加一个新的换行符,但是没有成功。

到目前为止,我已经试过了:

<br />
<br>



下面是我正在处理的代码示例。我包含了“ 
”来显示代码中的中断位置。

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="dummy.xsl"?>
<item>
<summary>Tootsie roll tiramisu macaroon wafer carrot cake.
&#xA; Danish topping sugar plum tart bonbon caramels cake.
</summary>
</item>
495970 次浏览

You probably need to put it in a CDATA block to preserve whitespace

<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="dummy.xsl"?>
<item>
<summary>
<![CDATA[Tootsie roll tiramisu macaroon wafer carrot cake.
Danish topping sugar plum tart bonbon caramels cake.]]>
</summary>
</item>

New Line XML

with XML

  1. Carriage return: &#xD;
  2. Line feed: &#xA;

or try like @dj_segfault proposed (see his answer) with CDATA;

 <![CDATA[Tootsie roll tiramisu macaroon wafer carrot cake.
Danish topping sugar plum tart bonbon caramels cake.]]>

You don't need anything fancy: the following contains a new line (two, actually):

<summary>Tootsie roll tiramisu macaroon wafer carrot cake.
Danish topping sugar plum tart bonbon caramels cake.
</summary>

The question is, why isn't this newline having the desired effect: and that's a question about what the recipient of the XML is actually doing with it. For example, if the recipient is translating it to HTML and the HTML is being displayed in the browser, then the newline will be converted to a space by the browser. You need to tell us something about the processing pipeline.

Without using CDATA, try

<xsl:value-of select="'&#xA;'" />

Note the double and single quotes.

That is particularly useful if you are not creating xml aka text. <xsl:output method="text" />

The solution to this question is:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="dummy.xsl"?>
<item>
<summary>
<![CDATA[Tootsie roll tiramisu macaroon wafer carrot cake. <br />
Danish topping sugar plum tart bonbon caramels cake.]]>
</summary>
</item>

by adding the <br /> inside the the <![CDATA]]> this allows the line to break, thus creating a new line!

You are probably using Windows, so new line is CR + LF (carriage return + line feed). So solution would be:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="dummy.xsl"?>
<item>
<summary>Tootsie roll tiramisu macaroon wafer carrot cake.&#13;&#10;Danish topping sugar plum tart bonbon caramels cake.
</summary>
</item>

For Linux there is only LF and for Mac OS only CR.

In question there showed Linux way.

The easiest way to give a line break is to do the following :
1> Add the following in CSS - e{display:block}
2> Now wherever you want to give a line break, type -

<e></e>

The Way to do Line Break in XML is to use &#xA;

It worked for me in Eclipse IDE , when I was designing my XML layout & was using TextView.

just simply press enter it make a break

<![CDATA[this is
my text.]]>

Had same issue when I had to develop a fixed length field format.

Usually we do not use line separator for binary files but For some reason our customer wished to add a line break as separator between records. They set

< record_delimiter value="\n"/ >

but this didn't work as records got two additional characters:
< record1 > \n < record2 > \n.... and so on.

Did following change and it just worked.

< record_delimiter value="\n"/> => < record_delimiter value="&#xA;"/ >

After unmarshaling Java interprets as new line character.

Wanted to add my solution:

&lt; br/ &gt;

which is basically the same as was suggested above
In my case I had to use &lt for < and &gt for > Simply putting <br /> did not work.

(Using system.IO)

You can simply use \n for newline and \t in front of the string to indent it.

For example in c#:

public string theXML() {
string xml = "";
xml += "<Scene>\n";
xml += "\t<Character>\n";




xml += "\t</Character>\n";
xml += "</Scene>\n";
return xml;
}

This will result in the output: http://prntscr.com/96dfqc

This solution worked to me:

<summary>Tootsie roll tiramisu macaroon wafer carrot cake. &#xD;Danish topping sugar plum tart bonbon caramels cake.</summary>

You will have the text in two lines.

This worked to me using the XmlReader.Read method.

This can be addressed simple by CSS attribute:

<label name="pageTac"> Hello how are you doing? Thank you I'm Good</label>

CSS

.pageText{
white-space:pre !important; // this wraps the xml text.}

HTML / XSL

<tr>
<td class="pageText"><xsl:value-of select="$Dictionary/infolabels/label[@name='pageTac']" />
</td></tr>