1. Purpose
Match options control what to compare and how strictly, but different algorithms interpret these options differently. Understanding these differences is crucial for choosing the right configuration.
This page explains how the DOM and Semantic algorithms each handle match dimensions and provides guidance for migrating between algorithms.
2. Key Concept
The same match option settings can produce different comparison behavior depending on which algorithm you choose:
-
DOM algorithm uses options for element-by-element positional comparison
-
Semantic algorithm uses options during signature calculation and similarity matching
3. Algorithm Comparison
3.1. DOM Algorithm Match Behavior
The DOM algorithm applies match options during positional element comparison:
Characteristics: * Elements matched by position in document tree * Match options control comparison strictness at each position * No understanding of semantic relationships * Order matters significantly
How Options Are Used:
* text_content - Controls how text at each position is compared
* structural_whitespace - Controls whitespace comparison in structure
* attribute_order - Controls whether attribute order must match
* attribute_values - Controls how attribute values are compared
Best For: * Documents with similar structure * Traditional diff workflows * Fast comparisons * Stable, predictable results
3.2. Semantic Algorithm Match Behavior
The Semantic algorithm applies match options during signature calculation and similarity matching:
Characteristics: * Elements matched by semantic signatures * Match options influence signature generation * Understands moves, merges, splits * Order less critical (uses similarity scoring)
How Options Are Used:
* text_content - Included in element signature
* structural_whitespace - Affects structural signatures
* attribute_order - Ignored (attributes are unordered in signatures)
* attribute_values - Included in element signature
Best For: * Restructured documents * Detecting semantic changes * Operation-level analysis * Content evolution tracking
4. Match Dimension Handling by Algorithm
This table shows how each algorithm interprets each match dimension:
| Match Dimension | DOM Algorithm | Semantic Algorithm |
|---|---|---|
text_content |
Compares text at each position. |
Influences element signature. |
structural_whitespace |
Compares whitespace-only text nodes at each position |
Affects structural signature calculation. Normalized whitespace creates different signatures |
attribute_whitespace |
Compares whitespace in attribute values at each position |
Affects attribute value signatures. Normalized values create different signatures |
attribute_order |
|
Always ignored - attributes are unordered in semantic signatures |
attribute_values |
Compares attribute values at each position |
Attribute values included in element signature |
key_order (JSON/YAML) |
|
Always ignored - keys are unordered in semantic signatures |
comments |
Compares comments at each position. |
Comments can be included in signatures or ignored. Less impact than in DOM |
namespace_uri (XML) |
Always compared strictly - elements must have same namespace URI to match at each position |
Always included in element signature - elements must have same namespace URI for signatures to match |
5. Example: Same Options, Different Results
Here’s an example showing how the same match options produce different results with each algorithm:
5.1. Document Pair
<!-- Document 1 -->
<book>
<title>Canon Guide</title>
<author>John Doe</author>
</book>
<!-- Document 2 -->
<book>
<author>John Doe</author>
<title>Canon Guide</title>
</book>
5.2. DOM Algorithm Result
Canon::Comparison.equivalent?(doc1, doc2,
diff_algorithm: :dom,
match: {
text_content: :normalize,
attribute_order: :ignore
},
verbose: true
)
# Result: NOT EQUIVALENT
# Reason: Elements at positions don't match (title vs author)
# Even though content is identical, position matters
5.3. Semantic Algorithm Result
Canon::Comparison.equivalent?(doc1, doc2,
diff_algorithm: :semantic,
match: {
text_content: :normalize,
attribute_order: :ignore
},
verbose: true
)
# Result: EQUIVALENT (with MOVE operation)
# Reason: Elements have same signatures, just reordered
# Semantic algorithm detects this as a MOVE operation
6. Match Profile Behavior Differences
Match profiles also behave differently with each algorithm:
6.1. strict Profile
DOM Algorithm: * Exact positional matching * All elements must be in same positions * Whitespace must match exactly * Fast comparison
Semantic Algorithm: * Exact signature matching * Elements can be reordered * Signatures must match exactly * Slower but detects moves
7. Migration Guide
7.1. Switching from DOM to Semantic
When migrating from DOM to Semantic algorithm:
Expected Changes: 1. Reordered elements will be detected as MOVEs instead of DELETE+INSERT 2. attribute_order setting becomes irrelevant (always ignored) 3. Performance will be slower but more intelligent 4. Output format changes to operation-based
Configuration Adjustments:
# Before (DOM)
Canon::Comparison.equivalent?(doc1, doc2,
diff_algorithm: :dom,
match: {
attribute_order: :strict # This mattered
},
diff_mode: :by_line
)
# After (Semantic)
Canon::Comparison.equivalent?(doc1, doc2,
diff_algorithm: :semantic,
match: {
attribute_order: :ignore # Changed (but actually doesn't matter)
},
diff_mode: :by_object # Better for semantic output
)
What to Watch For: * Tests expecting positional differences may now pass (moves detected) * Diff output format changes significantly * Performance may be slower on large documents
7.2. Switching from Semantic to DOM
When migrating from Semantic to DOM algorithm:
Expected Changes: 1. MOVE operations will become DELETE+INSERT pairs 2. Reordered content will show as differences 3. Performance will be faster 4. Output format changes to line-based
Configuration Adjustments:
# Before (Semantic)
Canon::Comparison.equivalent?(doc1, doc2,
diff_algorithm: :semantic,
diff_mode: :by_object
)
# After (DOM)
Canon::Comparison.equivalent?(doc1, doc2,
diff_algorithm: :dom,
match: {
attribute_order: :ignore # May want to add this
},
diff_mode: :by_line # Better for DOM output
)
What to Watch For:
* Tests may now fail on reordered content
* Need to add attribute_order: :ignore if attribute order shouldn’t matter
* Diff output is less semantic, more positional
8. Choosing the Right Algorithm
9. Common Patterns
9.1. Pattern 1: Test-Friendly DOM Comparison
Canon::Comparison.equivalent?(expected, actual,
diff_algorithm: :dom,
match_profile: :spec_friendly,
verbose: true
)
# Ignores formatting but requires same structure
9.2. Pattern 2: Content-Only Semantic Comparison
Canon::Comparison.equivalent?(doc1, doc2,
diff_algorithm: :semantic,
match_profile: :content_only,
verbose: true,
diff_mode: :by_object
)
# True content comparison, structure-independent
9.3. Pattern 3: Hybrid Approach
# Try DOM first (fast)
if Canon::Comparison.equivalent?(doc1, doc2, diff_algorithm: :dom)
puts "Documents identical"
else
# Use semantic for detailed analysis
result = Canon::Comparison.equivalent?(doc1, doc2,
diff_algorithm: :semantic,
verbose: true,
diff_mode: :by_object
)
puts result.operations
end
10. Performance Implications
11. See Also
-
Algorithms - Detailed algorithm documentation
-
Match dimensions - All available dimensions
-
Match profiles - Preset configurations
-
Algorithm-Specific Output - How output differs
-
Choosing Configuration - Decision guide