当XML doc包含名称空间时,XMLUnit Diff测试XPath

时间:2017-03-15 08:55:58

标签: xpath xmlunit

我想比较2个xml文档

d1.xml



<?xml version="1.0" encoding="UTF-8"?>
<test:process xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/Application2/FileRead1/BPELProcess1 xsd/BPELProcess1.xsd" xmlns:test="http://xmlns.oracle.com/Application2/FileRead1/BPELProcess1">
    <test:input1>RAVI1</test:input1>
   <test:input2>RAVI2</test:input2>
</test:process>
&#13;
&#13;
&#13;

d2.xml

&#13;
&#13;
<?xml version="1.0" encoding="UTF-8"?>
<test:process xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/Application2/FileRead1/BPELProcess1 xsd/BPELProcess1.xsd" xmlns:test="http://xmlns.oracle.com/Application2/FileRead1/BPELProcess1">
    <test:input1>RAVI1</test:input1>
   <test:input2>RAVI2</test:input2>
</test:process>
&#13;
&#13;
&#13;

使用以下代码编写java代码以对这些XML文件执行diff操作

&#13;
&#13;
 public static void main(String[] args) throws Exception {
              
       String sourceXml = new String(readAllBytes(get("c:\\temp\\d1.xml")));
       String targetXml = new String(readAllBytes(get("c:\\temp\\d2.xml")));
       
    XPathEngine engine = new JAXPXPathEngine(); 
    
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
            
        Document sourceDoc = Convert.toDocument(Input.fromString(sourceXml).build(), dbf);
       
        Document targetDoc = Convert.toDocument(Input.fromString(targetXml).build(), dbf);
        
        Diff myDiff = DiffBuilder.compare(sourceDoc).withTest(targetDoc)
                    .checkForSimilar()
                                 .checkForIdentical()
                    .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndAllAttributes))
                    .ignoreWhitespace() 
                    .withNamespaceContext(null)
                    .build();
   
        Iterator<Difference> iter = myDiff.getDifferences().iterator();
           int size = 0;
           while (iter.hasNext()) {
             
              Difference d1 =  iter.next();
               System.out.println(d1);
             
            System.out.println(d1.getComparison().getControlDetails().getXPath()  +" (SOURCE) -->  "+d1.getComparison().getControlDetails().getValue());
            System.out.println(d1.getComparison().getTestDetails().getXPath()  +" (TARGET) -->  "+d1.getComparison().getTestDetails().getValue());
              
               System.out.println();
               size++;
           }
    }
    
&#13;
&#13;
&#13;

这是输出,我正在

预期的文字价值&#39; RAVI2&#39;但是&#39; RAVI3&#39; - 将/ process [1] / input2 [1] / text()[1]中的RAVI2与/ process [1] / input2 [1] / text()[1](不同)中的RAVI3进行比较 / process [1] / input2 [1] / text()[1](SOURCE) - &gt; RAVI2 / process [1] / input2 [1] / text()[1](TARGET) - &gt; RAVI3

这里Xpath不包含名称空间 /过程[1] / INPUT1 [1] /文本()[1] 我认为这就是,

/测试:过程[1] /测试:INPUT1 [1] /文本()

在我的代码中,我尝试使用此Xpath,但它没有给出任何结果。如果XML不包含名称空间,那么这样可以正常工作。

XPathEngine engine = new JAXPXPathEngine();

String value = engine.evaluate(d1.getComparison()。getTestDetails()。getXPath(),Input.fromString(targetXml).build()); System.out.println(&#34; ------------------&#34; + value);

1 个答案:

答案 0 :(得分:0)

我在任何一个示例中都没有看到RAVI3

无论如何,如果你想要带有名称空间前缀的XPath,你必须指定一个包含所需映射的NamespaceContext(从前缀到uri的Map)。请参阅Javadocs of DiffBuilder