Whitespace and Strings
Whitespace handling varies by implementation and depends on whether the implementation uses schema validation, and how it chooses to handle whitespace in element content. Every XML parser normalizes the whitespace in attribute values, replacing carriage returns, line feeds, and tabs with spaces. XML Schema processors may further normalize whitespace of an attribute or element value based on its type. During XML Schema validation, whitespace is preserved in values of type xs:string
(and some of its descendants), but collapsed in all others.
Within string literals in queries, whitespace is always significant. For example, the expression string-length(" x ")
evaluates to 3
, not 1
.
Normalizing Whitespace
The normalize-space
function collapses whitespace in a string. Specifically, it performs the following steps:
Replaces each carriage return (
#xD
), line feed (#xA
), and tab (#x9
) character with a single space (#x20
)Collapses all consecutive spaces into a single space
Removes all leading and trailing spaces
Table 17-11 shows some examples.
Table 17-11. Examples of the normalize-space function
Example |
Return value |
---|---|
|
|
|
|
|
|
|
|
|
A zero-length string |
Get XQuery now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.