I was wondering if is able to merge the following XML data on example.php
file:
<?xml version="1.0" encoding="UTF-8"?>
<elevate>
<query text="foo bar">
<doc id="1"/>
<doc id="2"/>
<doc id="3"/>
</query>
<query text="foo bar">
<doc id="4"/>
<doc id="9"/>
<doc id="3"/>
</query>
<elevate>
The result would be something like this:
<query text="foo bar">
<doc id="1"/>
<doc id="2"/>
<doc id="3"/>
<doc id="4"/>
<doc id="9"/>
</query>
You could loop through the elements and create a "new" xml. Something like this?
$xml = <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<elevate>
<query text="foo bar">
<doc id="1"/>
<doc id="2"/>
<doc id="3"/>
</query>
<query text="foo bar">
<doc id="4"/>
<doc id="9"/>
<doc id="3"/>
</query>
<query text="foo bar">
<doc id="4"/>
<doc id="8"/>
<doc id="3"/>
</query>
</elevate>
EOF;
$parsed = simplexml_load_string($xml);
$docs = [];
foreach ($parsed->query as $query) {
foreach ($query->doc as $doc) {
foreach ($doc->attributes() as $attribute) {
// you should do some checking here, like in_array to make sure there is no duplicates.
// we use __toString so it puts it as a string, not an XmlElement object
$docs[] = $attribute[0]->__toString();
}
}
}
$docsXml = '';
foreach ($docs as $doc) {
$docsXml .= '<doc id="' . $doc .'" />' . PHP_EOL;
}
$newXml = <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<elevate>
<query text="foo bar">
$docsXml
</query>
</elevate>
EOF;
echo $newXml;
Which outputs
<?xml version="1.0" encoding="UTF-8"?>
<elevate>
<query text="foo bar">
<doc id="1" />
<doc id="2" />
<doc id="3" />
<doc id="4" />
<doc id="9" />
<doc id="3" />
<doc id="4" />
<doc id="8" />
<doc id="3" />
</query>
</elevate>
Well i did manage to do it thanks James Elliott, in a harder way, but is working for me. The inputs of the xml file are coming from a HTML form so i added this code, that every time you append a
"query == foo bar"
and is already exists, i unset the appended one but first I copy the children to the already existing foo bar. Now i have to find a way to remove duplicates attributes inside query.
//load xml file to xml1
$xml1 = new DomDocument("1.0","UTF-8");
$xml1 = simplexml_load_file ( '../elevate.xml' );
$deleItem=0;
//finds if you try to append an already existing search qquery
$counterDel=0;
//checks if you append an already existing elevated document and append it to the existing one
foreach ($xml1 -> query as $item1)
{
# transform it to string because otherwise it would search for only the identical object
$var1 = $item1['text']->__toString();
foreach ($xml1 -> query as $item2) {
$var2 = $item2['text']->__toString();;
# check if it has other objects with the same name but exclude itslef
if ( $var1 == $var2 && $item1!=$item2 )
{
$counterDel++;
foreach ($item2-> doc as $key => $added)
{
$doc = $item1->addChild('doc');
$doc -> addAttribute('id',$added['id']);
if ($added["exclude"])
$doc -> addAttribute('exclude',$added['exclude']);
}
//if the flag is 1 then delete the second eleveted document because it would duplicate it self
if ($counterDel == 1)
$deleItem=$item2;
}
}
}
unset($deleItem[0][0]);
//saves new xml
$xml1->saveXML( '../elevate.xml' );