Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a new line/break tag in XML [duplicate]

Tags:

xml

I have been trying to add a new line/break to code within XML and have been unsuccessful.

I have tried so far:

<br />
<br> 



Here is a sample of the code I am working with. I included "
" to show where the break is located within the code.

<?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>
like image 748
Courtney Jordan Avatar asked Jun 06 '12 15:06

Courtney Jordan


People also ask

Do line breaks matter in XML?

It's generally considered bad practice to rely on linebreaks, since it's a fragile way to differentiate data. While most XML processors will preserve any whitespace you put in your XML, it's not guaranteed.

How do I insert a new line break in XML?

You can use &#xA; for line feed (LF) or &#xD; for carriage return (CR), and an XML parser will replace it with the respective character when handing off the parsed text to an application.

Is \n allowed in XML?

No. New line characters are perfectly acceptable in XML. They are included in the character range for text.

Which tag adds a line break?

Adding line breaks in your HTML using the <br> tag is a simple way to make your text more readable. The <br> tag will insert a line break wherever you put it within your code.


8 Answers

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.]]>
like image 126
arthur Avatar answered Oct 02 '22 18:10

arthur


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!

like image 36
Courtney Jordan Avatar answered Oct 02 '22 18:10

Courtney Jordan


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.

like image 32
Michael Kay Avatar answered Oct 02 '22 16:10

Michael Kay


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.

like image 42
ST3 Avatar answered Oct 02 '22 17:10

ST3


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.

like image 33
merce_00 Avatar answered Oct 02 '22 18:10

merce_00


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> 
like image 45
dj_segfault Avatar answered Oct 02 '22 18:10

dj_segfault


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" />

like image 29
Gabe Rainbow Avatar answered Oct 02 '22 17:10

Gabe Rainbow


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>
like image 25
Yash Kulkarni Avatar answered Oct 02 '22 16:10

Yash Kulkarni