Menu

Issue while using RecursiveElementNameAndTextQualifier

Help
2017-12-12
2017-12-13
  • Bharath Kumar

    Bharath Kumar - 2017-12-12

    I'm facing an issue while usign RecursiveElementNameAndTextQualifier as element qualifier.

    Code:

    Diff xmlDiff = new Diff(source, target);
    xmlDiff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());

    XML1 :
    <catalog>
    <product>
    <catalog_item>
    <item_number>QWZ5671</item_number>
    </catalog_item>
    <name>bharath</name>
    </product>
    </catalog>

    XML 2:

    <catalog>
    <product>
    <catalog_item>
    <item_number>QWZ5671</item_number>
    </catalog_item>
    <catalog_item>
    <item_number>1QWZ5671</item_number>
    </catalog_item>
    <name>bharath1</name>
    </product>
    </catalog>

    But I get following issue counts :
    Expected number of child nodes '2' but was '3' - comparing <product...> at /catalog[1]/product[1] to <product...> at /catalog[1]/product[1]
    Expected element tag name 'name' but was 'catalog_item' - comparing <name...> at /catalog[1]/product[1]/name[1] to <catalog_item...> at /catalog[1]/product[1]/catalog_item[2]
    Expected node type '3' but was '1' - comparing <name ...="">bharath</name> at /catalog[1]/product[1]/name[1]/text()[1] to <item_number...> at /catalog[1]/product[1]/catalog_item[2]/item_number[1]
    Expected presence of child nodes to be 'false' but was 'true' - comparing <name ...="">bharath</name> at /catalog[1]/product[1]/name[1]/text()[1] to <item_number...> at /catalog[1]/product[1]/catalog_item[2]/item_number[1]
    Expected number of child nodes '0' but was '1' - comparing <name ...="">bharath</name> at /catalog[1]/product[1]/name[1]/text()[1] to <item_number...> at /catalog[1]/product[1]/catalog_item[2]/item_number[1]
    Expected presence of child node 'null' but was '#text' - comparing at null to <item_number ...="">1QWZ5671</item_number> at /catalog[1]/product[1]/catalog_item[2]/item_number[1]/text()[1]
    Expected presence of child node 'null' but was 'name' - comparing at null to <name...> at /catalog[1]/product[1]/name[1]

    Expected
    1. New catalog node added
    2. Mismath in name node.

     

    Last edit: Bharath Kumar 2017-12-12
  • Bharath Kumar

    Bharath Kumar - 2017-12-13

    Thanks Stefan Bodewig for your response.

    I have done with XMLUnit 2.X also. But no Luck!

    My Sample 2.x code

    Diff myDiff = DiffBuilder.compare(control).withTest(test)
    .normalizeWhitespace()
    .ignoreComments()
    .withNodeMatcher(
    new DefaultNodeMatcher( new ByNameAndTextRecSelector()))
    .checkForSimilar()
    .build();

    Reult :
    Expected child 'catalog' but was 'null' - comparing <catalog...> at /catalog[1] to <NULL> (DIFFERENT)
    Expected child 'null' but was 'catalog' - comparing <NULL> to <catalog...> at /catalog[1] (DIFFERENT)

    Waiting for your response.

    Expetation:
    Comparing two large XML with different element order in various hirarchy
    1.Verifying element values.
    2.Identify newly added or removed elements.

     

    Last edit: Bharath Kumar 2017-12-13
  • Stefan Bodewig

    Stefan Bodewig - 2017-12-13

    I really wish I had never added RecursiveElementNameAndTextQualifier it is probably the most ab-/overused feature of XMLUnit 1.x. You don't need a recursive matcher at all, really.

    Judging from your example XML you want an ElementSelector (or ElementQualifier in 1.x) that "normally" picks elements based on their names unless they happen to be the element named "catalog_item". For "catalog_item" you want to select the one where the nested texts of the child element named "item_number" match.

    In 2.x this is

    ElementSelectors.conditionalBuilder()
        .whenElementIsNamed("catalog_item")
        .thenUse(ElementSelectors.byXPath("./item_number", ElementSelectors.byNameAndText))
        .elseUse(ElementSelectors.byName)
        .build()
    

    Please see https://github.com/xmlunit/user-guide/wiki/SelectingNodes for more details.

     
  • Bharath Kumar

    Bharath Kumar - 2017-12-13

    Thanks Stefan Bodewig for your quick response once again.
    Great Effort!!

    I was searching for an generic solution

    My Expetation :
    Comparing two large XML with different element order in various hirarchy
    1.Verifying element values.
    2.Identify newly added or removed elements.

    Is it feasible using the XMLUnit?

     
  • Stefan Bodewig

    Stefan Bodewig - 2017-12-13

    I don't believe a "generic" approach really is feasible. Well, one could take a brute-force approach and try all possible permutations of matching sibling elements until the comparison ends up with an EQUAL result. Some kind of "do what I mean" ElementSelector. This doesn't exist, but there is https://github.com/xmlunit/xmlunit/issues/45 - maybe anybody will find the time to code this up one day.

    XMLUnit cannot really guess what makes identifies the sibbling elements it really should match with each other. Blindly searching for a nested text somehwre as RecursiveElementNameAndTextQualifier does works remarkably well for many real world problems - until it no longer does and you wonder why it has ever worked. Matching on attributes is also very common.

    The conditional builder is there because things are really difficult to do. Only the people who define the XML really know what identifies the elements to match and they will have to tell XMLUnit.

    BTW there is something like RecursiveElementNameAndTextQualifier in 2.x as well, it is ByNameAndTextRecSelector.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.