<p>I'm generating some XML documents and when it comes to the address part I have fragments that look like this:</p>

<pre><code>&lt;Address&gt;15 Sample St
Example Bay
Some Country&lt;/Address&gt;
</code></pre>

<p>The XSLT that I have for converting this to XHTML has some funky recursive template to convert newline characters within strings to &lt;br/&gt; tags.</p>

<p>This is all working fine; but is it considered "bad practice" to rely on linebreaks within XML documents?  If so, is it recommended that I do this instead?</p>

<pre><code>&lt;Address&gt;&lt;Line&gt;15 Sample St&lt;/Line&gt;
&lt;Line&gt;Example Bay&lt;/Line&gt;
&lt;Line&gt;Some Country&lt;/Line&gt;&lt;/Address&gt;
</code></pre>

<p>Seems like it'd be really awkward to wrap every place where my text may be multiple lines with tags like that..</p>

[{'answer_id': 7280, 'author': 'Mark Renouf', 'author_id': 758, 'author_profile': 'https://Stackoverflow.com/users/758', 'pm_score': -1, 'selected': False, 'text': '<p>Yes, I think using a <a href="http://en.wikipedia.org/wiki/CDATA" rel="nofollow noreferrer">CDATA</a> block would protect the whitespace. Although some parser APIs allow you to preserve whitespace.</p>\n'}, {'answer_id': 7282, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 2, 'selected': False, 'text': "<p>I think the only real problem is that it makes the XML harder to read.  e.g.</p>\n\n<pre><code>&lt;Something&gt;\n    &lt;Contains&gt;\n        &lt;An&gt;\n            &lt;Address&gt;15 Sample St\nExample Bay\nSome Country&lt;/Address&gt;\n        &lt;/An&gt;\n    &lt;/Contains&gt;\n&lt;/Something&gt;\n</code></pre>\n\n<p>If pretty XML isn't a concern, I'd probably not worry about it, so long as it's working.  If pretty XML is a concern, I'd convert the explicit newlines into <code>&lt;br /&gt;</code> tags or <code>\\n</code> before embedding them in the XML.</p>\n"}, {'answer_id': 7285, 'author': 'Nikhil', 'author_id': 853, 'author_profile': 'https://Stackoverflow.com/users/853', 'pm_score': 1, 'selected': False, 'text': '<p>It depends on how you\'re reading and writing the XML.</p>\n\n<p>If XML is being generated automatically - if newlines or explicit <strong>\\n</strong> flags are being parsed into <strong><br /></strong> - then there\'s nothing to worry about.  Your input likely doesn\'t have any other XML in it so it\'s just cleaner to not mess with XML at all.</p>\n\n<p>If tags are being worked with manually, it\'s still cleaner to just have a line break, if you ask me.</p>\n\n<p>The exception is if you\'re using DOM to get some structure out of the XML.  In that case line breaks are obviously evil because they don\'t represent the heirarchy properly.  It sounds like the heirarchy is irrelevant for your application, though, so line breaks sound sufficient.</p>\n\n<p>If the XML just looks bad (especially when automatically generated), <a href="http://tidy.sourceforge.net/" rel="nofollow noreferrer">Tidy</a> can help, although it works better with HTML than with XML.</p>\n'}, {'answer_id': 7292, 'author': 'James Sulak', 'author_id': 207, 'author_profile': 'https://Stackoverflow.com/users/207', 'pm_score': 4, 'selected': True, 'text': '<p>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.  </p>\n\n<p>The real problem is that most applications that output your XML into a readable format consider all whitespace in an XML interchangable, and might collapse those linebreaks into a single space.  That\'s why your XSLT has to jump through such hoops to render the data properly.  Using a "br" tag would vastly simplify the transform.</p>\n\n<p>Another potential problem is that if you open up your XML document in an XML editor and pretty-print it, you\'re likely to lose those line breaks.  </p>\n\n<p>If you do keep using linebreaks, make sure add an xml:space="preserve" attribute to "address."  (You can do this in your DTD, if you\'re using one.)</p>\n\n<p><strong>Some suggested reading</strong></p>\n\n<ul>\n<li>An <a href="http://www.xml.com/pub/a/2001/11/07/whitespace.html" rel="noreferrer">article from XML.com</a> says the following:</li>\n</ul>\n\n<blockquote>\n  <p>XML applications often seem to take a\n  cavalier attitude toward whitespace\n  because the rules about the places in\n  an XML document where whitespace\n  doesn\'t matter sometimes give these\n  applications free rein to add or\n  remove whitespace in certain places.</p>\n</blockquote>\n\n<ul>\n<li><a href="http://www.dpawson.co.uk/xsl/sect2/N8321.html" rel="noreferrer">A collection of XSL-list posts regarding whitespace</a>. </li>\n</ul>\n'}, {'answer_id': 7300, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': -1, 'selected': False, 'text': '<p>What you really should be doing is converting your XML to a format that preserves white-space.</p>\n\n<p>So rather than seek to replace \\n with &lt;br /&gt; you should wrap the whole block in a &lt;pre&gt;</p>\n\n<p>That way, your address is functionally preserved (whether you include line breaks or not) and the XSTL can choose whether to preserve white-space in the result.</p>\n'}, {'answer_id': 7331, 'author': 'Valters Vingolds', 'author_id': 885, 'author_profile': 'https://Stackoverflow.com/users/885', 'pm_score': -1, 'selected': False, 'text': '<p>I recommend you should either add the <code>&lt;br/&gt;</code> line breaks or maybe use line-break entity - <code>&amp;#x000D;</code></p>\n'}, {'answer_id': 7336, 'author': 'Ran Biron', 'author_id': 931, 'author_profile': 'https://Stackoverflow.com/users/931', 'pm_score': 0, 'selected': False, 'text': '<p>I don\'t see what\'s wrong with <code>&lt;Line&gt;</code> tags.<br>\nApparently, the visualization of the data is important to you, important enough to keep it in your data (via line breaks in your first example). Fine. Then really keep it, don\'t rely on "magic" to keep it for you. Keep every bit of data you\'ll need later on and can\'t deduce perfectly from the saved portion of the data, keep it even if it\'s visualization data (line breaks and other formatting). Your user (end user of another developer) took the time to format that data to his liking - either tell him (API doc / text near the input) that you don\'t intend on keeping it, or - just keep it.</p>\n'}, {'answer_id': 7356, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': -1, 'selected': False, 'text': '<p>If you need your linebreaks preserved, use a CDATA block, as <a href="https://stackoverflow.com/questions/7277/is-it-bad-practice-to-be-sensitive-to-linebreaks-in-xml-documents#7280">tweakt said</a></p>\n\n<p>Otherwise beware. Most of the time, the linebreaks will be preserved by XML software, but sometimes they won\'t, and you really don\'t want to be relying on things which only work by coincidence</p>\n'}, {'answer_id': 7358, 'author': 'Rob Thomas', 'author_id': 803, 'author_profile': 'https://Stackoverflow.com/users/803', 'pm_score': 2, 'selected': False, 'text': '<p>What about using attributes to store the data, rather than text nodes:</p>\n\n<pre><code>&lt;Address Street="15 Sample St" City="Example Bay" State="" Country="Some Country"/&gt;\n</code></pre>\n\n<p>I know the use of attributes vs. text nodes is an often debated subject, but I\'ve stuck with attributes 95% of the time, and haven\'t had any troubles because of it.</p>\n'}, {'answer_id': 7761, 'author': 'Hugo', 'author_id': 972, 'author_profile': 'https://Stackoverflow.com/users/972', 'pm_score': 1, 'selected': False, 'text': '<p>This is probably a bit deceptive example, since address is a bit non-normalized in this case. It is a reasonable trade-off, however since address fields are difficult to normalize.\nIf you make the line breaks carry important information, you\'re un-normalizing and making the post office interpret the meaning of the line break.</p>\n\n<p>I would say that normally this is not a big problem, but in this case I think the Line tag is most correct since it explicitly shows that you don\'t actually interpret what the lines may mean in different cultures. (Remember that most forms for entering an address has zip code etc, and address line 1 and 2.)</p>\n\n<p>The awkwardness of having the line tag comes with normal XML, and has been much debated at coding horror. <a href="http://www.codinghorror.com/blog/archives/001139.html" rel="nofollow noreferrer">http://www.codinghorror.com/blog/archives/001139.html</a></p>\n'}, {'answer_id': 17033, 'author': 'Boris Terzic', 'author_id': 1996, 'author_profile': 'https://Stackoverflow.com/users/1996', 'pm_score': 1, 'selected': False, 'text': '<p>The XML spec has something to say regarding <a href="http://www.w3.org/TR/xml/#sec-white-space" rel="nofollow noreferrer">whitespace</a> and <a href="http://www.w3.org/TR/xml/#sec-line-ends" rel="nofollow noreferrer">linefeeds and carriage returns in particular</a>. So if you limit yourself to true linefeeds (x0A) you should be Ok. However, many editing tools will reformat XML for "better presentation" and possibly get rid of the special syntax. A more robust and cleaner approach than the "&lt; line>&lt; / line>" idea would be to simply use namespaces and embed XHTML content, e.g.:</p>\n\n<pre><code>&lt;Address xmlns="http://www.w3.org/1999/xhtml"&gt;15 Sample St&lt;br /&gt;Example Bay&lt;br /&gt;Some Country&lt;/Address&gt;\n</code></pre>\n\n<p>No need to reinvent the wheel when it comes to standard vocabularies.</p>\n'}, {'answer_id': 25089, 'author': 'jelovirt', 'author_id': 2679, 'author_profile': 'https://Stackoverflow.com/users/2679', 'pm_score': 2, 'selected': False, 'text': '<p>Few people have said that CDATA blocks will allow you to retain line breaks. This is wrong. CDATA sections will only make markup be processed as character data, they will <strong>not</strong> change line break processing.</p>\n\n<pre><code>&lt;Address&gt;15 Sample St\nExample Bay\nSome Country&lt;/Address&gt;\n</code></pre>\n\n<p>is exactly the same as</p>\n\n<pre><code>&lt;Address&gt;&lt;![CDATA[15 Sample St\nExample Bay\nSome Country]]&gt;&lt;/Address&gt;\n</code></pre>\n\n<p>The only difference is how different APIs report this.</p>\n'}]
['https://Stackoverflow.com/questions/7277', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/755/']