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.

The following diagram shows an example of a direct element constructor, with the boundary whitespace highlighted:
Begin figure description. The figure shows carriage returns and whitespace characters around start and end tags of elements. End figure description.
The boundary whitespace in this example includes the following characters:
  • A newline character and four space characters that occur between the start tags of the product and description 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
Boundary whitespace does not include any of the following types of whitespace:
  • 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 named breed and color:
    <cat>
      <breed>{$b}</breed>
      <color>{$c}</color>
    </cat>
    Because the boundary-space policy is strip by default, the whitespace that surrounds the child elements is stripped away by the element constructor.
  • 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>:
    declare boundary-space preserve;
    <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.
  • 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>     &#x20;{"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>:
    <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.