Prince of Sweden
Prince of Sweden

Reputation: 475

XMLUnit - NodeFilter does not return anything

I'm currently trying to work a bit with XML Unit. I have two documents, test and control that I want to compare.

My scenario is that I only want to test two things(version and id) and ignore the rest.

Here is what I have tried to do:

   final org.xmlunit.diff.Diff result = DiffBuilder
            .compare(controlxml)
            .withTest(testxml)
            .withNodeFilter(node -> node.getNodeName().equals("id") && node.getNodeName().equals("version"))
            .build();

This does not work, as I don't get any diffs back. It seems to filter out every difference.

If I do this:

.withNodeFilter(node -> !node.getNodeName().equals("name") ...)

I can filter out the things I don't want(such as name in the example). However, this is not very feasible as there are plenty of things I don't want to compare.

Do I really have to explicitly define each one I want to filter out? Why can't I just tell it the two I want with "node.getNodeName().equals" and it will auto filter out the rest?

Either I am doing something wrong or I'm lost. Any help is appreciated in this matter, dear Stack Overflow.

Upvotes: 2

Views: 722

Answers (1)

Jordan Miller
Jordan Miller

Reputation: 143

Use or (||) not and (&&)

final org.xmlunit.diff.Diff result = DiffBuilder
        .compare(controlxml)
        .withTest(testxml)
        .withNodeFilter(node -> node.getNodeName().equals("id") || node.getNodeName().equals("version"))
        .build();

Upvotes: 1

Related Questions