【问题标题】:How to merge XML elements that have the same attribute?如何合并具有相同属性的 XML 元素?
【发布时间】:2016-09-02 13:38:24
【问题描述】:

我想知道是否能够在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>

【问题讨论】:

    标签: php jquery ajax xml


    【解决方案1】:

    您可以遍历元素并创建一个“新”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>
    

    【讨论】:

    • 感谢您的帮助,这是我的第一篇文章。我应该只使用两个“query=foo bar”,因为不可能追加一个以上。表单的输入构建这些 xml 对象。
    【解决方案2】:

    好吧,我确实设法做到了,感谢 James Elliott,以一种更难的方式,但正在为我工​​作。 xml 文件的输入来自 HTML 表单,所以我添加了这段代码,每次你附加一个

    “查询 == 富吧”

    并且已经存在,我取消设置附加的但首先我将孩子复制到已经存在的 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' );
    

    【讨论】:

      猜你喜欢
      • 2020-12-05
      • 2020-05-14
      • 1970-01-01
      • 2019-04-23
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多