1. Purpose

Canon’s comparison system uses a 4-layer architecture where each layer has a distinct responsibility. Understanding this pipeline is essential for configuring Canon effectively.

2. The 4-Layer Flow

Canon processes document comparisons through four sequential layers:

graph TD
    A[Input Documents] --> B[Layer 1: Preprocessing]
    B --> C[Layer 2: Algorithm Selection]
    C --> D[Layer 3: Match Options]
    D --> E[Layer 4: Diff Formatting]
    E --> F[Output]

    style B fill:#e1f5ff
    style C fill:#fff4e1
    style D fill:#ffe1f5
    style E fill:#e1ffe1

3. Layer Overview

3.1. Layer 1: Preprocessing

Purpose: Normalize documents before comparison

Options: * none - No preprocessing (default) * c14n - Canonicalize using format-specific rules * normalize - Normalize whitespace * format - Pretty-print with consistent formatting

When this runs: Before any comparison takes place

Documentation: See Preprocessing

3.2. Layer 2: Algorithm Selection

Purpose: Choose the comparison strategy

Options: * dom - DOM-based positional comparison (default, stable) * semantic - Tree-based semantic diff (experimental, intelligent)

Impact: Determines how Layers 3 and 4 behave

Documentation: See Algorithms

3.3. Layer 3: Match Options

Purpose: Configure what to compare and how strictly

Key Concept: This layer is algorithm-specific - each algorithm interprets match options differently.

Components: * Match dimensions (granular control) * Match profiles (preset combinations) * Algorithm-specific behaviors

3.4. Layer 4: Diff Formatting

Purpose: Control how differences are displayed

Key Concept: This layer is algorithm-specific - each algorithm generates different output types.

Components: * Diff mode (by_line, by_object) * Colors and symbols * Context and grouping * Character visualization

4. DiffNode Data Flow

4.1. How differences flow through the layers

DiffNode objects are created in Layer 2, enriched with metadata, and flow through to Layer 4 for rendering:

graph LR
    A[Layer 2: Algorithm] --> B[Create DiffNode]
    B --> C[Enrich Metadata]
    C --> D[Layer 3: Classification]
    D --> E[Layer 4: Rendering]
    E --> F[Formatted Output]

    C --> C1[PathBuilder]
    C --> C2[NodeSerializer]
    C1 --> G[path]
    C2 --> H[serialized_before/after]
    C2 --> I[attributes_before/after]

    style A fill:#fff4e1
    style C fill:#e1f5ff
    style E fill:#e1ffe1

4.2. Layer 2: DiffNode creation

Each algorithm creates DiffNode objects when it finds differences:

# DOM algorithm: Creates DiffNode during element-by-element comparison
diff_node = Canon::Diff::DiffNode.new(
  node1: element1,
  node2: element2,
  dimension: :text_content,
  reason: "Text content differs"
)

# Semantic algorithm: Creates DiffNode from tree operations
diff_node = Canon::Diff::DiffNode.new(
  node1: nil,
  node2: inserted_node,
  dimension: :element_structure,
  reason: "Element inserted"
)

4.3. Metadata enrichment

After creation, DiffNodes are enriched with metadata for Layer 4 rendering:

# Enriched with:
{
  path: "/#document/div[0]/p[1]/span[2]",      # Canonical location
  serialized_before: "<span>Old text</span>",  # Captured state
  serialized_after: "<span>New text</span>",   # Captured state
  attributes_before: {"id" => "old"},          # Normalized attrs
  attributes_after: {"id" => "new"}            # Normalized attrs
}

Enrichment utilities:

  • PathBuilder: Generates canonical paths with ordinal indices

  • NodeSerializer: Library-agnostic serialization of node content

  • Attribute extraction: Normalized attribute hashes

See DiffNode Enrichment for implementation details.

4.4. Layer 3: Classification

DiffNodes are classified to determine their impact:

diff_node.normative = true   # Affects semantic equivalence
diff_node.formatting = true  # Purely cosmetic difference

This classification affects whether differences cause equivalent? to return false.

4.5. Layer 4: Rendering

Layer 4 formatters use enriched metadata to display differences:

🔍 DIFFERENCE #1/3 [NORMATIVE]
════════════════════════════════════════════════════════════════════════
Dimension: text_content
Location:  /#document/div[0]/p[1]/span[2]

⊖ Expected (File 1):
   <span>Old text</span>

⊕ Actual (File 2):
   <span>New text</span>

✨ Changes:
   Text content changed from "Old text" to "New text"
  • The Location field uses the enriched path

  • The before/after content uses serialized_before/after

  • Attribute differences use attributes_before/after

This ensures accurate display regardless of which parsing library was used.

4.6. Benefits of enriched DiffNodes

Library flexibility: Layer 4 works with any parsing library through enriched metadata

Performance: Metadata captured once at diff creation, not recomputed during rendering

