Whitespace

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. Sometimes the standards, constraints, and conventions imposed by XML cause problems for TEI encodings and for programs that process TEI files.

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> ?

Normalize = Collapse + Trim
Many applications, including web browsers and many programs that read XML files will, unless instructed otherwise, “collapse” XML whitespace, that is, they will 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 Jo</tt> and Ann</tt>. Moreover many applications will remove (&ldquo;trim&rdquo;) leading and trailing XML whitespace. So these, too,

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

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

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

would be treated as if the XML had been simply &lt;name&gt;Jo Ann&lt;/name&gt;</tt>.

Sometimes, as in the XSLT function &ldquo;normalize-space&rdquo;, the term &ldquo;normalize&rdquo; refers to the combination of collapsing XML whitespace and then trimming. Other times, as in XML Schema, &ldquo;collapse&rdquo; is the name of the combined operation. This article uses the XSLT terminology: normalizing is collapsing plus trimming.

Normalizing XML whitespace is very common. It is so pervasive that it is easy to overlook that it is happening and even difficult to know which program processing an XML file is doing the normalizing—is it the XSLT processor, the XSL program, the web browser, the print routine, or some combination?

@xml:space
XML defines an attribute, xml:space</tt>, that when set to preserve</tt> instructs applications to suspend default trimming, collapsing, and normalizing and instead keep all the spaces, tabs, carriage returns, and line feeds just as they are. If @xml:space</tt> is set to default or is simply left off, no such request is made; the application is free to do whatever its developer thinks best.

The attribute xml:space</tt> is inherited by child elements. One could, for example, put xml:space="preserve"</tt> into a TEI &lt;text&gt;</tt> element but not in &lt;teiHeader&gt;</tt>, to indicate that the request applies to all of the text but to none of the header.

TEI allows xml:space</tt> to be used on any element. But since TEI has so much rich functionality for encoding spaces, gaps, line breaks, and so on, the xml:space</tt> attribute is rarely used. Whatever could be accomplished by setting its value to <tt>preserve</tt> would be better accomplished by using native TEI elements. So the value is normally left as <tt>default</tt> by simply not including the attribute. Downstream processors are then left free to treat XML whitespace however the application developers want.

Default Whitespace Processing
When <tt>xml:space</tt> is left as <tt>default</tt>, nothing in XML or TEI specifies how consumers of a TEI XML file should treat whitespace. There are, however, unspecified conventions. TEI encodings generally assume that space will be normalized, that in this encoding

&lt;p&gt; We hold these truths to be self-evident, that all men are are created equal, that they are endowed by their creator with certain inalienable Rights, that among these are Life, Liberty and the pursuit of Happiness. &lt;/p&gt;

some downstream processor will collapse spaces, tabs, carriage returns, and line feeds and will trim the space just after the &lt;p&gt; and just before the &lt;/p&gt;

Collapsing
TEI encoders generally assume that some downstream processor will collapse spaces, tabs, carriage returns, and line feeds and will trim the space at the beginnings and ends of encodings such as this:

&lt;p&gt; We hold these truths to be self-evident, that all men are are created equal, that they are endowed by their creator with certain inalienable Rights, that among these are Life, Liberty and the pursuit of Happiness. &lt;/p&gt;

If the TEI file will be displayed in a web browser, the assumption is generally safe. Authors of web browsers have accepted responsibility for normalizing such text. The authors of other applications that read TEI XML files may need to be instructed to do the same.

Ideally there would be a formal mechanism for such communication, but XML has no such mechanism, and TEI 5 does not either. Because the assumption manifestly made above is so common, the author of a program that reads a TEI XML file should&mdash;unless instructed otherwise by the encoders&mdash;assume the burden of collapsing, but not necessarily of trimming, space in text nodes such as this one above.

Trimming
Whether text in an element should be trimmed depends (1) on TEI's specification of the element's parent, (2) on whether the element has siblings, and if so, whether the element is first of those siblings, is the last one, or is in the middle, and (3) on practices of the encoders, practices unspecified in TEI.

1. Children of Structured Elements
Part of defining an XML vocabulary such as TEI is specifying whether an element may contain text and elements or just elements. In TEI, <tt>&lt;address&gt;</tt> may only contain other elements. This

&lt;address&gt; &lt;street&gt;10 Downing Street&lt;/street&gt; &lt;settlement&gt;London&lt;/settlement&gt; &lt;postCode&gt;SW1A 2AA&lt;/postCode&gt; &lt;address&gt;

is valid TEI. But this

&lt;address&gt; &lt;street&gt;10 Downing Street&lt;/street&gt;, &lt;settlement&gt;London&lt;/settlement&gt; &lt;postCode&gt;SW1A 2AA&lt;/postCode&gt; &lt;address&gt;

is not, because of that comma after the <tt>&lt;street&gt;</tt> element. Free non-whitespace text is not allowed between the elements that comprise the <tt>&lt;address&gt;</tt> element. Though the term is sometimes used more loosely, <tt>&lt;address&gt;</tt> would commonly be called a "structured element."

Elements that do not allow free non-whitespace text&mdash;structured elements, strictly speaking&mdash;mimic database records. When XML is used to move data between databases, such elements are the norm. And often, a program extracting metadata from a TEI file will be looking for structured data, so that it can populate database fields:

street: 10 Downing Street city:   London postal: SW1A 2AA country:

The comma does not belong in the database. A program using the database will later decide how to format the full address&mdash;with commas, new-lines, spaces, etc.

If an element's parent does not allow free non-whitespace text, space within an element should be trimmed, that is, leading and trailing whitespace should be removed. A database extractor, for example, should populate a database field the same way for any of these encodings:

&lt;settlement&gt; London  &lt;/settlement&gt;

&lt;settlement&gt;London&lt;/settlement&gt;

&lt;settlement&gt; London &lt;/settlement&gt;

2. Child Among Siblings
If an element's parent does allow free non-whitespace text between elements&mdash;that is, if it is a "mixed-content" element&mdash;then whether or not to trim depends on where the element is among its siblings.

TEI's paragraph element, <tt>&lt;p&gt;</tt> elements can contain both free non-whitespace text and other elements. This <tt>&lt;p&gt;</tt> element

&lt;p&gt; The &lt;emph&gt; cat &lt;/emph&gt; ate the &lt;foreign&gt;croissant&lt;/foreign&gt;. It wasn't me! &lt;/p&gt;

has five children: