Menu

On using NodeFilter in difference builder, the XPath of the nodes are getting altered

MonishaM
2019-05-15
2019-06-27
  • MonishaM

    MonishaM - 2019-05-15

    CASE 1:
    Control:
    <grandparent>
    <parent id="user-content-0">
    </parent>
    <parent id="user-content-1">
    <child id="user-content-1"></child>
    <child id="user-content-2"></child>
    </parent>
    </grandparent>

    Test:
    <grandparent>
    <parent id="user-content-1">
    <child id="user-content-1"></child>
    </parent>
    </grandparent>

    In the above example, assume <parent id="user-content-0"> tag has been ignored. And we need to update the control xml to match the test xml. So, in our case we will have to remove the tag - <child id="user-content-2"></child>.</parent>

    The diff builder gives -
    control node:<child id="user-content-2"></child>
    and test node:null
    We get the parent of the node <child id="user-content-2"></child> and remove this node.

    On the contrary,
    CASE 2:
    Control:
    <grandparent>
    <parent id="user-content-0">
    </parent>
    <parent id="user-content-1">
    <child id="user-content-1"></child>
    </parent>
    </grandparent>

    Test:
    <grandparent>
    <parent id="user-content-1">
    <child id="user-content-1"></child>
    <child id="user-content-2"></child>
    </parent>
    </grandparent>

    In this case,
    control node: null
    and test node: <child id="user-content-2"></child>
    and we need to add node <child id="user-content-2"></child> to the control side.
    But, because the control node is null, the getParentXPath() returns a different XPath from what is expected in the original document(without excluding the filtered Node - <parent id="user-content-0"></parent>).

    We would like to know, what would be the best way to identify the Parent node - <parent id="user-content-1"> without using the XPath?</parent>

     

    Last edit: MonishaM 2019-05-15
  • Stefan Bodewig

    Stefan Bodewig - 2019-06-04

    I'm not entirely sure I understand the issue you are describing.

    I think you expect the parent XPath to be /grandparent[1]/parent[2] while you get /grandparent[1]/parent[1]. Have you checked you are actually looking at child with id 2 and not 1? Without any ElementSelector XMLUnit is going to match siblings in order. This means you are comparing control <parent id="0"> to test <parent id="1">.

    With https://github.com/xmlunit/xmlunit/commit/b0519685c0bbab843f63ddcba88861b16037e32b I've added a unit test that uses ElementSelectors.byNameAndAllAttributes and it does result in the parent XPaths that I would expect.

     
  • MonishaM

    MonishaM - 2019-06-04

    The problem we are facing is that, after we filter out the node Parent with id = 0, when we compute the XPath of the nodes from the Diff object, we get XPath values that are different from the original XPath values (as per the unfiltered document object).

    The filtering Nodes option in XMLUnit is removing all the filtered nodes from the document object before starting to evaluate. So, when we access the test and control nodes in the diff objects from the difference iterator - the test and control nodes' XPath values are different from its actual XPaths.

    Our question is - Is this change in the XPath value expected? If so, if we would want to access the node to make any modification, how do we do that (considering that we now don't have the correct xpath)?

     
  • Stefan Bodewig

    Stefan Bodewig - 2019-06-20

    Do you think you could give the 2.6.3-SNAPSHOT version a try that I've published to XMUnit's snapshot repository? This should result in the XPaths you expect.

     
  • MonishaM

    MonishaM - 2019-06-21

    Hi Stefan,
    We tested using 2.6.3 and it worked fine. This time the XPaths were preserved despite using NodeFilters in the Difference Evaluator.
    Thank you very much for such an expedited response and fix! This has been very helpful.

     
  • MonishaM

    MonishaM - 2019-06-27

    Hi Stefan,
    We had another question for you. Currently, we can only access the XPath of the ParentNode of a given target node (computed from the Comparison.Detail object). Is there a way to get a reference to the ParentNode directly from the Comparison.Detail object? Or atleast in a future version of XMLUnit?
    Have opened a new Discussion Topic for this here.
    Please advise.

     

Log in to post a comment.

MongoDB Logo MongoDB