Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
This process never removes whitespace characters that are adjacent to non-whitespace characters. For example, consider the following:
The story of Abelard and Heloise is best recalled nowadays from the stage drama of 1970 and it is perhaps inevitable that Diana Rigg stripping off for Keith Mitchell should be the most enduring image of this historic couple in some people's minds.
Our textual analysis will focus entirely on the whitespace—the actual content of the piece is best ignored.
There are five whitespace-only text nodes in this fragment, one before each of the child elements
.
The whitespace within the
xs:token
(or a type derived from this), then the schema processor will remove the leading and trailing whitespace characters and convert the newlines into single spaces. But if it is of type
xs:string
, or if no schema processing is done, then all the spaces and newlines will be present in the tree model of the source document. What you can do is to call the
normalize-space()
function when processing these nodes on the source tree, which will have the same effect as schema processing for a type that specifies the
collapse
option (that is, it will remove leading and trailing whitespace and replace all other sequences of one or more whitespace characters by a single space). The
normalize-space()
function is described in Chapter 13.
The processing done by a schema processor for data of type
xs:normalizedString
is to replace each newline, tab, and carriage return by a single space character. This is not the same as the processing done by the
normalize-space()
function in XPath. The term
normalization
, unfortunately, does not have a standard meaning.
So we can see that XSLT makes a very firm distinction between text nodes that consist of whitespace only, and those that hold something other than whitespace. A whitespace text node can exist only where there is nothing between two pieces of markup other than whitespace characters.
To take another example, consider the following document:
71 Lombard Street
London, UK
Where are the whitespace nodes? Let's look at it again, this time making the whitespace characters visible.
→
→
→
→♦♦♦71 Lombard Street
→♦♦♦London, UK
→♦♦♦
→
The newline and tab between
and
and
→
→
→
71 Lombard Street
London, UK
→>