1

我有以下两个 xml 文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<pricebooks xmlns="http://www.blablabla.com">
    <pricebook>
        <header pricebook-id="my-id">
            <currency>GBP</currency>
            <display-name xml:lang="x-default">display name</display-name>
            <description>my description 1</description>
        </header>
        <price-tables>
            <price-table product-id="id1" mode="mode1">
                <amount quantity="1">30.00</amount>
            </price-table>
            <price-table product-id="id2" mode="mode2">
                <amount quantity="1">60.00</amount>
            </price-table>
        </price-tables>
    </pricebook>
</pricebooks>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<pricebooks xmlns="http://www.blablabla.com">
    <pricebook>
        <header pricebook-id="my-id">
            <currency>GBP</currency>
            <display-name xml:lang="x-default">display name</display-name>
            <description>my description 1</description>
        </header>
        <price-tables>
            <price-table product-id="id2" mode="mode2">
                <amount quantity="1">60.00</amount>
            </price-table>
            <price-table product-id="id1" mode="mode1">
                <amount quantity="1">30.00</amount>
            </price-table>
        </price-tables>
    </pricebook>
</pricebooks>

我试图比较忽略元素的顺序price-table,所以对我来说这两个是相等的。我正在使用

        <dependency>
            <groupId>org.xmlunit</groupId>
            <artifactId>xmlunit-core</artifactId>
            <version>2.5.0</version>
        </dependency>

并且代码如下,但我无法使其工作。它抱怨是因为属性值id1id2不同。

Diff myDiffSimilar = DiffBuilder
    .compare(expected)
    .withTest(actual)
    .checkForSimilar()
    .ignoreWhitespace()
    .ignoreComments()
    .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
    .build();
assertFalse(myDiffSimilar.hasDifferences());

我还尝试按如下方式编辑 nodeMatcher:

.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder()
    .whenElementIsNamed("price-tables")
    .thenUse(ElementSelectors.byXPath("./price-table", ElementSelectors.byNameAndText))
    .elseUse(ElementSelectors.byName)
    .build()))

感谢您的帮助。

4

1 回答 1

1

price-table我在您的元素中根本看不到任何嵌套文本,因此byNamAndText仅匹配元素名称 - 这对于所有price-tables 都是相同的,因此不会执行您想要的操作。

在您的示例中,没有歧义,price-tables因为无论如何只有一个。所以这种byXPath方法看起来是错误的。至少在您的代码片段中,byName除了price-table元素之外,XMLUnit 应该可以很好地处理。

我不确定是product-id单独标识您的price-table元素还是所有属性的组合。要么byNameAndAttributes("product-id")或它的byNameAndAllAttributes表亲应该工作。

如果它只适用 product-id于根本没有任何属性的byNameAndAttributes("product-id")所有元素。如我们所见,仅在这种特殊情况下,它就可以用于您的整个文档-或多或少是偶然的。byNameproduct-idbyNameAndAttribute("product-id")

如果您需要比其他元素更复杂的规则,price-table或者您想让事情比

ElementSelectors.conditionalBuilder()
    .whenElementIsNamed("price-table")
    .thenUse(ElementSelectors.byNameAndAttributes("product-id"))
    // more special cases
    .elseUse(ElementSelectors.byName)

是更好的选择。

于 2019-09-07T09:10:44.080 回答