Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Carriage return encoding

I was looking to represent a carriage return within an xml node.
I have tried a whitespace preserve, hex entity with no luck- and a \n. viewing via a browser.

Example

<Quote> Alas, poor Yorick! I knew him </Quote> 

Thanks.

like image 812
user273502 Avatar asked Feb 15 '10 12:02

user273502


People also ask

Can an XML have carriage return?

Yes, &#10; is LF, &#13; is CR. Windows world usually has CRLF sequences (that is &#13;&#10;), Linux has just LF (&#10;). OMG, thank you sooooo much!

How do I insert a carriage return 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.

How do I break a line in XML?

Write content in separate tags as this will result in a line break. Also try using \n or \r. Here is the answer from stackoverflow.com https://stackoverflow.com/questions/35504890/how-to-add-a-newline-line-break-in-xml-file Scroll down to the part with cdata. That should be the answer you want.

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.


2 Answers

To insert a CR into XML, you need to use its character entity &#13;.

This is because compliant XML parsers must, before parsing, translate CRLF and any CR not followed by a LF to a single LF. This behavior is defined in the End-of-Line handling section of the XML 1.0 specification.

like image 189
Lachlan Roche Avatar answered Sep 21 '22 16:09

Lachlan Roche


xml:space="preserve" has to work for all compliant XML parsers.

However, note that in HTML the line break is just whitespace and NOT a line break (this is represented with the <br /> (X)HTML tag, maybe this is the problem which you are facing.

You can also add &#10; and/or &#13; to insert CR/LF characters.

like image 41
Lucero Avatar answered Sep 21 '22 16:09

Lucero