1

For my Java program im using XMLUnit 1.5, wich compares two XML-Files (with Namespaces). But the XPathes, I get, do not contain any Namespaces. How I can tell XMLUnit todo that? Is that possible?

Here is my code:

        control = new FileReader(file1);
        test = new FileReader(file2);

        list = new ArrayList<Unterschied>();

        Diff diff;
        diff = new Diff(control, test);

        diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());

        if (diff.similar())
        {
            MessageDialog.openInformation(null, "Info", "Similar");
        }

        else if (!diff.similar())
        {

            DetailedDiff detDiff = new DetailedDiff(diff);
            detDiff.overrieElementQualifier(new ElementNameAndAttributeQualifier());
            @SuppressWarnings("unchecked")
            List<Object> differences = detDiff.getAllDifferences();


            for (Object object : differences) 
            {
                Difference difference = (Difference) object;
                if (!difference.isRecoverable() && !difference.getDescription().contains("number of child nodes"))
                {
                    controlXPath = difference.getControlNodeDetail().getXpathLocation();  // without Namespaces!
                    controlValue = difference.getControlNodeDetail().getValue();
                    testXPath = difference.getTestNodeDetail().getXpathLocation();   // without Namespaces!             
                    testValue = difference.getTestNodeDetail().getValue();

                    list.add(new Unterschied(controlOrt, controlWert, testOrt, testWert));
                }
            }

Later in my program I evaluating these XPathes for getting the correct position of this node. With these position I select the appropriate text in the editor, so that the user can see the Difference.

My code for evaluating:

 public void evaluateXPath(String xp, Node node) {


    XPath newXPath = new XPathFactoryImpl().newXPath();

    XPathExpression xpExp = newXPath.compile(xpath);

    NodeList nodeList = (NodeList) xpExp.evaluate(node, XPathConstants.NODESET);

    int length = nodeList.getLength();
    for (int i=0; i < length; i++) {

       IDOMNode iDOMnode = (IDOMNode) nodeList.item(i);
       textEditor.selectAndReveal(iDOMnode.getStartOffset(), iDOMnode.getEndOffset()-iDOMnode.getStartOffset());
    }
}

Thanks for your help and sorry for my bad english! ;)

1 Answers1

0

No, XMLUnit 1.x will only return plain XPath without any namespace indication at all.

XMLUnit 2.x (under development) will return XPaths using prefixes for namespaces that can be configured per NS-URI.

Stefan Bodewig
  • 3,260
  • 15
  • 22