The basic concepts
What is whitespace exactly? In XML, the following characters are considered to be whitespace characters: tab (	), newline (
), carriage return (
), and space ( ).
The global variable WHITESPACE in our template contains these entities:
<xsl:variable name="WHITESPACE">
<xsl:text>	

 </xsl:text>
</xsl:variable>
You could define the variable using the actual characters, but using entities makes the XSL more readable. Later on, the stylesheet uses the WHITESPACE variable in an XSL contains() function to test if individual characters are whitespace characters.
XSL provides a good way of dealing with most whitespace: the normalize-space() function. This function replaces all contiguous whitespace in a string with a single space character ( ). If any whitespace characters are found at the beginning or end of the string, they are deleted. Carried to its logical extreme, these rules mean that a string containing only whitespace is reduced to an empty string.
Normalizing the spaces is useful, but there are places where the leading and trailing whitespace are important. When an inline element occurs before or after a text node, the space between the text node and the element is important. Consider the phrase this is a <b>test</b>. If the space after the article “a” is deleted, the resulting string is “this is atest.” Additionally, a text node that consists of only whitespace must retain at least one space because the space might fall between two elements that must be separated. For example, the phrase <b>test</b> <i>phrase</i> must retain its space, or the result is “testphrase.”
Additionally, whitespace at the beginning of most elements that can contain CDATA must be deleted (<p> or <li> elements, in particular). Not deleting the leading whitespace leads to odd indentation on the first line of these elements.
Finally, the stylesheet must also allow for elements that are intended to preserve space (such as the DITA <pre> or <codeblock> elements). The stylesheet must pass these through without modification.
Next Page:
Stylesheet
organization
