Line Break in Xml

How to add a newline (line break) in XML file?

A newline (aka line break or end-of-line, EOL) is special character or character sequence that marks the end of a line of text. The exact codes used vary across operating systems:























Operating SystemEnd-of-Line (EOL) marker
UnixLF
Mac OS up to version 9CR
Windows, DOSCR+LF

Adding a new line/break tag in XML

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!

Line Break in XML formatting?

Use \n for a line break and \t if you want to insert a tab.

You can also use some XML tags for basic formatting: <b> for bold text, <i> for italics, and <u> for underlined text.

Other formatting options are shown in this article on the Android Developers' site:

https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

Line Break in XML?

@icktoofay was close with the CData

<myxml>
<record>
<![CDATA[
Line 1 <br />
Line 2 <br />
Line 3 <br />
]]>
</record>
</myxml>

Is a newline character invalid in XML?

Presumably you mean to ask about well-formed, not valid, XML. (See Well-formed vs Valid XML for details on the difference.)

Newline characters are most certainly allowed in well-formed XML.

  • (#xA) is CR
  • (#xD) is LF

(Windows usually end lines with CR+LF; MacOS X and Linux, LF; classic Mac OS, CR.)

The XML Recommendation does indeed clearly allow both. See Character Range:

Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
[#x10000-#x10FFFF]

Common Usage

Within an element, new lines are typically significant to an application:

<a>one
two</a>

usually means something different than

<a>one two</a>

Between markup, new lines typically are insignificant:

<a>
<b>one</b>
</a>

usually means the same as

<a><b>one</b></a>

Other Characters

Finally, you're painting in somewhat sloppy strokes in saying that &, <, and > are illegal. Instead, use the following guidelines:

  • &: must use & if not a part of an entity reference.
  • <: must use < if not a part of a tag, comment, PI, etc.
  • >: must use > if part of the string ]]>.
  • ': must use apos; if within attribute values delimited by '.
  • ": must use quot; if within attribute values delimited by ".

See also

  • How to add a newline (line break) in XML file?

How to make a new line or tab in <string> XML (eclipse/android)?

Add \t for tab and \n for new line.

How to add a line break to text in UI5?

New lines in text controls can be added with the following characters:

  • In XML views or XML fragments:

    • Line feed: or .
    • Recommended:* In combination with carriage return: or .
  • In JS or i18n.properties files:

    • Line feed: \n or \u000a.

    • Recommended:* In combination with carriage return: \r\n or \u000d\u000a.

    • Alternatively, consider using template literals instead of concatenating the above characters manually (i.e. simply replace "..." with `...`).

  • Some UI5 controls allow the HTML tag <br> (in XML: <br>) out of the box:
    • sap.m.MessageStrip since UI5 v1.85: API reference, Sample
    • sap.m.FormattedText: API reference

* See Issues with different newline formats. It is recommended to use the combination with Carriage Return for most of the internet protocols.

Here is a UI5 demo with sap.suite.ui.commons.TimelineItem* and sap.m.Text:

globalThis.onUI5Init = () => sap.ui.require([
"sap/ui/core/mvc/XMLView",
"sap/m/Text",
], async (XMLView, Text) => {
"use strict";

const view = await XMLView.create({
definition: `<mvc:View xmlns:mvc="sap.ui.core.mvc" height="100%">
<App xmlns="sap.m" autoFocus="false">
<Page showHeader="false" class="sapUiResponsiveContentPadding">
<commons:TimelineItem xmlns:commons="sap.suite.ui.commons"
text="Multiline supported in Timeline items (XML)"
/>
<HBox id="myBox" justifyContent="SpaceAround">
<Text
text="This is a text (created in XML view)!"
renderWhitespace="true"
/>
</HBox>
</Page>
</App>
</mvc:View>`,
});

const textCreatedInJS = new Text({
renderWhitespace: true,
text: "And this\nis\u000aanother\r\ntext (created in JS)!",
});
view.byId("myBox").addItem(textCreatedInJS);
view.placeAt("content");
});
<script id="sap-ui-bootstrap"
src="https://ui5.sap.com/resources/sap-ui-core.js"
data-sap-ui-libs="sap.ui.core,sap.m,sap.suite.ui.commons"
data-sap-ui-theme="sap_fiori_3"
data-sap-ui-oninit="onUI5Init"
data-sap-ui-async="true"
data-sap-ui-compatversion="edge"
data-sap-ui-excludejquerycompat="true"
data-sap-ui-xx-waitfortheme="init"
></script>
<body id="content" class="sapUiBody"></body>

Is it bad practice to be sensitive to linebreaks in XML documents?

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.

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.

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.

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.)

Some suggested reading

  • An article from XML.com says the following:

XML applications often seem to take a
cavalier attitude toward whitespace
because the rules about the places in
an XML document where whitespace
doesn't matter sometimes give these
applications free rein to add or
remove whitespace in certain places.

  • A collection of XSL-list posts regarding whitespace.


Related Topics



Leave a reply



Submit