Cloning Distinct XML structures without data in PHP I struggled for the last couple days on this question and finally came up with a solution. But it's got so many loops I get dizzy just looking at the code - from recursive functions with loops to loops within loops.

I really expected to find a canned function in a popular XML class to extract all the unique nodes without data, but I am surprised how non-trivial I've found this endeavor.

While this works for my piddly example data, I have performance and scalability concerns when trying to use this code in production. For example, this elegant answer uses XSLT to solve the problem, but it's limited by node depth. So is there anyway to improve efficiency by cutting out a loop, or using less objects, or even utilizing a different class or technology completely? Are there any glaring failing conditions that aren't represented in my example data?

&lt;?php
$xml = '
&lt;root&gt;
  &lt;node/&gt;
  &lt;node&gt;
    &lt;sub&gt;more&lt;/sub&gt;
  &lt;/node&gt;
  &lt;node&gt;
    &lt;sub&gt;another&lt;/sub&gt;
  &lt;/node&gt;
  &lt;node&gt;value&lt;/node&gt;
&lt;/root&gt;
';
$doc = new DOMDocument();
$doc-&gt;loadXML($xml);


// clone without data
$empty_xml = new DOMDocument();
$empty_xml-&gt;appendChild($empty_xml-&gt;importNode($doc-&gt;documentElement));
function clone_without_data(&amp;$orig, &amp;$clone, &amp;$clonedoc){
  foreach ($orig-&gt;childNodes as $child){
    if(get_class($child) === "DOMElement")
      $new_node = $clone-&gt;appendChild($clonedoc-&gt;importNode($child));
    if($child-&gt;hasChildNodes())
      clone_without_data($child,$new_node,$clonedoc);
  }
}
clone_without_data($doc-&gt;documentElement, $empty_xml-&gt;documentElement, $empty_xml);


// remove all duplicates
$distinct_structure = new DOMDocument();
$distinct_structure-&gt;appendChild($distinct_structure-&gt;importNode($doc-&gt;documentElement));
foreach ($empty_xml-&gt;documentElement-&gt;childNodes as $child){
  $match = false;
  foreach ($distinct_structure-&gt;documentElement-&gt;childNodes as $element){
    if ($distinct_structure-&gt;saveXML($element) === $empty_xml-&gt;saveXML($child)) {
      $match = true;
      break;
    }
  }
  if (!$match)
    $distinct_structure-&gt;documentElement-&gt;appendChild($distinct_structure-&gt;importNode($child,true));
}
$distinct_structure-&gt;formatOutput = true;
echo $distinct_structure-&gt;saveXML();


This results in the unique XML structures stripped of all data:

&lt;?xml version="1.0"?&gt;
&lt;root&gt;
  &lt;node/&gt;
  &lt;node&gt;
    &lt;sub/&gt;
  &lt;/node&gt;
&lt;/root&gt;
I would consider getting all nodes using getElementsByTagName('*') (note wildcard usage).

I would also consider simply cloning the $doc object.

That could simplify to this:

$doc = new DOMDocument();
$doc-&gt;loadXML($xml);
$clone = cloneDOMDocument($doc, true);

function cloneDOMDocument(DOMDocument $source, $remove_values = false) {  
    $clone = clone $source;
    if($remove_values === true) {
        // select all nodes in clone
        $all_nodes = $clone-&gt;getElementsByTagName('*');

        // iterate all nodes in clone, resetting leaf values to empty string
        foreach($all_nodes as $node) {
            if($node-&gt;hasChildNodes() === false) {
                $node-&gt;nodeValue = '';
            }
        }
    }
    return $clone;
}