1. Problem statement
A common pattern in Metanorma and similar XML-generating libraries is to compare a compact generated document (no inter-element whitespace) against a hand-indented fixture stored as an RSpec heredoc:
expected_xml = <<~XML
<root>
<fmt-title>
<semx element="title" source="_">Foreword</semx>
</fmt-title>
</root>
XML
received_xml = "<root><fmt-title><semx element=\"title\" source=\"_\">Foreword</semx></fmt-title></root>"
When fmt-title and semx are classified as :collapse whitespace elements
(their inline content matters but all whitespace forms are equivalent), the
comparison correctly detects whitespace differences: the fixture has text nodes
"\n " and "\n" inside <fmt-title> that are absent from the compact
received document.
This is the right behaviour in general. However, for fixture files that cannot be written as compact single-line strings — because doing so would produce unreadable 1000-character lines — the indentation whitespace in the fixture is a necessary formatting artefact, not a meaningful content difference.
1.1. Why the whitespace cannot simply be stripped
The whitespace classification is intentionally asymmetric across elements:
-
Structural container elements (e.g.
<clause>,<foreword>) are:strip— their inter-element whitespace is already dropped in both compact and indented forms. -
Mixed-content elements (e.g.
<fmt-title>,<semx>) are:collapse— the presence of a space between inline children is significant (it represents a word-boundary), but its exact form is not.
Stripping all whitespace from the fixture would destroy the signal inside
mixed-content elements: <p>See <em>note</em></p> would become identical to
<p>See<em>note</em></p>, hiding a real content difference.
2. Solution: asymmetric pretty-print flags
Canon provides two match options and two display-preprocessing flags that
instruct the relevant subsystem to treat whitespace-only text nodes that begin
with "\n" as structural indentation from the pretty-printer and drop them
from the comparison. They operate independently so that only the
actually-pretty-printed side is affected.
pretty_printed_expected-
When
true, whitespace-only text nodes that start with"\n"in:collapse-classified elements of the expected (first / fixture) document are dropped before comparison and before display. Use this when fixture files are indented heredocs but received XML is compact. pretty_printed_received-
When
true, the same treatment applies to the received (second / actual) document. Use this when the received output may be pretty-printed but the fixture is compact.
2.1. The heuristic explained
The heuristic is:
Whitespace-only text node starts with "\n" → structural indentation → drop
Whitespace-only text node has no "\n" → inline content space → keep
The reasoning: compact Metanorma XML (and most programmatic XML serializers)
never emit a bare "\n" immediately after a closing or opening tag inside a
mixed-content element. If a fixture heredoc contains such a "\n", it was
introduced by the human author formatting the fixture for readability — it is
not part of the original document content.
A space not preceded by "\n" is treated as real content: in
<p>See <em>note</em></p>, the text node "See " starts with "S", so its
trailing space is kept and normalized to a single ░ symbol in the display.
This heuristic is only activated for :collapse elements. :preserve
elements always preserve every whitespace character regardless of these flags.
:strip elements already drop all whitespace and are unaffected.
|
3. Configuration
3.1. Comparison (equivalence detection)
Pass pretty_printed_expected or pretty_printed_received inside the match:
hash of Canon::Comparison.equivalent? or the RSpec be_xml_equivalent_to
matcher:
# One-off: fixture is indented, received is compact
result = Canon::Comparison.equivalent?(
fixture_xml,
received_xml,
match: {
collapse_whitespace_elements: %w[fmt-title semx],
pretty_printed_expected: true,
}
)
# RSpec global config in spec_helper.rb
Canon::Config.configure do |cfg|
cfg.xml.match.options = {
collapse_whitespace_elements: %w[fmt-title semx],
pretty_printed_expected: true,
}
end
3.2. Display preprocessing (:normalize_pretty_print)
When using display_preprocessing: :normalize_pretty_print, set the
corresponding diff-config flags so that the display side also strips structural
indentation from only the flagged side:
Canon::Config.configure do |cfg|
cfg.xml.diff.display_preprocessing = :normalize_pretty_print
cfg.xml.diff.collapse_whitespace_elements = %w[fmt-title semx]
# Drop structural indentation from the indented fixture side only
cfg.xml.diff.pretty_printed_expected = true
cfg.xml.diff.pretty_printed_received = false # compact received is unchanged
end
4. Behaviour by whitespace class
Element class |
|
|
Note |
|
All inter-element whitespace dropped (both sides) |
Same — no change |
Already dropped; flag has no effect |
|
|
|
Inline content spaces ( |
|
Every whitespace character compared verbatim |
Same — no change |
|
5. Example walkthrough
5.1. Fixture (hand-indented heredoc)
<root>
<clause>
<fmt-title>
<semx element="title" source="_">Foreword</semx>
</fmt-title>
</clause>
</root>
Text nodes inside <fmt-title> after Nokogiri parsing:
-
"\n "between<fmt-title>open tag and<semx>— whitespace-only, starts with"\n" -
"\n "between</semx>and</fmt-title>— whitespace-only, starts with"\n"
5.2. Received (compact Metanorma output)
<root><clause><fmt-title><semx element="title" source="_">Foreword</semx></fmt-title></clause></root>
No whitespace nodes inside <fmt-title>.
6. Interaction with :pretty_diff mode
:pretty_diff mode applies display_preprocessing to both documents before
running a text-LCS diff on the resulting lines. When
pretty_printed_expected: true is set on the formatter, XmlNormalized is
instantiated with pretty_printed: true for the expected-side printer only.
This drops the "\n"-starting whitespace visualization from the expected
side’s serialized lines, so the LCS diff sees identical lines for purely
structural indentation differences.
Canon::DiffFormatter.new(
mode: :pretty_diff,
display_preprocessing: :normalize_pretty_print,
collapse_whitespace_elements: %w[fmt-title semx],
pretty_printed_expected: true, # strip structural \n nodes from fixture side
pretty_printed_received: false, # compact received side is unchanged
)
7. Relation to deprecated normalize_pretty_print_ignore_structural_newlines
The deprecated flag normalize_pretty_print_ignore_structural_newlines applied
the newline-stripping heuristic to both sides simultaneously and without
regard to whitespace classification.
The new pretty_printed_expected / pretty_printed_received flags replace it
with a more granular design:
| Old flag | New equivalent |
|---|---|
|
|
The old flag is deprecated and still emits a warning; use the new flags for all new code.
8. See also
-
Display preprocessing — the
display_preprocessingoption andXmlNormalizedserializer -
Match options overview — whitespace sensitivity classification
-
Options across interfaces — cross-interface reference table
-
Environment variables —
CANON_XML_DIFF_PRETTY_PRINTED_EXPECTED,CANON_XML_DIFF_PRETTY_PRINTED_RECEIVED