3.4 Whitespace Stripping
Whitespace Stripping
After the tree for a source document or stylesheet document has
been constructed, but before it is otherwise processed by XSLT,
some text nodes are stripped. A text node is never stripped
unless it contains only whitespace characters. Stripping the text
node removes the text node from the tree. The stripping process takes
as input a set of element names for which whitespace must be
preserved. The stripping process is applied to both stylesheets and
source documents, but the set of whitespace-preserving element names
is determined differently for stylesheets and for source
documents.
A text node is preserved if any of the following apply:
-
The element name of the parent of the text node is in the set
of whitespace-preserving element names.
-
The text node contains at least one non-whitespace character.
As in XML, a whitespace character is #x20, #x9, #xD or #xA.
-
An ancestor element of the text node has an
xml:space attribute with a value of
preserve, and no closer ancestor element has
xml:space with a value of
default.
Otherwise, the text node is stripped.
The xml:space attributes are not stripped from the
tree.
NOTE:
This implies that if an xml:space attribute is
specified on a literal result element, it will be included in the
result.
For stylesheets, the set of whitespace-preserving element names
consists of just xsl:text.
<
strip-space
elements=
tokens
>
<-- Content: -->
<
/strip-space>
<
preserve-space
elements=
tokens
>
<-- Content: -->
<
/preserve-space>
For source documents, the set of whitespace-preserving element
names is specified by xsl:strip-space and
xsl:preserve-space Top-level elements. These elements each
have an elements attribute whose value is a
whitespace-separated list of NameTests. Initially, the
set of whitespace-preserving element names contains all element names.
If an element name matches a NameTest in an
xsl:strip-space element, then it is removed from the set
of whitespace-preserving element names. If an element name matches a
NameTest in an
xsl:preserve-space element, then it is added to the set
of whitespace-preserving element names. An element matches a NameTest if and only if the
NameTest would be true
for the element as an [XPath node
test]. Conflicts between matches to
xsl:strip-space and xsl:preserve-space
elements are resolved the same way as conflicts between template rules
(see [Conflict Resolution for Template Rules]). Thus, the applicable match for a
particular element name is determined as follows:
It is an error if this leaves more than one match. An XSLT
processor may signal the error; if it does not signal the error, it
must recover by choosing, from amongst the matches that are left, the
one that occurs last in the stylesheet.
|