Skip to main content
XMLDir

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

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 b
  • normalize-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 b
  • Canonical 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.

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.