【问题标题】:XMLUNIT 2 using comparison with ignore element order with diffbuilder and namespaces failsXMLUNIT 2 使用与 diffbuilder 和命名空间的忽略元素顺序进行比较失败
【发布时间】:2020-04-06 23:59:58
【问题描述】:

在比较两个 .xml 文件时,我尝试使用 DiffBuilder 忽略 XML 元素顺序,但它失败了。在发布这个问题之前,我已经尝试了所有可能的组合并阅读了很多文章。

例如:

<Data:Keys>
 <Data:Value Key="1" Name="Example1" />
 <Data:Value Key="2" Name="Example2" />
 <Data:Value Key="3" Name="Example3" />
</Data:Keys>

<Data:Keys>
 <Data:Value Key="2" Name="Example2" />
 <Data:Value Key="1" Name="Example1" />
 <Data:Value Key="3" Name="Example3" />
</Data:Keys>

我希望这两个被视为相同的 XML。请注意,元素是空的,它们只有属性。 到目前为止我做了什么:

def diff = DiffBuilder.compare(Input.fromString(xmlIN))
           .withTest(Input.fromString(xmlOUT))
           .ignoreComments()
           .ignoreWhitespace()
           .checkForSimilar()
           .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder()
           .whenElementIsNamed("Data:Keys").thenUse(ElementSelectors.byXPath("./Data:Value", 
            ElementSelectors.byNameAndText))
           .elseUse(ElementSelectors.byName)
           .build()))

但每次都失败。我不知道问题是命名空间,还是元素为空。

我们将提供任何帮助。提前谢谢你。

【问题讨论】:

    标签: xmlunit


    【解决方案1】:

    如果您的目标是将标签Data:Value 通过它们的属性匹配在一起,您应该从这个开始:

    .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder()
               .whenElementIsNamed("Data:Value") 
    

    由于该标签没有任何文本,byNameAndText 将不起作用。您只能处理名称和属性。我的建议是这样做:

    .thenUse(ElementSelectors.byNameAndAttributes("Key"))
    

    .thenUse(ElementSelectors.byNameAndAllAttributes()) 
    //equivalent
    .thenUse(ElementSelectors.byNameAndAttributes("Key", "Name"))
    

    关于命名空间的问题,checkForSimilar() 应该输出SIMILAR,这意味着它们不是DIFFERENT,所以这就是你需要的。如果您不使用checkForSimilar(),则命名空间的差异将输出为DIFFERENT

    【讨论】:

      猜你喜欢
      • 2018-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多