[xmlunit] Contains relation of two JXPaths

W

Wojciech Gdela

Hello,

I'm using XmlUnit to compare two XML documents, the documents may have
different values of some tag's attributes. Say i have XPath
"/thread/message/@id" which selects a set of attributes, and i want to
check if this set contains attribute selected by following XPath
"/thread[1]/message[2]/@id". How can i perform this check (possibly
using JXPath)?

For now i use regexp matching with following class:

import org.custommonkey.xmlunit.DifferenceListener;

class IgnoreAttribeValueDifferenceListener implements
DifferenceListener {
private String ignoredAttributePath;
private Set pathPattern;

public IgnoreAttribeValueDifferenceListener(String[] path) {
// ignoredAttributePath = path;
pathPattern = new HashSet();
for (int i = 0; i < path.length; i++) {
pathPattern.add(Pattern.compile(path));
}
}

public int differenceFound(Difference d) {
if (d.getId() == DifferenceConstants.ATTR_VALUE_ID) {
Iterator it = pathPattern.iterator();
while (it.hasNext()) {
Matcher m = ((Pattern)
it.next()).matcher(d.getTestNodeDetail().getXpathLocation());
if (m.matches()) {
return RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR;
}
}
}

return RETURN_ACCEPT_DIFFERENCE;
}

public void skippedComparison(Node arg0, Node arg1) {

}

}

And a snippet of code that uses it:

Diff d = new Diff(expected, current);
d.overrideDifferenceListener(new IgnoreAttribeValueDifferenceListener(
new String[] {
"/thread\\[[0-9]+\\]/message\\[[0-9]+\\]/@id"
}));
assertTrue(d.toString(), d.similar());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top