title: Modification and replacement of nodes in xml file
body: <p>On daily basis, i receive an .xml file, but some additional editing of element values is required. I've done what i need in Excel via pivot tables, but that solution is not acceptable, since that creates new structure and i cant save it as xml, and i need same xml structure as output. Since i have PHP Composer installed, i think simpleXML library is the easyest solution. Raw .xml file is uploaded and modifications are done via simplexml_load_string function. Thing is, im not very profficient in that syntax, and i need some assistance. This is my .xml file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-2"?&gt;
  &lt;ZCOLL&gt;
    &lt;IDOC BEGIN="1"&gt;
      &lt;EDI_DC40 SEGMENT="1"&gt;
          &lt;DOCNUM&gt;0000000008857855&lt;/DOCNUM&gt;
          &lt;SERIAL&gt;20191025143123&lt;/SERIAL&gt;
      &lt;/EDI_DC40&gt;
      &lt;Z1COLL_AGENCY SEGMENT="1"&gt;
          &lt;GPART&gt;0000000101&lt;/GPART&gt;
          &lt;EMAIL&gt;domain@domain.com&lt;/EMAIL&gt;                        
          &lt;Z1COLL_HEADER SEGMENT="1"&gt;
              &lt;VKONT&gt;200000541301&lt;/VKONT&gt;
              &lt;GPART&gt;1000447089&lt;/GPART&gt;
              &lt;VKONA&gt;22611402001&lt;/VKONA&gt;
              &lt;INKNO&gt;00000000000101953558&lt;/INKNO&gt;
              &lt;INKBP&gt;0000000101&lt;/INKBP&gt;
              &lt;INKDAT&gt;20191025&lt;/INKDAT&gt;
              &lt;INKENDAT&gt;20200123&lt;/INKENDAT&gt;
              &lt;BANKRUPTDAT&gt;00000000&lt;/BANKRUPTDAT&gt;
              &lt;CLOSED/&gt;
              &lt;UPDATED_DATE&gt;00000000&lt;/UPDATED_DATE&gt;
              &lt;COLLREFNR&gt;200005413019-024-6&lt;/COLLREFNR&gt;
              &lt;NAME_ORG1&gt;SOME OTHER NAME&lt;/NAME_ORG1&gt;
              &lt;LEG_CITY1&gt;SOME OTHER CITY&lt;/LEG_CITY1&gt;
              &lt;LEG_POST_CODE1&gt;105677&lt;/LEG_POST_CODE1&gt;
              &lt;LEG_STREET&gt;ADDRESS 2&lt;/LEG_STREET&gt;
              &lt;LEG_HOUSE_NUM1/&gt;
              &lt;BU_SORT2&gt;02226696981&lt;/BU_SORT2&gt;
              &lt;MAIL_CITY1&gt;CITY 1&lt;/MAIL_CITY1&gt;
              &lt;MAIL_POST_CODE1&gt;35220&lt;/MAIL_POST_CODE1&gt;
              &lt;MAIL_STREET&gt;MAIL STREET 1&lt;/MAIL_STREET&gt;
              &lt;MAIL_HOUSE_NUM1/&gt;
                 &lt;Z1COLL_ITEM SEGMENT="1"&gt;
                     &lt;OPBEL&gt;000210625857&lt;/OPBEL&gt;
                     &lt;XBLNR&gt;0000198653579124&lt;/XBLNR&gt;
                     &lt;FAEDN&gt;20190916&lt;/FAEDN&gt;
                     &lt;AGDAT&gt;20191025&lt;/AGDAT&gt;
                     &lt;INITAMNT&gt;         80.00&lt;/INITAMNT&gt;
                     &lt;PAYAMNT&gt;          0.00&lt;/PAYAMNT&gt;
                     &lt;WRTOFFAMNT&gt;          0.00&lt;/WRTOFFAMNT&gt;
                     &lt;OPENAMNT&gt;         80.00&lt;/OPENAMNT&gt;
                     &lt;INVAMNT&gt;         80.00&lt;/INVAMNT&gt;
                     &lt;WAERS&gt;HRK&lt;/WAERS&gt;
                     &lt;RECINKDAT&gt;00000000&lt;/RECINKDAT&gt;
                 &lt;/Z1COLL_ITEM&gt;
  
                 &lt;Z1COLL_ITEM SEGMENT="1"&gt;
                     &lt;OPBEL&gt;000210625857&lt;/OPBEL&gt;
                     &lt;XBLNR&gt;0000198653579124&lt;/XBLNR&gt;
                     &lt;FAEDN&gt;20191016&lt;/FAEDN&gt;
                     &lt;AGDAT&gt;20191025&lt;/AGDAT&gt;
                     &lt;INITAMNT&gt;         80.00&lt;/INITAMNT&gt;
                     &lt;PAYAMNT&gt;          0.00&lt;/PAYAMNT&gt;
                     &lt;WRTOFFAMNT&gt;          0.00&lt;/WRTOFFAMNT&gt;
                     &lt;OPENAMNT&gt;         80.00&lt;/OPENAMNT&gt;
                     &lt;INVAMNT&gt;         80.00&lt;/INVAMNT&gt;
                     &lt;WAERS&gt;HRK&lt;/WAERS&gt;
                     &lt;RECINKDAT&gt;00000000&lt;/RECINKDAT&gt;
                 &lt;/Z1COLL_ITEM&gt;
  
                 &lt;Z1COLL_ITEM SEGMENT="1"&gt;
                     &lt;OPBEL&gt;019183828875&lt;/OPBEL&gt;
                     &lt;XBLNR&gt;2261140200119081&lt;/XBLNR&gt;
                     &lt;FAEDN&gt;20190816&lt;/FAEDN&gt;
                     &lt;AGDAT&gt;20191025&lt;/AGDAT&gt;
                     &lt;INITAMNT&gt;        159.00&lt;/INITAMNT&gt;
                     &lt;PAYAMNT&gt;          0.00&lt;/PAYAMNT&gt;
                     &lt;WRTOFFAMNT&gt;          0.00&lt;/WRTOFFAMNT&gt;
                     &lt;OPENAMNT&gt;        159.00&lt;/OPENAMNT&gt;
                     &lt;INVAMNT&gt;        159.00&lt;/INVAMNT&gt;
                     &lt;WAERS&gt;CURRENCY&lt;/WAERS&gt;
                     &lt;RECINKDAT&gt;00000000&lt;/RECINKDAT&gt;
                 &lt;/Z1COLL_ITEM&gt;
  
                 &lt;MSISDNS&gt;
                     &lt;MSISDN&gt;381653490012&lt;/MSISDN&gt;
                 &lt;/MSISDNS&gt;
                 &lt;MCD_DATA&gt;
                     &lt;MCD_MONTHS/&gt;
                     &lt;MCD_AMOUNT/&gt;
                 &lt;/MCD_DATA&gt;
              &lt;PHONE_NUM/&gt;
          &lt;/Z1COLL_HEADER&gt;            
       &lt;/Z1COLL_AGENCY&gt;
    &lt;/IDOC&gt;
  &lt;/ZCOLL&gt;
  </code></pre>
  
  <p>What i need is, if OPBEL and XBLNR element values are identical, delete that node and replace it with single duplicated value of OPBEL and XBLNR and with summarized values of INITAMNT, PAYAMNT, WRTOFFAMNT, OPENAMNT and INVAMNT, and return .xml file with same structure.</p>
  
  <p>This is my code, im stuck half way:</p>
  
  <pre><code>if (isset($_POST['submit'])) {
      //echo "&lt;pre&gt;";
      //print_r($_FILES['file']['name']);
      //echo "&lt;/pre&gt;";
      if(isset($_FILES['file']['name'])) {
          $get = file_get_contents($_FILES['file']['tmp_name']);
          $arr = simplexml_load_string($get);
  
      foreach ($arr-&gt;IDOC-&gt;Z1COLL_AGENCY-&gt;Z1COLL_HEADER as $element) {
          $delete_node = array();
          foreach($element-&gt;Z1COLL_ITEM as $item) {   
              $doubles_XBLNR = array_count_values($item-&gt;XBLNR);
              $doubles_OPBEL = array_count_values($item-&gt;OPBEL);
              $doubles_no_XBLNR = count($doubles_XBLNR);
              $doubles_no_OPBEL = count($doubles_OPBEL);
                  if (($doubles_no_XBLNR &gt; 2) &amp;&amp; ($doubles_no_OPBEL &gt; 2)){
                      $item-&gt;INITAMNT += $item-&gt;INITAMNT;
                      $item-&gt;PAYAMNT += $item-&gt;PAYAMNT;
                      $item-&gt;WRTOFFAMNT += $item-&gt;WRTOFFAMNT;
                      $$item-&gt;OPENAMNT += $item-&gt;OPENAMNT;
                      $item-&gt;INVAMNT += $item-&gt;INVAMNT;
                  }                   
          }
          echo "&lt;br /&gt;";  
      }
  </code></pre>
  
  <p>And this is desired xml output:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-2"?&gt;
  &lt;ZCOLL&gt;
    &lt;IDOC BEGIN="1"&gt;
      &lt;EDI_DC40 SEGMENT="1"&gt;
          &lt;DOCNUM&gt;0000000008857855&lt;/DOCNUM&gt;
          &lt;SERIAL&gt;20191025143123&lt;/SERIAL&gt;
      &lt;/EDI_DC40&gt;
      &lt;Z1COLL_AGENCY SEGMENT="1"&gt;
          &lt;GPART&gt;0000000101&lt;/GPART&gt;
          &lt;EMAIL&gt;domain@domain.com&lt;/EMAIL&gt;                    
          &lt;Z1COLL_HEADER SEGMENT="1"&gt;
              &lt;VKONT&gt;200000541301&lt;/VKONT&gt;
              &lt;GPART&gt;1000447089&lt;/GPART&gt;
              &lt;VKONA&gt;22611402001&lt;/VKONA&gt;
              &lt;INKNO&gt;00000000000101953558&lt;/INKNO&gt;
              &lt;INKBP&gt;0000000101&lt;/INKBP&gt;
              &lt;INKDAT&gt;20191025&lt;/INKDAT&gt;
              &lt;INKENDAT&gt;20200123&lt;/INKENDAT&gt;
              &lt;BANKRUPTDAT&gt;00000000&lt;/BANKRUPTDAT&gt;
              &lt;CLOSED/&gt;
              &lt;UPDATED_DATE&gt;00000000&lt;/UPDATED_DATE&gt;
              &lt;COLLREFNR&gt;200005413019-024-6&lt;/COLLREFNR&gt;
              &lt;NAME_ORG1&gt;SOME OTHER NAME&lt;/NAME_ORG1&gt;
              &lt;LEG_CITY1&gt;SOME OTHER CITY&lt;/LEG_CITY1&gt;
              &lt;LEG_POST_CODE1&gt;105677&lt;/LEG_POST_CODE1&gt;
              &lt;LEG_STREET&gt;ADDRESS 2&lt;/LEG_STREET&gt;
              &lt;LEG_HOUSE_NUM1/&gt;
              &lt;BU_SORT2&gt;02226696981&lt;/BU_SORT2&gt;
              &lt;MAIL_CITY1&gt;CITY 1&lt;/MAIL_CITY1&gt;
              &lt;MAIL_POST_CODE1&gt;35220&lt;/MAIL_POST_CODE1&gt;
              &lt;MAIL_STREET&gt;MAIL STREET 1&lt;/MAIL_STREET&gt;
              &lt;MAIL_HOUSE_NUM1/&gt;
                 &lt;Z1COLL_ITEM SEGMENT="1"&gt;
                     &lt;OPBEL&gt;000210625857&lt;/OPBEL&gt;
                     &lt;XBLNR&gt;0000198653579124&lt;/XBLNR&gt;
                     &lt;FAEDN&gt;20190916&lt;/FAEDN&gt;
                     &lt;AGDAT&gt;20191025&lt;/AGDAT&gt;
                     &lt;INITAMNT&gt;         160.00&lt;/INITAMNT&gt;
                     &lt;PAYAMNT&gt;          0.00&lt;/PAYAMNT&gt;
                     &lt;WRTOFFAMNT&gt;          0.00&lt;/WRTOFFAMNT&gt;
                     &lt;OPENAMNT&gt;         160.00&lt;/OPENAMNT&gt;
                     &lt;INVAMNT&gt;         160.00&lt;/INVAMNT&gt;
                     &lt;WAERS&gt;CURRENCY&lt;/WAERS&gt;
                     &lt;RECINKDAT&gt;00000000&lt;/RECINKDAT&gt;
                 &lt;/Z1COLL_ITEM&gt;
  
                 &lt;Z1COLL_ITEM SEGMENT="1"&gt;
                     &lt;OPBEL&gt;019183828875&lt;/OPBEL&gt;
                     &lt;XBLNR&gt;2261140200119081&lt;/XBLNR&gt;
                     &lt;FAEDN&gt;20190816&lt;/FAEDN&gt;
                     &lt;AGDAT&gt;20191025&lt;/AGDAT&gt;
                     &lt;INITAMNT&gt;        159.00&lt;/INITAMNT&gt;
                     &lt;PAYAMNT&gt;          0.00&lt;/PAYAMNT&gt;
                     &lt;WRTOFFAMNT&gt;          0.00&lt;/WRTOFFAMNT&gt;
                     &lt;OPENAMNT&gt;        159.00&lt;/OPENAMNT&gt;
                     &lt;INVAMNT&gt;        159.00&lt;/INVAMNT&gt;
                     &lt;WAERS&gt;CURRENCY&lt;/WAERS&gt;
                     &lt;RECINKDAT&gt;00000000&lt;/RECINKDAT&gt;
                 &lt;/Z1COLL_ITEM&gt;
                 &lt;MSISDNS&gt;
                     &lt;MSISDN&gt;381653490012&lt;/MSISDN&gt;
                 &lt;/MSISDNS&gt;
                 &lt;MCD_DATA&gt;
                     &lt;MCD_MONTHS/&gt;
                     &lt;MCD_AMOUNT/&gt;
                 &lt;/MCD_DATA&gt;
                 &lt;PHONE_NUM/&gt;
           &lt;/Z1COLL_HEADER&gt;           
      &lt;/Z1COLL_AGENCY&gt;
    &lt;/IDOC&gt;
   &lt;/ZCOLL&gt;
  </code></pre>
  
  <p>Any help is appreciated. Thank you.</p>