XML Line Break Normalization Across Platforms

XML solves the challenge of cross-platform line break incompatibility by enforcing a strict end-of-line normalization process during parsing. Because different operating systems represent newlines using different character sequences, the W3C XML specification requires all conforming XML processors to normalize any line break variant into a single standard Line Feed (\n or #xA) character before passing the text data to the receiving application.

The Cross-Platform Line Break Problem

Operating systems have historically used distinct ASCII control characters to signify the end of a line:

Without standard normalization, data shared across these environments can lead to parsing errors, unintended whitespace differences, or inconsistent text rendering.

XML 1.0 End-of-Line Handling

According to the XML 1.0 specification, an XML parser must normalize all line endings automatically during the initial scanning phase. The rules are:

  1. Any two-character sequence of Carriage Return followed by Line Feed (#xD #xA) is converted into a single Line Feed (#xA).
  2. Any individual Carriage Return (#xD) that is not followed by a Line Feed is converted into a single Line Feed (#xA).

As a result, whether an XML file is authored on Windows, Linux, or classic macOS, the XML parser guarantees that the application processing the XML receives only the #xA character for line breaks.

XML 1.1 Extended Line Break Support

The XML 1.1 specification expanded these normalization rules to accommodate line breaking conventions used in mainframe environments (such as EBCDIC) and newer Unicode standards. XML 1.1 processors normalize the following sequences to a single Line Feed (#xA):

Any #xD not immediately followed by #xA or #x85 is also converted directly to #xA.

Preserving Literal Line Break Characters

If an author needs to preserve an exact Carriage Return (#xD) without it being converted to a Line Feed, they must use a numeric character reference instead of a raw character:

The XML processor resolves character references after the end-of-line normalization phase, allowing the literal Carriage Return to be retained in the final parsed output.

Benefits for Developers

Because normalization is handled at the parser level, software developers writing XML-consuming applications do not need to implement platform-detection logic or custom regular expressions to handle \r\n versus \n. All parsed character data is predictable and uniform across all runtime environments.