Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using XPath to compare two XML objects for exact equality

Tags:

xml

xpath

When I have two XML objects, how can I compare them for exact equality (all the same nodes and attributes and values) using XPath?

like image 242
mmzc Avatar asked Feb 02 '23 14:02

mmzc


2 Answers

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.

like image 145
Dimitre Novatchev Avatar answered Feb 05 '23 03:02

Dimitre Novatchev


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.

like image 23
Martin Honnen Avatar answered Feb 05 '23 05:02

Martin Honnen