2.32 KB
Newer Older
Danijel Dragicevic's avatar
Danijel Dragicevic committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
# Comparing XML

The XML comparison is based on [xmlunit](, version 2.3.0.

For the demos in this documentation, I provided the XML as String in MEL expressions. This works only for really small XML
and the escaping of `<` and `>` makes it completely unreadable when you switch to the XML view in AnypointStudio.

Fortunately, xmlunit is quite flexible when it comes to input sources: It can consume Strings (as in the examples), byte arrays, InputStreams,
Files and some more sources. See [xmlunit documentation]( for details.

If you have bigger XML documents, place them in `src/test/resources` and load them with `getResourceAsString(...)`, e.g. like this:


## Simple example

So let's start with a simple example to compare two XML Strings:


As long as you ignore all the quoting, it's quite simple in XML, too:
<munit:test name="compare-xml" description="Munit Test">
    <munit:validation >
        <assertobjectequals:compare-xml doc:name="xml is xml"
            expected="#['&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;a&gt;&lt;b&gt;&lt;/b&gt;&lt;/a&gt;']"
            actual="#['&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;a&gt;&lt;b&gt;&lt;/b&gt;&lt;/a&gt;']"

## Handling whitespace and comments

Usually we don't want to compare our XML documents character by character, otherwise we could just use the built in assert of MUnit.

We can select between three modes of comparison:

* NORMALIZE_WHITESPACE : Normalize Text-Elements by removing all empty text nodes and normalizing the non-empty ones.
* IGNORE_WHITESPACE: Ignore whitespace by removing all empty text nodes and trimming the non-empty ones.
* IGNORE_COMMENTS: Will remove all comment-Tags "<!-- Comment -->" from test- and control-XML before comparing.
"normalized" in this context means all whitespace characters are replaced by space characters and consecutive
whitespace characters are collapsed.

For a complete example of the test suite see [xml-demos.xml](xml-demos.xml).