XML diff

Compare two XML documents by meaning: indentation, attribute order and optional comments are ignored, namespaces are resolved.

loading XML diff
About this tool

What is an XML diff?

An XML diff compares two XML documents by meaning instead of by characters. XML has a lot of freedom that carries no information: indentation and line breaks between elements, the order of attributes, single or double quotes, CDATA or plain text, & written in different but equivalent ways. A text diff reports every one of those as a change, so a file that went through a different serializer looks completely rewritten.

This tool parses both documents with a strict XML 1.0 parser and compares the trees. It lists each real difference with its type (element added, removed or moved; attribute added, removed or changed; text changed; comment) and an XPath-style path such as /order/items/item[2]/@qty, showing the value before and after. An insertion in the middle of a list does not make the following siblings look changed. Namespaces are resolved by URI, so renaming a prefix is not a difference but changing the URI is, and the default namespace does not apply to attributes. Below the list you also get a text diff of both documents rewritten with the same indentation and attributes in alphabetical order, which you can copy as a unified diff or download as a .diff file.

It helps with SOAP and API payloads, configuration files, SVG, Maven or Android files, exports from other systems and test fixtures. Everything runs in your browser: nothing is uploaded, and nothing external is fetched.

How to compare two XML documents

  1. Paste the old XML in Original and the new one in Changed. The page opens with an order example loaded: default namespace, attributes in a different order, different indentation, a comment, a changed e-mail, a changed quantity and a new item.
  2. Read the list of differences. Each row gives the type, the path and the before → after values.
  3. Decide about comments. Ignoring them is on by default. Turn it off to see comment changes too.
  4. Turn on "Ignore the order of child elements" if the order does not matter in your format. It is off by default, so a child that changed position is reported as "Element moved".
  5. Check the formatted text diff below if you want to see the changes in context, then copy the unified diff or download the .diff.
  6. If a side is not well-formed, the error names the side, line, column and reason. Click it to select that line.

Examples

Same document, different formatting

<order id="7" status="paid"><total>10</total></order>
<order status='paid' id='7'>
  <total>10</total>
</order>

No differences are reported: the attribute order, the quotes and the whitespace between elements are not part of the meaning.

A changed attribute and a new item

<items><item sku="A1" qty="1"/></items>
<items><item sku="A1" qty="2"/><item sku="B2" qty="1"/></items>

The result lists an attribute changed at /items/item[1]/@qty (1 → 2) and an element added at /items/item[2]. The first item is not marked as changed because of the second.

A prefix change that is not a change

<a:book xmlns:a="urn:lib"/> and <b:book xmlns:b="urn:lib"/> are the same element, because both resolve to the namespace urn:lib. Change the URI to urn:library and it is reported as a difference.

A malformed document

Changed, line 3, column 9: closing tag </item> does not match <items>

Fix that side and the comparison continues.

Common mistakes

  • Comparing as text when the source is a different serializer. If one export sorts attributes or rewrites quotes, a text diff buries the real changes. Compare by meaning first.
  • Forgetting that the order of children matters by default. Two lists with the same items in a different order show "Element moved" unless you turn on the option to ignore order.
  • Expecting the default namespace to apply to attributes. An unprefixed attribute has no namespace, even inside an element that has one.
  • Leaving an entity undefined. &nbsp; or &foo; is not valid XML unless a DOCTYPE defines it. Use &#160; or declare the entity.
  • Using more than one root element. A document needs exactly one root. Wrap fragments in a parent element before comparing.
  • Expecting mixed text to be compared word by word. Text with elements inside it is compared as a single text.

Frequently asked questions

Is my XML uploaded?

No. Parsing and comparing happen in your browser. DOCTYPE is accepted only with simple internal entities, and nothing external is ever fetched.

Does it validate against an XSD or a DTD?

No. It checks that the XML is well-formed and compares two documents. Schema validation is a different job.

Does it match items by their id attribute?

No. Items with the same name are paired by content and order, not by the identity of an id. If you reorder records that have ids, turn on the option to ignore order.

How reliable is the parser?

It was checked against expat (Python) on 60 documents: the same well-formed verdict and the same error lines. Semantic equality was also checked against W3C canonical XML (C14N). Processing instructions are ignored.

What should I use next?

To tidy a file first, use the XML formatter. To work with the data, try XML to JSON or JSON to XML. For plain text there is the text diff, for JSON the JSON diff, and for documents the Markdown diff.

Report a problem