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());
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());