Document structure
Significant whitespace
Whitespace that is part of a document's content, as opposed to indentation a processor may drop.
XML itself preserves all whitespace in content: the parser hands it over exactly as written. What varies is what happens next. A processor told the document's element model — by DTD or schema — knows which whitespace is between elements that cannot contain text, and may discard it as ignorable.
Without that model, nothing can tell indentation from data. This is why pretty-printing an arbitrary document is unsafe, and why our formatter and minifier only touch whitespace where no text content is at risk.
- Defined in
- XML 1.0 §2.10
- Category
- Document structure
- Also written
- ignorable whitespace, xml:space
Shown, not asserted
All 3 claims are executed on every test run, against the same engines the tools use. If the behaviour changes, the test fails rather than this page quietly becoming wrong.
Whitespace inside an element is content and is preserved exactly.
document.xml <x> a b </x>expression string(/x)What the engine returns
a bnormalize-space() is how you get the collapsed form when you want it.
document.xml <x> a b </x>expression normalize-space(/x)What the engine returns
a bCanonical form keeps whitespace between elements — it is content until proven otherwise.
document.xml <r> <a/> </r>What the engine returns
c14n <r> <a></a> </r>
What catches people out
- xml:space="preserve" is a signal to applications, not an instruction to the parser. The parser preserved it either way.
- Reformatting a document with mixed content changes its meaning. Only reformat when you know the model.
- A schema's whiteSpace facet collapses values during validation, which means the validated value and the document's value can differ.
Where it bites
A definition on its own is trivia. These are the pages where this term decides whether a document works.
- DatatypeswhiteSpaceThe facet that decides whether a value is preserved, replaced or collapsed.
- XPathnormalize-space()The function for collapsing runs of whitespace in a query.
- ToolsXML minifierRemoves only the whitespace it can prove is safe to remove.
- NamespacesThe reserved xml: namespaceWhere xml:space is defined.
Related terms
- Mixed contentAn element containing both text and child elements, interleaved — the model prose uses and data rarely does.
- Text nodeA run of character data in the tree, as a node in its own right — the model a query language sees rather than the markup you wrote.
- Attribute-value normalizationThe parser's rewriting of attribute values before anything sees them: literal tabs, newlines and carriage returns become spaces.
- Canonicalization (C14N)Rewriting a document into a single normal form — sorted attributes, expanded empty elements, resolved references, no declaration — so that equivalent documents become byte-identical.
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.