Whitespace

Managing XML’s Whitespace in TEI Documents
TEI has robust features for specifying space, gaps, line breaks, and related aspects of the space between text. But TEI is an XML vocabulary, and XML itself, and programs that read and process XML files, have their own ways to deal with what they call whitespace, that is, space, tab, carriage return and linefeed characters. Often the standards, constraints, and conventions imposed by XML cause no problem for TEI encodings. But the interactions between XML's features and TEI's can sometimes cause subtle problems and sometimes even significant damage during processing of a TEI document.

This page offers an introduction to those interactions.

Where XML Considers Whitespace to be Significant
In XML documents, some whitespace is significant, some is not. For example, inside the brackets that mark XML elements extra whitespace is not significant. For any program processing these as pieces of XML,

&lt;title type="main"&gt;

and

&lt;title        type =   "main"   &gt;

are the same. There is no significance to the extra space. By XML rules, no application that processes the data in this XML file (processing it as XML and not just as text) is allowed to treat these two representations differently. A person or computer editing this file is free to use either one, based merely on readability and aesthetics. The fact that there is whitespace between title and type is significant, but how much or of what kind (space characters, tabs, carriage returns, new lines) is not significant. The space between type and = is not significant.

Whitespace can be significant, however, in the content of an element. For example,

&lt;name&gt;JoAnn&lt;/name&gt;

and

&lt;name&gt;Jo Ann&lt;/name&gt;

are different because of that space between Jo</tt> and Ann</tt>, and any program reading this element in an XML file is obliged to maintain the distinction.

But things can get complicated. Consider this:

&lt;persName&gt; &lt;forename&gt;Jo&lt;/forename&gt; &lt;forename&gt;Ann&lt;/forename&gt; &lt;surname&gt;Henry&lt;/forename&gt; &lt;/persName&gt;

Should the carriage returns and new lines matter? Should it matter if that open area before &lt;surname&gt;</tt> is a tab or is instead four space characters? Should it matter that there is extra space after &lt;persName&gt;</tt> ?

&ldquo;Collapse,&rdquo; &ldquo;Trim,&rdquo; &ldquo;Normalize&rdquo;
Many applications, including web browsers and some programs that convert XML into HTML, “collapse” XML whitespace, that is, they replace any contiguous string of space characters (0x20), tabs (0x09), carriage returns (0x0D) and line feeds (0x0A) with just one space character. So

&lt;name&gt;Jo Ann&lt;/name&gt; &lt;name&gt;Jo   Ann&lt;/name&gt; &lt;name&gt;Jo Ann&lt;/name&gt;

are all treated as if there were just one space character between Mary</tt> and Ann</tt>. Moreover many applications will remove (&ldquo;trim&rdquo;) leading and trailing XML whitespace.