XMLunit仅比较标签。

huangapple 未分类评论44阅读模式
英文:

XMLunit compare only tags

问题

我想要比较两个XML文件,但仅基于标签的值进行比较,还需要将标签作为整体进行比较,例如:

<someTag><otherTag value1="0" value2="5"/>text1</someTag>

<someTag><otherTag value1="10" value2="15"/>text2</someTag>

我需要实现的目标是显示<otherTag value1="10" value2="15"/>不同,我不关心text1text2的差异。问题在于,默认情况下,XMLUnit会将value1value2分别进行比较,而我希望将它们作为一个字符串进行比较。有没有办法实现这一点?我知道可以通过实现DifferenceEvaluator来排除text,但仍然不知道如何将属性作为整体进行比较。我还考虑使用XSLT。

英文:

I would like to compare two XML files, but based only on tag values, also I need to compare tags as a whole, e.g.

&lt;someTag&gt;&lt;otherTag value1=&quot;0&quot; value2=&quot;5&quot;/&gt;text1&lt;/someTag&gt;

&lt;someTag&gt;&lt;otherTag value1=&quot;10&quot; value2=&quot;15&quot;/&gt;text2&lt;/someTag&gt;

what I need to achieve is to show that &lt;otherTag value1=&quot;10&quot; value2=&quot;15&quot;/&gt; is different, I don't care about text1 text2 difference. Problem is, that by default XMLUnit will compare value1 and value2 separetly, while I want to compare them together, whole tag as one string. Is there a way to achieve this? I know I can exclude text implementing DifferenceEvaluator but still, I don't know how to compare Attributes as whole. I also consider to use xslt.

答案1

得分: 0

我会将这两份文件进行转换,排除您希望从比较中排除的部分,然后对比结果。

英文:

I would transform both documents to get rid of the things you want to exclude from the comparison, and then compare the results.

答案2

得分: 0

我认为你可以将DifferenceEvaluators链接在一起来实现这一点。

例如,在我必须合并两个节点的比较时,我做过类似的事情。在你的情况下,可能需要进行一些定制。

Diff myDiff = DiffBuilder.compare(FileReadingUtil.readFileAsByteArray(source,this.getClass().getClassLoader()))
                .withTest(FileReadingUtil.readFileAsByteArray(test,this.getClass().getClassLoader()))
                .ignoreComments()
                .ignoreWhitespace()
                .withDifferenceEvaluator(
                        DifferenceEvaluators.chain(
                                DifferenceEvaluators.Default,
                                new IgnoreAttributeDifferenceEvaluator("MsgId"),
                                new IgnoreAttributeDifferenceEvaluator("PmtId")
                        )
                )
                .checkForSimilar()
                .build();
英文:

I think you can chain the DifferenceEvaluators together to achieve this.

For example, this is something I did when I had to combine comparison for two nodes. Might work in your case with some customisations.

Diff myDiff = DiffBuilder.compare(FileReadingUtil.readFileAsByteArray(source,this.getClass().getClassLoader()))
                .withTest(FileReadingUtil.readFileAsByteArray(test,this.getClass().getClassLoader()))
                .ignoreComments()
                .ignoreWhitespace()
                .withDifferenceEvaluator(
                        DifferenceEvaluators.chain(
                                DifferenceEvaluators.Default,
                                new IgnoreAttributeDifferenceEvaluator(&quot;MsgId&quot;),
                                new IgnoreAttributeDifferenceEvaluator(&quot;PmtId&quot;)
                        )
                )
                .checkForSimilar()
                .build();

huangapple
  • 本文由 发表于 2020年4月9日 15:24:27
  • 转载请务必保留本文链接:https://java.coder-hub.com/61115955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定