rick645 Posted January 25 Share Posted January 25 (edited) Here's my scenario + ls -1 main.php x1.xml x2.xml + cat x1.xml <aaa xxx1="aaa"> <bbb> blabla1 </bbb> </aaa> + cat x2.xml <aaa xxx2="bbb"> </aaa> + cat main.php <?php $x1Doc = new DOMDocument(); $x1Doc->load('x1.xml'); $x2Doc = new DOMDocument(); $x2Doc->load('x2.xml'); $dom = new XMLDiff\DOM; $diff = $dom->diff($x1Doc, $x2Doc); echo $dom->merge($x1Doc, $diff)->saveXML(); Let's do some tests + php main.php <?xml version="1.0"?> <aaa xxx2="bbb"> </aaa> I was expecting something like this though <aaa xxx1="aaa" xxx2="bbb"> <bbb> blabla1 </bbb> </aaa> Any suggestions? Edited January 25 by rick645 Quote Link to comment https://forums.phpfreaks.com/topic/326644-xml-files-merging/ Share on other sites More sharing options...
requinix Posted January 25 Share Posted January 25 Merging XML cannot happen in a generic way that will support everything. If you know the structure of the documents then the best thing will be to write code to merge the two together. The big question for that code is going to be how XMLDiff works. Have you seen what $diff looks like? Does that hold any clues as to what's happening when you try to "merge" it into the first document? Quote Link to comment https://forums.phpfreaks.com/topic/326644-xml-files-merging/#findComment-1648520 Share on other sites More sharing options...
rick645 Posted January 27 Author Share Posted January 27 On 1/25/2025 at 8:47 PM, requinix said: Merging XML cannot happen in a generic way that will support everything. If you know the structure of the documents then the best thing will be to write code to merge the two together. I agree Quote The big question for that code is going to be how XMLDiff works. XMLDiff is a class, and it works as a class should work IHMO Quote Have you seen what $diff looks like? <?xml version="1.0"?> <dm:diff xmlns:dm="http://www.locus.cz/diffmark"> <dm:delete> <aaa xxx1="aaa"/> </dm:delete> <dm:insert> <aaa xxx2="bbb"> </aaa> </dm:insert> </dm:diff> Quote Does that hold any clues as to what's happening when you try to "merge" it into the first document? Of course, I would say yes Quote Link to comment https://forums.phpfreaks.com/topic/326644-xml-files-merging/#findComment-1648598 Share on other sites More sharing options...
requinix Posted January 27 Share Posted January 27 I like how you said "it works as it should", then you post the output of the diff which clearly shows what it's doing. So no problem, right? It's all working as it should, which means your expectations are incorrect. 👍 Quote Link to comment https://forums.phpfreaks.com/topic/326644-xml-files-merging/#findComment-1648611 Share on other sites More sharing options...
requinix Posted January 27 Share Posted January 27 Less sarcastic answer is, Apparently the diff says that the two <aaaa> nodes are different, likely due to the attributes, so they can't be merged. What happens if they have the same attributes? Or is it that the diff somehow isn't being recursive? Quote Link to comment https://forums.phpfreaks.com/topic/326644-xml-files-merging/#findComment-1648612 Share on other sites More sharing options...
gizmola Posted January 28 Share Posted January 28 You are starting with File1 and you diff it using FIle2. What do you get? You get a Diff file telling you how to change File1 into FIle2. The use case for diff-ing was as a tool for "authors" (in the olden times when diffs were commonly used to distribute updates to text based files) to provide patch files to "end users" who would use a patching tool to transform their files into the "patched" state. In the modern era, there are just better more reliable and resilient ways of distributing updates. Clearly, in this case the xml diff file shows exactly what would be expected when you give it a diff that is based on a file that has nothing but a new element. It deletes the original element, adds the new one, and leaves you with ..... file2. It's hard to say whether or not this is worth doing, even if it works, given this is sample data, however, you might try reversing the order of the parameters -- create a diff where you: $diff = $dom->diff($x2Doc, $x1Doc); I am not sure how it will resolve the 2 different attributes, but at least in this case, I suspect that it might generate the ultimate result you expect. Quote Link to comment https://forums.phpfreaks.com/topic/326644-xml-files-merging/#findComment-1648655 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.