How to Compare Two XML Files and See What Changed
To compare two XML files, pretty-print both with the same indentation, put them side by side, and then sort the highlighted lines into real edits and layout noise. XML produces a lot of the second kind. Attributes in a different order, an empty element written two ways, or a different namespace prefix all show up as changes while meaning exactly the same thing.
This post covers which differences you can safely skip, the tools that remove them, and a real logging config with two edits buried among the noise. The side-by-side part runs on our XML compare page in your browser.
Why equivalent XML files diff as different
XML has a strict grammar (see the W3C XML specification), but it gives writers a lot of latitude in how the text is laid out. Two documents can describe the exact same data and still differ byte for byte, and a line-by-line comparison flags every one of those differences.
Attribute order is the biggest source of false alarms. The
XML Information Set
treats the attributes on an element as an unordered set, so
<user id="7" role="admin"/> and
<user role="admin" id="7"/> carry the same information.
The order of child elements is different: in most schemas it is part of
the document, and moving an element is a real change.
| What the diff shows | Does the data change? | What to do |
|---|---|---|
| Attributes in a different order | No, attribute order is not significant | Canonicalize both sides |
| 2-space vs 4-space indent | No | Pretty-print both the same way |
| Whitespace between elements | Usually not | Format, or strip insignificant whitespace |
<br/> vs <br></br> | No, same empty element | Canonicalize both sides |
| A different namespace prefix for the same URI | No, prefixes are arbitrary labels | Compare by namespace URI, not prefix |
| Child elements in a different order | Usually yes, element order matters | Investigate, this is likely real |
That last row is the one to watch. Attribute order is free, but the order of child elements is part of the document in most schemas. If you want the detail on how a parser sees all this, MDN has a solid reference on parsing XML with DOMParser.
Ways to compare XML
Each option removes a different amount of noise, so the right one depends on how messy the two files are.
| Method | Good for | Removes |
|---|---|---|
| Online diff with Format | Pasted snippets, SOAP responses, quick reviews | Indentation and line-break differences |
xmllint --c14n then diff | Files on disk | Attribute order, empty-element style, entity references |
| A namespace-aware parser (lxml, DOM) | Documents that mix prefixes | Prefix differences, by comparing the namespace URI |
git diff | Files already committed | Nothing; it is line-based |
libxml2
ships xmllint, which is already installed on most Linux and
macOS machines, so canonicalizing usually costs nothing to set up.
The routine, step by step
Here is how I check a config change when the files came from two different editors.
- Open the XML compare tool.
- Put the current file in the left pane and the changed one in the right.
- Click Format on each side. It puts every tag on its own line with two-space indentation, so a minified file and a hand-indented one line up.
- Read down the highlighted lines. Red on the left was removed, green on the right was added.
- For each changed line, ask whether only the attribute order or the empty-element style differs. If so, skip it; if there are many, canonicalize both files with xmllint and compare again.
Format is a layout pass, not a parser: it does not sort attributes or
collapse <x></x> into <x/>. The
comparison uses Google's
diff-match-patch
and pairs each edited line with its original, so inside a changed line
only the differing characters are highlighted, which makes a swapped
attribute easy to spot.
A worked example: a log4j2 config
Here is the Loggers section of a
log4j2 configuration
as deployed:
<Loggers>
<Logger name="com.shop.payments" level="info" additivity="false">
<AppenderRef ref="File"/>
</Logger>
<Root level="warn">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
And here is the version in a pull request, after the author's IDE reformatted it:
<Loggers>
<Logger additivity="false" level="debug" name="com.shop.payments">
<AppenderRef ref="File"></AppenderRef>
<AppenderRef ref="Console"/>
</Logger>
<Root level="warn">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
Every line inside the Logger element comes up as changed.
Sorted into noise and edits:
| Node | Deployed | Pull request | Change |
|---|---|---|---|
Logger/@level | info | debug | Modified |
Logger/AppenderRef[@ref="Console"] | – | present | Added |
Logger attribute order | name, level, additivity | additivity, level, name | Noise |
AppenderRef ref="File" | self-closing | start and end tag | Noise |
Two edits, and together they matter more than either looks. The payments
logger drops to debug, and because additivity is
false it previously wrote only to the file; now it also writes to the
console. That is a lot of payment detail in a container's stdout. The
reordered attributes and the expanded empty element were the IDE. After
xmllint --c14n on both files, the AppenderRef
lines match and the Logger line differs only in its level.
Canonical XML: the proper way to ignore noise
Formatting both sides handles indentation, but there is a standard built for exactly this problem. Canonical XML, defined by the W3C in Canonical XML 1.1, rewrites a document into a single normalized form: attributes sorted, empty elements expanded, whitespace in tags normalized, and default attributes made explicit. Two documents that differ only in those ways produce identical canonical output.
xmllint --c14n old.xml > old.c14n.xml
xmllint --c14n new.xml > new.c14n.xml
diff old.c14n.xml new.c14n.xml
One thing canonical form keeps: whitespace between elements. If one file
is indented and the other is not, run xmllint --format on
both first, then canonicalize, or paste the canonical output into the
browser tool and click Format there.
Namespaces: the part that confuses everyone
XML namespaces let two documents use the same vocabulary with different
prefix labels. <ns1:user> bound to a URI and
<u:user> bound to the same URI are the same
element; the prefix is just a local nickname. A text diff sees
ns1 versus u and flags a change that is not one.
Canonicalization does not fix this, because Canonical XML keeps prefixes
exactly as written. To ignore them you need a namespace-aware parser that
names each element by its URI. Python's lxml, for example, reports the tag
as {http://example.com/users}user whichever prefix the file
used. The
Namespaces in XML
spec is what to cite when a reviewer insists the prefix matters.
Smaller XML traps
| Gotcha | Why it bites | Fix |
|---|---|---|
| Character encoding | A UTF-8 and a UTF-16 file can hold the same text but differ byte for byte | Normalize encoding; the XML declaration states it |
| Entity references | & and a literal & can both appear for the same character | Canonicalize, which resolves entities consistently |
| CDATA vs escaped text | <![CDATA[a<b]]> and a<b are the same text content | Compare the parsed value, not the raw bytes |
| Significant whitespace | Inside xml:space="preserve", spaces matter and must not be stripped | Do not blindly trim; respect xml:space |
| Self-closing tags | <x/> and <x></x> are identical | Canonicalize so both render the same way |
When you need a tree diff instead
A side-by-side text diff is the right tool when a person is reviewing a change. It stops being enough when element order genuinely does not matter in your schema, or when a program has to apply the difference to a third file. For that, use a tree diff such as xmldiff, which reports edits as actions on nodes ("update attribute level", "insert element at this path") instead of changed lines.
Related tools
XML is rarely the only format you deal with. If you are comparing API payloads, JSON compare can also sort keys for you. Marked-up pages are easier to read on the HTML compare page, and environment settings line up well on the config compare tool. And when the two files are plain prose, not markup, you can compare two text files on the main page.
Frequently asked questions
- Can I paste a SOAP response or a config with internal hostnames into an online XML diff?
- On comparetext.org the two documents are compared in your browser tab and are not stored or sent anywhere unless you press Share, which saves both panes to create a link. So internal hostnames and IDs stay on your machine. Credentials are a different matter: strip passwords and API keys out of any document before pasting it into a web page, this one included.
- Why does my XML diff show every line as changed?
- Usually one file is minified onto a single line or indented differently from the other. Click Format on both sides of the XML compare page so every tag sits on its own line with the same indentation; most of the red and green disappears. If lines still differ only by attribute order or by
versus , canonicalize both files with xmllint --c14n and compare those instead. - Is attribute order significant in an XML diff?
- No. In XML the attributes on an element are an unordered set, so
<a x="1" y="2"/>and<a y="2" x="1"/>are equivalent. A plain text diff does not know this and will flag the reorder as a change. Canonical XML sorts attributes into a fixed order, so canonicalizing both sides before comparing makes the false positive disappear. Element order, by contrast, usually is significant. - How do I compare XML while ignoring namespace prefixes?
- Use a namespace-aware parser, not canonicalization. Canonical XML keeps prefixes exactly as written, so
ns1:userandu:userstill differ after xmllint --c14n even when both prefixes point to the same URI. A parser such as Python's lxml names every element by its namespace URI plus local name, so comparing the parsed trees ignores the prefix. A plain text diff cannot do this on its own. - How large an XML file can I compare in the browser?
- Files of a few thousand lines compare as you type, including a pretty-printed sitemap or a long Maven POM. The page aligns whole lines before it looks at characters, which keeps it quick. Multi-megabyte exports, such as a full database dump in XML, are better compared with xmllint and diff in a terminal, where nothing has to be drawn on screen.
- When should I use an XML tree diff instead of a text diff?
- Use a text diff when a person is reviewing the change: it shows both versions side by side and is easy to read once both files are formatted. Switch to a tree diff, such as the xmldiff Python library, when child order does not matter in your schema or when a script needs a list of edits it can apply. A tree diff reports node-level actions like an updated attribute or an inserted element at a given path.
Have two versions of a config or a feed? Paste them into the XML compare tool and click Format on both sides.