Hello,
I am using XMLUnit 2.x for comparing two XMLs. I have a requirement to compare two XMLs (Control.xml and Test.xml) and if any of the nodes in the Control.xml has an attribute with the name 'lastupdatedby', then I should ignore any type of differences generated on that node (which contains 'lastupdatedby' attribute). Please let me know how can I implement this.
I tried NodeFilter but it is of no luck since the Test.xml will NOT have any nodes with 'lastupdatedby' attribute. The 'lastupdatedby' attribute will ONLY be present in the Control.xml.
These are two sample XMLs. Based on my requirement, I only need the differences on the second <book> item (except the <author> since it also has 'lastupdatedby' attribute) and <library> item in the <books> list.</books></library></author></book>
Please help me identify a solution for my requirement.
Once I am able to ignore any differences on the nodes with ''lastupdatedby" attribute, I have written custom code to loop through the differences and merge the Control.xml and Test.xml into Merge.xml.
My end goal is to merge Control.xml and Test.xml into Merge.xml and the requirement is to replace the values (even appending childs from Test.xml) from Test.xml into Control.xml and create a new file called 'Merge.xml'. The only exception for this merge is that, I should NOT overwrite any nodes in the Control.xml which has 'lastupdatedby' attribute. If there is any other API in XMLUnit to merge two XMLs, please point me to that as well.
Thanks in Advance for your help!
Thanks,
Vairavan.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
One approach may be to implement a DifferenceEvaluator and for every real difference walk up the tree starting from the Comparison's control node checking whether any node contains the attribute and return "EQUAL" in this case.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks Stefan,
Can you please help me with this particular DifferenceEvaluator implementation? I am new to this and any help on this would be of great help to me.
Thanks,
Vairavan
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
which contains more differences than you'd like to see (the added Type element and the text difference in category). They happen because there is no "control node" one could traverse up.
You could cache the XPath of nodes that have been considered ignorable and ignore all comparisons with an XPath that starts with such an ignorable path. I think you can go from here.
Last edit: Stefan Bodewig 2018-01-03
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
I am using XMLUnit 2.x for comparing two XMLs. I have a requirement to compare two XMLs (Control.xml and Test.xml) and if any of the nodes in the Control.xml has an attribute with the name 'lastupdatedby', then I should ignore any type of differences generated on that node (which contains 'lastupdatedby' attribute). Please let me know how can I implement this.
I tried NodeFilter but it is of no luck since the Test.xml will NOT have any nodes with 'lastupdatedby' attribute. The 'lastupdatedby' attribute will ONLY be present in the Control.xml.
Control.xml:
Test.xml:
These are two sample XMLs. Based on my requirement, I only need the differences on the second <book> item (except the <author> since it also has 'lastupdatedby' attribute) and <library> item in the <books> list.</books></library></author></book>
Please help me identify a solution for my requirement.
Once I am able to ignore any differences on the nodes with ''lastupdatedby" attribute, I have written custom code to loop through the differences and merge the Control.xml and Test.xml into Merge.xml.
My end goal is to merge Control.xml and Test.xml into Merge.xml and the requirement is to replace the values (even appending childs from Test.xml) from Test.xml into Control.xml and create a new file called 'Merge.xml'. The only exception for this merge is that, I should NOT overwrite any nodes in the Control.xml which has 'lastupdatedby' attribute. If there is any other API in XMLUnit to merge two XMLs, please point me to that as well.
Thanks in Advance for your help!
Thanks,
Vairavan.
This is a tough requirement.
One approach may be to implement a
DifferenceEvaluatorand for every real difference walk up the tree starting from theComparison's control node checking whether any node contains the attribute and return "EQUAL" in this case.Thanks Stefan,
Can you please help me with this particular DifferenceEvaluator implementation? I am new to this and any help on this would be of great help to me.
Thanks,
Vairavan
Actually this is probably more complex than I though.
leads to
which contains more differences than you'd like to see (the added Type element and the text difference in category). They happen because there is no "control node" one could traverse up.
You could cache the XPath of nodes that have been considered ignorable and ignore all comparisons with an XPath that starts with such an ignorable path. I think you can go from here.
Last edit: Stefan Bodewig 2018-01-03