https://github.com/aknopov/xml-comparator
GoLang library for comparing XML strings
https://github.com/aknopov/xml-comparator
comparison golang library xml
Last synced: over 1 year ago
JSON representation
GoLang library for comparing XML strings
- Host: GitHub
- URL: https://github.com/aknopov/xml-comparator
- Owner: aknopov
- License: mit
- Created: 2024-12-26T19:20:55.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-31T16:05:23.000Z (over 1 year ago)
- Last Synced: 2025-01-31T17:20:27.112Z (over 1 year ago)
- Topics: comparison, golang, library, xml
- Language: Go
- Homepage:
- Size: 77.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README


[](https://opensource.org/licenses/MIT)
[](https://pkg.go.dev/github.com/aknopov/xmlcomparator)
# XmlComparator
GoLang library for comparing XML strings
## Overview
API consists of two functions -
```
xmlcomparator.CompareXmlStrings(sample1 string, sample2 string, stopOnFirst bool) []string
xmlcomparator.CompareXmlStringsEx(sample1 string, sample2 string, stopOnFirst bool, ignored []string) []string
```
that return a list of detected differences between two XML samples. Comparison can be stopped on the first occasion - `stopOnFirst=true`. The second form takes a list of RegEx strings to be used as a filter for ignored differences.
Each entry in the returned list contains the XML path to the node like `..., path='/note/to[0]'`. Path elements might contain zero-based index of an element in the siblings list.
When a difference in children elements is detected, the message has the form `Children differ: counts 3 vs 4: ...` where the first number is the count of children in the first sample.
Mismatched child elements in the `diffs` list have two numbers. The first, in square brackets, is the index in the sibling nodes list.
The second - suffix like `:+1` or `:-3` is the count of consecutive mismatched elements with the same name. A positive number relates to the count of elements in `sample1`, negative - to `sample2`.
Example of usage in the code -
```go
import (
"github.com/aknopov/xmlcomparator"
)
...
xmlSample1 := ""
xmlSample2 := ""
diffs := xmlcomparator.CompareXmlStrings(xmlSample1, xmlSample2, false)
assert.Equal([]string{"Children order differ for 2 nodes, path='/a'"},
CompareXmlStrings(xmlSample1, xmlSample2, true))
xmlSample3 := ``
xmlSample4 := ``
assert.Equal([]string{"Children differ: counts 3 vs 4: c[0]:+2, e[1]:-3, path='/a/b'"}, CompareXmlStrings(xmlSample3, xmlSample4, false))
diffs := CompareXmlStringsEx(xmlString1, xmlMixed, false, []string{`Node texts differ: '.+' vs '.+'`})
assert.Equal(1, len(diffs))
xmlString5 := `Node Content`
xmlString6 := `Another Content`
diffs = CompareXmlStringsEx(xmlString5, xmlString6, false, []string{`Node textsNodes test differ: '.+' vs '.+'`})
assert.Equal(0, len(diffs))
// To get more insite
recorder := ComputeDifferences(xmlString1, xmlMixed, false, []string{})
assert.Equal(3, len(recorder.Diffs))
assert.Equal(DiffContent, recorder.Diffs[0].GetType())
assert.Equal("Node texts differ: 'Jani' vs 'Tove', path='/note/from[1]'", recorder.Diffs[0].DescribeDiff())
assert.Equal("/note/from[1]", recorder.Diffs[0].XmlPath())
```