1

I have to compare 2 xml strings.

Actual

<?xml version="1.0" encoding="utf-8"?>
    <entry xmlns="http://www.w3.org/2005/Atom"
        xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
        xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
        xml:base="http://localhost:49531/ODataV2.svc/ODataV2.svc/">
        <content type="application/xml">
            <m:properties>
                <d:ID>2000</d:ID>
                <d:ReleaseDate>1992-01-01T00:00:00</d:ReleaseDate>
                <d:Rating>4</d:Rating>
                <d:Price>2.5</d:Price>
            </m:properties>
        </content>
    </entry>

Expected

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
    xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
    xml:base="http://localhost:55615/ODataV2.svc/ODataV2.svc/">
    <content type="application/xml">
        <m:properties>
            <d:ID>2000</d:ID>
            <d:ReleaseDate>1992-01-01T00:00:00</d:ReleaseDate>
            <d:Rating>4</d:Rating>
            <d:Price>2.5</d:Price>
        </m:properties>
    </content>
</entry>

the below code fails because the port number in attribute xml:base is different in the root node.

XMLAssert.assertXMLEqual(Actual, Expected);

Expected attribute value 'http://localhost:55615/ODataV2.svc/ODataV2.svc/' but was 'http://localhost:49531/ODataV2.svc/ODataV2.svc/'

the port number in the unit test changes in runtime. Is there a way to ignore the particular attribute and compare the xml.

  • 1
    possible duplicate of http://stackoverflow.com/questions/1241593/java-how-do-i-ignore-certain-elements-when-comparing-xml & http://stackoverflow.com/questions/5249031/xmlunit-ignoring-id-attribute-in-comparison – Redlab Feb 07 '17 at 09:35

4 Answers4

3

Found the Solution. I used custommonkey library for xml compare. Instead of XMLAssert.assertXMLEqual(Actual, Expected);

I used the below code to decide the difference.

Diff diff=XMLUnit.compareXML(Expected,Actual);
    diff.overrideDifferenceListener(new DifferenceListener() {
       public void skippedComparison(Node arg0, Node arg1) {
       }

       public int differenceFound(Difference difference) {
        if (difference.getControlNodeDetail().getXpathLocation().contains("entry")) {
            return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
        } else {
            return DifferenceListener.RETURN_ACCEPT_DIFFERENCE;
        }
        }
    });
XMLAssert.assertXMLIdentical(diff,true);
2

XMLUnit 2.x has the concept of NodeFilters and AttributeFilters which are simple Predicates.

assertThat(Actual, isIdenticalTo(Expected)
    .withAttributeFilter(a -> !"base".equals(a.getName())))

would probably work.

Stefan Bodewig
  • 3,260
  • 15
  • 22
1

I know wont be the coolest or fancies solution , but you could 'parse' both XML before comparing them and remove or change the value of that attribute to something similar in both, so when you later compare them you are sure that thing wont affect the result

Armando SM
  • 142
  • 5
  • In general it's the only solution. The way to ignore differences is to mask them out by normalizing both values (typically using XSLT) to exclude the areas/nodes where you don't care about differences. – Michael Kay Feb 07 '17 at 11:43
-1

Perhaps you can configure the Namespace Context:

http://xmlunit.sourceforge.net/api/org/custommonkey/xmlunit/XMLUnit.html#getXpathNamespaceContext()

thopaw
  • 3,796
  • 2
  • 17
  • 24