2026-09-12 · 8 min read
- xml
- whitespace
- interoperability
XML represents text between child elements as text nodes. In element-only configuration vocabularies, indentation nodes may be unimportant to the application. In mixed-content vocabularies such as documents, localization files, and markup-rich messages, the same spaces can separate words and carry visible meaning.
A generic formatter does not know every vocabulary's content model or application contract. Adding indentation to mixed content or trimming text nodes can therefore alter data while producing output that looks cleaner.
Classify content with grammar and context
Inspect each affected element's ordered children: text, elements, comments, processing instructions, and entity-expanded content. A DTD or schema may declare element-only versus mixed content, but non-validating pipelines may not load that information.
- Assume text beside inline elements can be significant.
- Distinguish absent text from whitespace-only text.
- Record parser validation and entity settings.
- Keep namespace-aware element identity in the content model.
Apply xml:space as an inherited signal
The xml:space attribute can communicate preserve or default intent and is inherited by descendants. It informs application handling but does not by itself prescribe one universal pretty-print algorithm.
Document how the selected parser and serializer treat xml:space, line-ending normalization, CDATA boundaries, and entity references. CDATA changes source spelling, not the character data represented after parsing.
Separate display, storage, and canonicalization
Render a pretty inspection view without replacing authoritative source when lossless round trips are required. If normalized output is part of the contract, define it for the specific vocabulary and test semantic equivalence after parsing.
- Do not parse XML with regular expressions.
- Do not trim all text nodes recursively.
- Preserve authenticated bytes until signature processing is complete.
- Use the required XML canonicalization algorithm for signatures.
Build serializer differential tests
Use Flashman's XML formatter for synthetic structures, diff for source and serializer outputs, HTML entities tool for public character references, regex only for narrow text diagnostics, and hash for fixture labels. These tools do not validate vocabulary semantics or signatures.
Test element-only and mixed content, leading and trailing spaces, adjacent inline elements, empty elements, xml:space inheritance, CDATA, predefined and custom entities, comments, processing instructions, namespaces, line endings, DTD and schema validation, parser upgrades, canonicalization, digital signatures, translations, and every parser and serializer in the data path.