Accuracy: Shows actual node state when difference was found, not current state

Debuggability: Ordinal indices in paths make it easy to locate specific elements

5. Complete Example

Here’s a full 4-layer configuration showing all layers working together:

5.1. Ruby API

result = Canon::Comparison.equivalent?(doc1, doc2,
  # Layer 1: Preprocessing
  preprocessing: :normalize,

  # Layer 2: Algorithm
  diff_algorithm: :semantic,

  # Layer 3: Match Options
  match_profile: :spec_friendly,

  # Layer 4: Diff Formatting
  verbose: true,
  diff_mode: :by_object,
  use_color: true,
  context_lines: 3
)

5.2. CLI

canon diff file1.xml file2.xml \
  --preprocessing normalize \          # Layer 1
  --diff-algorithm semantic \          # Layer 2
  --match-profile spec_friendly \      # Layer 3
  --diff-mode by_object \              # Layer 4
  --verbose \                          # Enable Layer 4 output
  --context-lines 5                    # Layer 4 option

6. Layer-by-Layer Build-Up

Let’s see how each layer adds to the configuration:

6.1. Just Layer 1

Canon::Comparison.equivalent?(doc1, doc2,
  preprocessing: :normalize
)
# Documents are normalized, then compared using default DOM algorithm
# with strict matching and by-line diff output

6.2. Layers 1 + 2

Canon::Comparison.equivalent?(doc1, doc2,
  preprocessing: :normalize,
  diff_algorithm: :semantic
)
# Documents are normalized, then compared using semantic algorithm
# with strict matching and by-object diff output (semantic's natural mode)

6.3. Layers 1 + 2 + 3

Canon::Comparison.equivalent?(doc1, doc2,
  preprocessing: :normalize,
  diff_algorithm: :semantic,
  match_profile: :spec_friendly
)
# Documents are normalized, compared using semantic algorithm
# with spec_friendly matching (ignores formatting differences)
# Default diff output is by-object

6.4. All 4 Layers

Canon::Comparison.equivalent?(doc1, doc2,
  preprocessing: :normalize,
  diff_algorithm: :semantic,
  match_profile: :spec_friendly,
  verbose: true,
  diff_mode: :by_line,
  use_color: true
)
# Complete configuration with all layers specified
# Documents normalized, semantic comparison, spec-friendly matching,
# traditional line-based diff output with colors

7. Layer Interaction Matrix

This table shows common configuration patterns:

Layer 1 Layer 2 Layer 3 Layer 4 Result

none

dom

strict

by_line

Traditional exact comparison

normalize

dom

spec_friendly

by_line

Test-friendly comparison

c14n

dom

content_only

by_object

Canonical structure view

none

semantic

strict

by_object

Semantic operations view

normalize

semantic

rendered

by_line

Rendered diff with operations

8. Key Principles

8.1. Layer Independence

Each layer has a distinct purpose: * Layer 1: Document transformation * Layer 2: Comparison strategy * Layer 3: Match criteria * Layer 4: Output presentation

8.2. Algorithm Specificity

Layers 3 and 4 are interpreted differently by each algorithm: * The same match options may behave differently with DOM vs Semantic * Diff modes have different natural fits for each algorithm * Understanding this is crucial for effective configuration

8.3. Default Behavior

If you don’t specify a layer: * Layer 1: none (no preprocessing) * Layer 2: dom (stable algorithm) * Layer 3: strict (exact matching) * Layer 4: by_line for DOM, by_object for Semantic

9. Common Patterns

9.1. Testing XML Generation

Canon::Comparison.equivalent?(expected, actual,
  preprocessing: :normalize,       # Ignore formatting
  match_profile: :spec_friendly,   # Focus on content
  verbose: true                    # Show differences
)

9.2. Debugging Test Failures

Canon::Comparison.equivalent?(expected, actual,
  diff_algorithm: :semantic,       # Detect moves/changes
  verbose: true,
  diff_mode: :by_object,           # See operations
  use_color: true                  # Easier to read
)

9.3. Content-Only Comparison

Canon::Comparison.equivalent?(doc1, doc2,
  preprocessing: :format,          # Normalize structure
  match_profile: :content_only,    # Ignore all formatting
  verbose: true
)

10. Anti-Patterns to Avoid

10.1. Over-Configuration

# DON'T: Too many conflicting options
Canon::Comparison.equivalent?(doc1, doc2,
  preprocessing: :c14n,
  diff_algorithm: :dom,
  match: {
    text_content: :ignore,         # Conflicts with preprocessing
    structural_whitespace: :strict  # Conflicts with preprocessing
  }
)

10.2. Wrong Algorithm/Mode Combination

# SUBOPTIMAL: Semantic algorithm with by-line mode loses operation info
Canon::Comparison.equivalent?(doc1, doc2,
  diff_algorithm: :semantic,
  diff_mode: :by_line  # Better to use by_object with semantic
)

11. See Also