In this chapter, we will discuss whitespace handling in XML documents. Whitespace is a collection of spaces, tabs, and newlines. They are generally used to make a document more readable.
XML document contains two types of whitespaces - Significant Whitespace and Insignificant Whitespace. Both are explained below with examples.
A significant Whitespace occurs within the element which contains text and markup present together. For example −
<name>TanmayPatil</name>
and
<name>Tanmay Patil</name>
The above two elements are different because of the space between Tanmay and Patil. Any program reading this element in an XML file is obliged to maintain the distinction.
Insignificant whitespace means the space where only element content is allowed. For example −
<address.category = "residence">or
<address....category = "..residence">
The above examples are same. Here, the space is represented by dots (.). In the above example, the space between address and category is insignificant.
A special attribute named xml:space may be attached to an element. This indicates that whitespace should not be removed for that element by the application. You can set this attribute to default or preserve as shown in the following example −
<!ATTLIST address xml:space (default|preserve) 'preserve'>
Where,
The value default signals that the default whitespace processing modes of an application are acceptable for this element.
The value preserve indicates the application to preserve all the whitespaces.