How to implement the .withAttributeFilter in XMLUnit2.0 DiffBuilder?

969 Views Asked by At

I want my Java program to ignore few attributes while comparing 2 XMLs. I do not want to use the Node Filter as it would ignore the child elements as well from comparison. I tried different implementations for the attribute filter, but nothing worked. When I use Node filter, the filtering works. The problem is only the attribute filter. Can someone point out where am going wrong?

Part of the XML Test Document

<?xml version="1.0"?>
<!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.009/Fulfill.dtd">
<cXML payloadID="{hh896538025}" timestamp="2019-12-02">
<Header>
     <!lines of text>
</Header>
<Request>
        <ConfirmationRequest>
            <ConfirmationHeader operation="new" noticeDate="2019-12-20">
                <............>
            </ConfirmationHeader>
            <OrderReference orderID="jkd578zw6nr" orderDate="2019-12-20">
                <DocumentReference payloadID="l90p459s35" />
            </OrderReference>
        </ConfirmationRequest>
    </Request>
<cXML>

The XML to be compared is also of the same format.

Here is my code:

Diff diff =
            DiffBuilder.compare(testDocument)
           .withTest(ComparisonDocument)
           .checkForSimilar()
           .ignoreWhitespace()
           .withAttributeFilter(attr -> (attr.getName().equals("timestamp")) || (attr.getName().equals("noticeDate")) || attr.getName().equals("confirmID")                                                    || attr.getName().equals("orderID") || attr.getName().equals("orderDate"))
        // .withNodeFilter(node ->(node.getNodeName().equals("ConfirmationHeader") || node.getNodeName().equals("OrderReference")))
           .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder()            .whenElementIsNamed("ConfirmationItem").thenUse(ElementSelectors.byXPath("./Name", ElementSelectors.byNameAndText))                                                    .elseUse(ElementSelectors.byName).build())).build();
0

There are 0 best solutions below