0

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();
  • Your filter says you only want to see attributes named timestamp, noticeDate or confirmID. Is this what you intend? It may be a problem of XML namespaces, even though I don't see any. If your attributes belong to an XML namespace then `getName` returns a "qualified name". – Stefan Bodewig Jan 24 '20 at 17:56
  • @StefanBodewig: Yes, I want to filter out only those specific attributes from comparison. I want the rest of the attributes in that node to be compared. For node filtering, i could use *getNodeName*. What would work for attributes if not *getName*. For now, I have been able to filter out these attributes by defining a DifferenceEvaluator. The below code worked for me: `.withDifferenceEvaluator(new IgnoreAttributeDifferenceEvaluator(attributesToIgnore))` Is there any other solution? – pavithra .s Feb 04 '20 at 17:23
  • In that case your `NodeFilter` is defined the wrong way around. It should return true if you want to compare the attribute, yours returns true for timestamp and so on. – Stefan Bodewig Feb 05 '20 at 05:31

0 Answers0