XSD facet
whiteSpace
preserve, replace or collapse — applied before every other facet, which is why it changes what they see.
whiteSpace decides what happens to whitespace before validation: preserve leaves it, replace turns tab, LF and CR into spaces, and collapse additionally trims and squeezes runs.
It is fixed for most built-in types — preserve on xs:string, replace on xs:normalizedString, collapse on xs:token and on every numeric and date type. That last one is why ' 42 ' is a valid xs:integer.
- Lexical space
- One of: preserve, replace, collapse.
- Derives from
- Primitive type
- Category
- Constraining facets
What catches people out
- It runs before every other facet, so patterns and enumerations see the processed value.
- Numeric and date types collapse, which means ' 42 ' validates as xs:integer. Surrounding whitespace is never the reason a number fails.
- It cannot be relaxed by derivation — a type derived from xs:token cannot go back to preserve.
The schema used
This is the exact schema the literals above are validated against. Paste it into the validator with one of them to see the result yourself.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="t"><xs:restriction base="xs:integer"><xs:whiteSpace value="collapse"/></xs:restriction></xs:simpleType>
<xs:element name="v" type="t"/>
</xs:schema>Related
- xs:tokenA string trimmed of leading and trailing whitespace, with internal runs collapsed to one space. The right type for most identifiers.
- xs:stringAny sequence of legal XML characters, with whitespace preserved exactly. The only string type that does not touch your value.
- xs:normalizedStringA string with tabs, newlines and carriage returns replaced by spaces — but not trimmed and not collapsed.
- patternA regular expression the whole value must match — anchored implicitly, and in XSD's own dialect rather than PCRE.
Get started
Bring order to the XML your team can't afford to ignore.
Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.