Whitespace in XQuery

Whitespace is allowed in most XQuery expressions to improve readability even if whitespace is not part of the syntax for the expression. Whitespace consists of space characters (U+0020), carriage returns (U+000D), line feeds (U+000A), and tabs (U+0009).

In general, whitespace is not significant in an XQuery expression, except in the following situations where whitespace is preserved:

  • The whitespace is in a string literal.
  • The whitespace clarifies an expression by preventing two adjacent tokens from being mistakenly recognized as one.
  • The whitespace is in an element constructor. The boundary-space declaration in the prolog determines whether to preserve or strip whitespace in element constructors.

The following examples include expressions that require whitespace for clarity:

  • one- two results in a syntax error. The parser recognizes one- as a single QName (qualified name) and raises an error when no operator is found.
  • one -two does not result in a syntax error. The parser recognizes one as a QName, the minus sign (-) as an operator, and then two as another QName.
  • one-two does not result in a syntax error. However, the expression parses as a single QName because a hyphen (-) is a valid character in a QName.
  • The following expressions all result in syntax errors:
    • 5 div2
    • 5div2
    In these expressions, whitespace is required for the parser to recognize each token separately. Notice that 5div 2 does not result in a syntax error.