Boundary whitespace in direct element constructors
Within a direct element constructor, boundary whitespace is a sequence of consecutive whitespace characters. The sequence is delimited at each end by the start or end of the content, by a direct constructor, or by an enclosed expression.
For example, boundary whitespace might be used in the content of the constructor to separate the end tag of a direct constructor from the start tag of a nested element.
- A newline character and four space characters that occur between the start tags of the
product
anddescription
elements - Four space characters that occur between the start tag of the
description
element and the enclosed expression - Four space characters that occur between the enclosed expression and the end tag of the
description
element - One newline character that appears after the end tag of the
description
element
- Whitespace that is generated by an enclosed expression
- Characters that are generated by character references (for example,
 
) or by CDataSections - Whitespace characters that are adjacent to a character reference or a CDataSection
The boundary-space policy controls whether boundary whitespace is preserved by element constructors. This policy is specified by a boundary-space declaration in the query prolog. If the boundary-space declaration specifies strip, boundary whitespace is discarded. If the boundary-space declaration specifies preserve, boundary whitespace is preserved. If no boundary-space declaration is specified, the default behavior is to strip boundary whitespace during element construction.
Examples
- In the following example, the constructed
cat
element node has two child element nodes that are namedbreed
andcolor
:
Because the boundary-space policy is strip by default, the whitespace that surrounds the child elements is stripped away by the element constructor.<cat> <breed>{$b}</breed> <color>{$c}</color> </cat>
- In the following example, the boundary-space policy is strip.
The result of the constructor is
<a>abc</a>
:declare boundary-space strip; <a> {"abc"} </a>
- In the following example, the boundary-space policy is preserve.
The result of the constructor is
<a> abc </a>
:
Because the boundary-space policy is preserve, the spaces that appear before and after the enclosed expression is preserved by the element constructor.declare boundary-space preserve; <a> {"abc"} </a>
- In the following example, the whitespace that surrounds
z
is not boundary whitespace. The whitespace is always preserved, and the result of the constructor is<a> z abc</a>
:<a> z {"abc"}</a>
- In the following example, the whitespace characters that are generated
by the character reference and adjacent whitespace characters are
preserved, regardless of the boundary-space policy. The result of
the constructor is
<a> abc</a>
:<a>  {"abc"}</a>
- In the following example, the whitespace in the enclosed expression
is preserved, regardless of the boundary-space policy, because whitespace
that is generated by an enclosed expression is never considered to
be boundary whitespace. The result of the constructor is
<a> </a>
:
The two spaces in the enclosed expression are preserved by the element constructor and appear between the start tag and the end tag in the result.<a>{" "}</a>