Skip to main content
XMLDir

Search 261 pages — tools, formats, elements, namespaces, XPath, datatypes, glossary, parse errors, schema errors, use cases, blog and product.

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 validates

Every literal below is put through the schema validator on each test run, against the schema at the foot of this page.

  • 42

What does not

  • 4 2

    Collapsing does not remove internal whitespace between digits.

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.

schema.xsd
<?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>

Open the XSD validator

Related

All 29 datatypes and facets

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.