Jump to content

Recommended Posts

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 by rick645
Link to comment
https://forums.phpfreaks.com/topic/326644-xml-files-merging/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/326644-xml-files-merging/#findComment-1648520
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/326644-xml-files-merging/#findComment-1648598
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/326644-xml-files-merging/#findComment-1648612
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/326644-xml-files-merging/#findComment-1648655
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.