Whitespace Basics
Before we begin, it’s worth defining the four characters that the XML spec defines as whitespace:
The tab character (
	)The newline character (

)The carriage return character (

)The space character (
 )
We’ll use this modified version of our car list to illustrate how XML parsers and XSLT processors work with whitespace:
<?xml version="1.0" encoding="utf-8"?>
<!-- carlist_whitespace.xml -->
<cars>
<manufacturer name=" Chevrolet
">
<car>Cavalier</car>
<car>Corvette</car>
<car>Impala</car>
<car>Monte
Carlo</car>
</manufacturer>
</cars>From an XML parser’s perspective, there are a number of whitespace-only
nodes (nodes that contain only whitespace characters) in this
document. The <cars> element
contains a whitespace-only node with the newline character and the tab
or spaces before the <manufacturer> tag, the node for the
<manufacturer> element, and a
whitespace-only node with the newline character after the </manufacturer> tag. Similarly, the
<manufacturer> element contains
whitespace-only nodes between the various <car> elements.
The XML parser doesn’t remove any whitespace-only nodes, so we can always use them in our stylesheets. Put another way, the data model used by the XSLT processor contains the whitespace-only nodes from the XML source. The one exception to this is in an XSLT 2.0 processor that validates the XML source against a schema. If the schema indicates that an element can only contain other elements, any whitespace-only nodes contained ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access