[Solved] How to concatenate similar tags in a XML [duplicate]


Here is one way to solve the problem. It doesn’t hardcode any of the child node names (TI, MO, AU, etc.) so you could potentially use it on similar documents. I’ve put comments in the code so please read them and ask if you don’t understand what the code is doing.

$txt="your XML string goes here";
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($txt);

$xp = new DOMXPath($dom);

# find all the nodes types that appear under Record
$cnode_type = array();
foreach ($xp->query("/Results/Recordset/Record/*") as $c) {
    $cnode_type[] = $c->nodeName;
}
# cnode_type now contains the different child node types
$cnode_type = array_unique($cnode_type);

# get all the Record nodes
$recs = $xp->query("/Results/Recordset/Record");

# for every Record node...
foreach ($recs as $par) {
    # for each type of child node...
    foreach ($cnode_type as $c) {
        # run an XPath query to count the number of children of node type $c
        # if there are more than one, we need to remove the extras
        if ($xp->evaluate("count($c)", $par) > 1) {
            # go through all the $c nodes, saving the value in $node_vals
            # delete the node
            $node_vals = [];
            foreach ($xp->query($c, $par) as $n) {
                # only save the contents of nodes with a value
                if (isset($n->nodeValue) && strlen($n->nodeValue) > 0) {
                    $node_vals[] = $n->nodeValue;
                }
                $par->removeChild($n);
            }
            # create a new $c node and set the value to the list in $node_vals
            # add it to the parent node
            $new_node = $dom->createElement($c, implode("; ", $node_vals));
            $par->appendChild($new_node);
        }
    }
}
# print out the result
echo $dom->saveXML();

Output from the XML you have posted:

<?xml version="1.0"?>
<Results>
  <Recordset setCount="3">
    <Record setEntry="0">
      <TI>Test-1</TI>
      <MO>Mo-1</MO>
      <JF>OK</JF>
      <JT/>
      <AU>One; Two; three</AU>
    </Record>
    <Record setEntry="1">
      <TI>Test-2</TI>
      <MO>Mo-2</MO>
      <JF/>
      <JT/>
      <AU>One; Two; Three; Four; Five; Six; Seven</AU>
    </Record>
    <Record setEntry="2">
      <TI>Test31</TI>
      <MO>Mo-3</MO>
      <JF/>
      <JT/>
      <AU>One</AU>
    </Record>
  </Recordset>
</Results>

5

solved How to concatenate similar tags in a XML [duplicate]