What Is the xml:space Attribute in XML?
The xml:space attribute is a reserved attribute in the
XML standard that signals to an XML parser or consuming application how
whitespace characters within an element should be handled. By default,
XML parsers may strip, collapse, or alter insignificant whitespace such
as line breaks, tabs, and multiple spaces. This article explains the
purpose of the xml:space attribute, the two valid values it
accepts, how inheritance works across nested elements, and its practical
importance for maintaining data integrity.
Purpose of xml:space
In standard XML processing, whitespace between tags or within text nodes is often normalized or stripped to reduce file size and simplify parsing. However, certain types of data—such as source code snippets, poetry, tabular text, or formatted documents—rely strictly on exact spacing and indentation.
The xml:space attribute was defined directly in the W3C
XML specification to standardize how authors declare their whitespace
preservation requirements without relying on proprietary or
application-specific flags.
Supported Values
The xml:space attribute accepts only two standard
values:
default: Tells the application that its standard whitespace handling rules apply. In this mode, the parser or application is free to remove or collapse redundant whitespace.<paragraph xml:space="default"> This text can have its extra spaces collapsed. </paragraph>preserve: Explicitly instructs the application to maintain all whitespace characters (spaces, tabs, carriage returns, and line feeds) exactly as they appear within the element.<code-block xml:space="preserve"> function example() { return true; } </code-block>
Inheritance and Scope
The xml:space attribute applies to the element on which
it is declared and recursively inherits down to all child elements.
If a parent element defines xml:space="preserve", all
nested tags will preserve whitespace unless a child element explicitly
overrides the behavior by setting xml:space="default".
<document xml:space="preserve">
<formatted-text>
Spaces and line breaks are preserved here.
</formatted-text>
<unformatted-text xml:space="default">
Spaces are handled by the application's default rules here.
</unformatted-text>
</document>Schema and DTD Requirements
Because xml:space is an official reserved attribute
belonging to the xml namespace, it must be properly
declared in a Document Type Definition (DTD) or XML Schema (XSD) if
validation is enabled.
In a DTD, it is typically declared as an enumerated type:
<!ATTLIST pre xml:space (default|preserve) "preserve">In an XML Schema, it is referenced via the standard XML namespace:
<xs:attribute ref="xml:space" default="preserve"/>Key Significance in XML Workflows
- Data Preservation: Guarantees that structured text content remains intact through parsing and serialization cycles.
- Interoperability: Provides a universally recognized, standard mechanism across different XML tools, processors, and programming languages.
- Validation Compatibility: Allows schema authors to specify exact formatting expectations for specific elements without altering the processing of the entire document.