---
title: "How to compare two XML files"
description: "Compare two XML files without formatting noise: canonicalize, re-indent, and diff, with commands for Mac, a Python script, and a side-by-side view."
author: "XML Lens editorial"
datePublished: 2026-11-08
dateModified: 2026-11-08
canonical: https://xmllens.ayushdev.com/guides/compare-xml-files
---

# How to compare two XML files

Normalize both files first, then compare them line by line. Canonicalizing and re-indenting removes differences in attribute order, empty-tag style, and indentation, so the diff shows only changed elements, attributes, and values.

XML Lens editorial · Reviewed 2026-11-08

## 1. Why a plain text diff shows too much

A text diff compares characters and lines. Two XML files can describe exactly the same data and still differ in indentation, line breaks, attribute order, or whether an empty element is written as <note/> or <note></note>. A line diff reports every one of those as a change.

In this example, the second order was exported by a different system. The only real change is the total, but a plain diff reports that every line changed.

```
<!-- old.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<order id="1042" status="paid">
  <customer>Ada Lovelace</customer>
  <total currency="GBP">42.00</total>
  <note/>
</order>

<!-- new.xml -->
<order status="paid" id="1042"><customer>Ada Lovelace</customer><total currency="GBP">42.50</total><note></note></order>

$ diff old.xml new.xml
1,6c1
< <?xml version="1.0" encoding="UTF-8"?>
< <order id="1042" status="paid">
...
> <order status="paid" id="1042"><customer>Ada Lovelace</customer>...
```

## 2. Decide what counts as a difference

Before choosing a method, decide which differences matter for your task. Most comparisons should ignore serialization details, but some differences are real data even when they look cosmetic.

- Usually safe to ignore: indentation between elements, line endings, attribute order, empty-element style, and the XML declaration.
- Check before ignoring: whitespace inside text values. XML processors pass all non-markup characters to the application, so "42.00 " and "42.00" can be different values to the receiving system.
- Usually significant: the order of sibling elements. Many schemas define a sequence, so moving an element can make a document invalid or change its meaning.
- Depends on the consumer: comments, processing instructions, and which namespace prefix is used for the same namespace URI.

## 3. Quick check on a Mac: canonicalize both files with xmllint

Canonical XML (C14N) is a W3C standard for writing XML in one consistent form. It sorts attributes and namespace declarations, writes empty elements as start-end tag pairs, normalizes line breaks, and removes the XML declaration. macOS includes xmllint at /usr/bin/xmllint, so you can try this in Terminal without installing anything.

Canonicalization keeps whitespace in character content, and xmllint keeps comments. That means two files with different indentation still differ after this step. Use it as a quick check when both files were indented the same way, or move on to the next method.

```
xmllint --c14n old.xml > old.c14n.xml
xmllint --c14n new.xml > new.c14n.xml
diff old.c14n.xml new.c14n.xml
```

## 4. Compare ignoring formatting with a short Python script

Python's standard library can canonicalize XML and strip the whitespace around text, then re-indent the result consistently. Run the script on both files and diff the two .normalized.xml outputs. With the example above, the diff now shows only the changed total.

The script needs Python 3.9 or later for ET.indent. It drops comments and may rename namespace prefixes, which is fine for spotting changes but means the output is for comparison only, not a replacement for the original file. Do not use strip_text when whitespace inside values matters.

```
import sys
import xml.etree.ElementTree as ET

# Usage: python3 normalize_xml.py old.xml new.xml
for path in sys.argv[1:]:
    flat = ET.canonicalize(from_file=path, strip_text=True)
    root = ET.fromstring(flat)
    ET.indent(root)
    with open(path + ".normalized.xml", "w", encoding="utf-8") as out:
        out.write(ET.tostring(root, encoding="unicode") + "\n")

$ python3 normalize_xml.py old.xml new.xml
$ diff old.xml.normalized.xml new.xml.normalized.xml
3c3
<   <total currency="GBP">42.00</total>
---
>   <total currency="GBP">42.50</total>
```

## 5. Review the differences side by side

A terminal diff is enough for small changes. For longer documents, a side-by-side view is easier to review: you see both versions, move from one change to the next, and can edit either side as you go.

In VS Code, run code --diff old.xml new.xml to open the two files in its diff editor. In XML Lens, open, drag, or paste each document into the two editable panes of the Compare workspace. Additions, removals, and changed lines are highlighted, and you can step through each difference. If the files are formatted differently, format both documents first so the comparison shows content changes rather than indentation. When the review needs to be shared, export the comparison as a Markdown, HTML, or PDF report.

## 6. Online XML compare tools: check what you upload

Browser-based XML diff tools are convenient for public sample files. Invoices, customer exports, configuration with credentials, and API payloads often contain personal or confidential data, so check how a site handles uploads before pasting a production file into it.

Comparing locally avoids that question. The xmllint and Python methods above run entirely on your computer, and XML Lens does not upload XML content, filenames, paths, or exported reports.

## 7. When a line diff is not enough

Normalizing and diffing lines works well when both files keep elements in the same order. It is less helpful in a few situations, and it is worth knowing them before you trust a clean or noisy result.

- Reordered siblings: if one system sorts records differently, every moved record appears as a removal and an addition. Sort both files by a key first, or use a tree-aware XML diff tool.
- Very large files: diff the normalized output of one record type at a time, or extract the records you care about with XPath first.
- Equivalent values in different forms: 42.0 and 42.00, or two date formats, are different text even when the receiving system treats them as equal. Validate against the schema and compare the values your process actually uses.

## Common questions

### How do I compare two XML files and ignore whitespace?

Canonicalize both files with whitespace stripped and re-indent them, then diff the results. The Python script in this guide does this with the standard library. xmllint --c14n alone normalizes markup but keeps indentation inside elements.

### Does attribute order matter in XML?

No. The XML specification does not give attribute order any meaning, and canonical XML sorts attributes so that order differences disappear before comparison.

### Does the order of elements matter when comparing XML?

Usually yes. Sibling element order is part of the document, and many schemas require a specific sequence. Treat reordered elements as a real change unless you know the consumer ignores order.

### Is it safe to compare XML files online?

It depends on the file and the site. For documents with personal, financial, or confidential data, compare locally with xmllint, a script, or a desktop tool so the content never leaves your computer.

### Can I compare XML files in VS Code?

Yes. Run code --diff old.xml new.xml to open both files in the diff editor. Format both files the same way first if their indentation differs.

## References

- [W3C Canonical XML Version 1.1](https://www.w3.org/TR/xml-c14n11/)
- [W3C XML 1.0, section 2.10: White Space Handling](https://www.w3.org/TR/xml/#sec-white-space)
- [Python xml.etree.ElementTree: canonicalize() and indent()](https://docs.python.org/3/library/xml.etree.elementtree.html)
- [libxml2 xmllint manual](https://gnome.pages.gitlab.gnome.org/libxml2/xmllint.html)
- [VS Code command line: --diff](https://code.visualstudio.com/docs/configure/command-line)

## Try the workflow in XML Lens

[Download XML Lens for Mac](https://apps.apple.com/us/app/xml-editor-lens-xpath/id6761771384?mt=12&ct=guide-compare-xml-files)

Explore the relevant Mac workspaces and their capabilities.

- [Compare XML files side by side on Mac](https://xmllens.ayushdev.com/xml-diff-tool)
- [View and format XML locally on Mac](https://xmllens.ayushdev.com/xml-viewer-formatter)
- [Merge XML files locally on Mac](https://xmllens.ayushdev.com/xml-merge)

[All XML guides](https://xmllens.ayushdev.com/guides)
