• <tfoot id='4dL3V'></tfoot>

    1. <small id='4dL3V'></small><noframes id='4dL3V'>

        <bdo id='4dL3V'></bdo><ul id='4dL3V'></ul>

      <legend id='4dL3V'><style id='4dL3V'><dir id='4dL3V'><q id='4dL3V'></q></dir></style></legend>
      <i id='4dL3V'><tr id='4dL3V'><dt id='4dL3V'><q id='4dL3V'><span id='4dL3V'><b id='4dL3V'><form id='4dL3V'><ins id='4dL3V'></ins><ul id='4dL3V'></ul><sub id='4dL3V'></sub></form><legend id='4dL3V'></legend><bdo id='4dL3V'><pre id='4dL3V'><center id='4dL3V'></center></pre></bdo></b><th id='4dL3V'></th></span></q></dt></tr></i><div id='4dL3V'><tfoot id='4dL3V'></tfoot><dl id='4dL3V'><fieldset id='4dL3V'></fieldset></dl></div>

        根据共同属性合并两个xml文件

        时间:2023-11-30
          • <bdo id='1iWRp'></bdo><ul id='1iWRp'></ul>

              <tbody id='1iWRp'></tbody>
            1. <i id='1iWRp'><tr id='1iWRp'><dt id='1iWRp'><q id='1iWRp'><span id='1iWRp'><b id='1iWRp'><form id='1iWRp'><ins id='1iWRp'></ins><ul id='1iWRp'></ul><sub id='1iWRp'></sub></form><legend id='1iWRp'></legend><bdo id='1iWRp'><pre id='1iWRp'><center id='1iWRp'></center></pre></bdo></b><th id='1iWRp'></th></span></q></dt></tr></i><div id='1iWRp'><tfoot id='1iWRp'></tfoot><dl id='1iWRp'><fieldset id='1iWRp'></fieldset></dl></div>

                1. <legend id='1iWRp'><style id='1iWRp'><dir id='1iWRp'><q id='1iWRp'></q></dir></style></legend>
                  <tfoot id='1iWRp'></tfoot>

                  <small id='1iWRp'></small><noframes id='1iWRp'>

                2. 本文介绍了根据共同属性合并两个xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有两个结构相似的xml文件,我需要基于一个共同的属性来合并它们.更明确地说,这里有两个示例:

                  I have two xml files with a similar structure, and I need to merge them based on a common attribute. To be more explicit, here are two samples:

                  file1.xml

                   <Products>
                   <record ProductId="366" ProductName="Test" ProductCategory="Categ1"></record>
                   </Products>
                  

                  file2.xml

                   <Productprices>
                   <record ProductId="366" ProductPrice="10" ProductVAT="24"></record>
                   </Productprices>
                  

                  这两个文件的共同属性是 ProductId.我需要合并所有属性,以便合并后的文件如下所示:

                  The common attribute in both files is ProductId. I need to merge all the attributes so that the combined file would look like this:

                   <Products>
                   <record ProductId="366" ProductName="Test" ProductCategory="Categ1" ProductPrice="10" ProductVAT="24"></record>
                   </Products>
                  

                  不幸的是,到目前为止我所做的只是合并两个文件,合并后的文件如下所示:

                  Unfortunately, all I have managed to do so far is simply merge the two files, the merged file looks like this:

                   <Products>
                   <record ProductId="366" ProductName="Test" ProductCategory="Categ1"></record>
                   <record ProductId="366" ProductPrice="10" ProductVAT="24"></record>
                   </Products>
                  

                  这是我使用的 PHP 代码:

                  This is the PHP code I used:

                   $doc1 = new DOMDocument();
                   $doc1->load('file1.xml');
                   $doc2 = new DOMDocument();
                   $doc2->load('file2.xml');
                   $res1 = $doc1->getElementsByTagName('Products')->item(0);
                   $items2 = $doc2->getElementsByTagName('record');
                   for ($i = 0; $i < $items2->length; $i ++) {
                   $item2 = $items2->item($i);
                   $item1 = $doc1->importNode($item2, true);
                   $res1->appendChild($item1);
                   }
                   $doc1->save('file1.xml');
                  

                  有没有什么方法可以使用 DomDocument 将所有属性合并到一个基于通用 ProductId 的记录中?我宁愿不进入 XSLT.

                  Is there any way I can merge all the attributes into one record based on the common ProductId by using DomDocument? I would rather not go into XSLT.

                  任何帮助将不胜感激.

                  提前致谢.

                  推荐答案

                  我使用 Xpath 从 DOM 中获取节点和值.在你的情况下,我看到了两个任务.

                  I use Xpath to fetch nodes and values from DOM. In your case I see two tasks.

                  一个任务是迭代文档中的所有记录元素,从第二个文档中获取匹配元素的属性并复制属性.

                  One task to iterate all record elements in on document, fetch the attributes of the matching element from the second document and copy the attributes.

                  如果这里没有具有该 ProductId 的元素,则另一个任务是迭代第二个文档中的所有记录元素并将它们添加到第一个文档中.

                  The other task to iterate all record elements in the second document and add them to the first if here is no element with that ProductId.

                  $xmlOne = <<<'XML'
                  <Products>
                   <record ProductId="366" ProductName="Test" ProductCategory="Categ1"></record>
                  </Products>
                  XML;
                  
                  $xmlTwo = <<<'XML'
                  <Productprices>
                   <record ProductId="366" ProductPrice="10" ProductVAT="24"></record>
                   <record ProductId="444" ProductPrice="23" ProductVAT="32"></record>
                  </Productprices>
                  XML;
                  
                  $targetDom = new DOMDocument();
                  $targetDom->loadXml($xmlOne);
                  $targetXpath = new DOMXpath($targetDom);
                  
                  $addDom = new DOMDocument();
                  $addDom->loadXml($xmlTwo);
                  $addXpath = new DOMXpath($addDom);
                  
                  // merge attributes of record elements depending on ProductId
                  foreach ($targetXpath->evaluate('//record[@ProductId]') as $record) {
                    $productId = $record->getAttribute('ProductId');
                    foreach ($addXpath->evaluate('//record[@ProductId='.$productId.']/@*') as $attribute) {
                      if (!$record->hasAttribute($attribute->name)) {
                        $record->setAttribute($attribute->name, $attribute->value);
                      }
                    }
                  }
                  
                  // copy records elements that are not in target dom
                  foreach ($addXpath->evaluate('//record[@ProductId]') as $record) {
                    $productId = $record->getAttribute('ProductId');
                    if ($targetXpath->evaluate('count(//record[@ProductId='.$productId.'])') == 0) {
                      $targetDom->documentElement->appendChild(
                        $targetDom->importNode($record)
                      );
                    }
                  }
                  
                  echo $targetDom->saveXml();
                  

                  这篇关于根据共同属性合并两个xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PHP中简单而干净的xml操作 下一篇:SimpleXML:如何查找顶级元素的子元素数量?

                  相关文章

                  <legend id='sNcY3'><style id='sNcY3'><dir id='sNcY3'><q id='sNcY3'></q></dir></style></legend>
                    <bdo id='sNcY3'></bdo><ul id='sNcY3'></ul>

                3. <i id='sNcY3'><tr id='sNcY3'><dt id='sNcY3'><q id='sNcY3'><span id='sNcY3'><b id='sNcY3'><form id='sNcY3'><ins id='sNcY3'></ins><ul id='sNcY3'></ul><sub id='sNcY3'></sub></form><legend id='sNcY3'></legend><bdo id='sNcY3'><pre id='sNcY3'><center id='sNcY3'></center></pre></bdo></b><th id='sNcY3'></th></span></q></dt></tr></i><div id='sNcY3'><tfoot id='sNcY3'></tfoot><dl id='sNcY3'><fieldset id='sNcY3'></fieldset></dl></div>
                  <tfoot id='sNcY3'></tfoot>

                      <small id='sNcY3'></small><noframes id='sNcY3'>