如何合并具有相同属性的XML元素?

时间:2016-09-02 13:38:24

标签: php jquery ajax xml

我想知道是否能够在example.php文件中合并以下XML数据:

<?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>

结果将是这样的:

<query text="foo bar">
 <doc id="1"/>
 <doc id="2"/>
 <doc id="3"/>
 <doc id="4"/>
 <doc id="9"/>
</query>

2 个答案:

答案 0 :(得分:0)

您可以循环遍历元素并创建“新”xml。像这样的东西?

$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;

哪个输出

<?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>

答案 1 :(得分:0)

我确实做到了这一点,感谢詹姆斯·艾略特以更加艰难的方式,但是为我工作。 xml文件的输入来自HTML表单,所以我添加了这段代码,每次附加一个

  

“query == foo bar”

并且已经存在,我取消了附加的一个,但首先我将孩子们复制到已经存在的foo栏。现在我必须找到一种方法来删除查询中的重复属性。

//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' );