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>
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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">.
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)?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
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 atchildwithid2 and not 1? Without anyElementSelectorXMLUnit 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.byNameAndAllAttributesand it does result in the parent XPaths that I would expect.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)?
Ah, this is an interesting twist I didn't think about - I didn't really get you've been talking about using a
NodeFilter.I've created https://github.com/xmlunit/xmlunit/issues/156 and tweaked the test I added with https://github.com/xmlunit/xmlunit/commit/3cf072e13fd288da3ef5a1a83b86d5be1c05a612 - this will be fixed with the next release, probably within the next two weeks.
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.
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.
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.