When I have two XML objects, how can I compare them for exact equality (all the same nodes and attributes and values) using XPath?
In XPath 2.0 use the standard function deep-equal().
Xpath 1.0 doesn't have such a function, therefore the comparison needs to be performed within the language hosting XPath.
You can use this solution in case you must use XPath 1.0: Generate/get xpath from XML node java to get a collection of XPath expressions for every node of Document1
and another collection of XPath expressions for every node of Document2
. Then compare the two collections -- they should have the same number of expressions each and the expressions must be equivalent.
Alternatively, you can generate just verify that the two collections contain the same number of expressions and apply each of the expressions for Document1
on Document2
.
XPath 2.0 has a function deep-equal for that: http://www.w3.org/TR/xpath-functions/#func-deep-equal. XPath 1.0 has nothing comparable, you would need to roll your own, in whatever host language you use XPath 1.0 with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With