-->

How do you remove duplicate, nested DOM elements i

2020-08-01 05:01发布

问题:

Assuming you have a DOM tree with nested tags, I would like to clean the DOM object up by removing duplicates. However, this should only apply if the tag only has a single child tag of the same type. For example,

Fix <div><div>1</div></div> and not <div><div>1</div><div>2</div></div>.

I'm trying to figure out how I could do this using PHP's DOM extension. Below is the starting code and I'm looking for help figuring out the logic needed.

<?php

libxml_use_internal_errors(TRUE);

$html = '<div><div><div><p>Some text here</p></div></div></div>';

$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadHTML($html);

function dom_remove_duplicate_nodes($node)
{
    var_dump($node);

    if($node->hasChildNodes())
    {
        for($i = 0; $i < $node->childNodes->length; $i++)
        {
            $child = $node->childNodes->item($i);

            dom_remove_duplicate_nodes($child);
        }
    }
    else
    {
        // Process here?
    }
}

dom_remove_duplicate_nodes($dom);

I collected some helper functions that might make it easier to work the DOM nodes like JavaScript.

function DOM_delete_node($node)
{
    DOM_delete_children($node);
    return $node->parentNode->removeChild($node);
}

function DOM_delete_children($node)
{
    while (isset($node->firstChild))
    {
        DOM_delete_children($node->firstChild);
        $node->removeChild($node->firstChild);
    }
}

function DOM_dump_child_nodes($node)
{
    $output = '';
    $owner_document = $node->ownerDocument;

    foreach ($node->childNodes as $el)
    {
        $output .= $owner_document->saveHTML($el);
    }
    return $output;
}

function DOM_dump_node($node)
{
    if($node->ownerDocument)
    {
        return $node->ownerDocument->saveHTML($node);
    }
}

回答1:

You can do this quite easily with DOMDocument and DOMXPath. XPath especially is really useful in your case because you easily divide the logic to select which elements to remove and the way you remove the elements.

First of all, normalize the input. I was not entirely clear about what you mean with empty whitespace, I thought it could be either empty textnodes (which might have been removed as preserveWhiteSpace is FALSE but I'm not sure) or if their normalized whitespace is empty. I opted for the first (if even necessary), in case it's the other variant I left a comment what to use instead:

$xp = new DOMXPath($dom);

//remove empty textnodes - if necessary at all
// (in case remove WS: [normalize-space()=""])
foreach($xp->query('//text()[""]') as $i => $tn)
{
    $tn->parentNode->removeChild($tn);
}

After this textnode normalization you should not run into the problem you talked about in one comment here.

The next part is to find all elements that have the same name as their parent element and which are the only child. This can be expressed in xpath again. If such elements are found, all their children are moved to the parent element and then the element will be removed as well:

// all child elements with same name as parent element and being
// the only child element.
$r = $xp->query('body//*/child::*[name(.)=name(..) and count(../child::*)=1]');
foreach($r as $i => $dupe)
{
    while($dupe->childNodes->length)
    {
        $child = $dupe->firstChild;
        $dupe->removeChild($child);
        $dupe->parentNode->appendChild($child);
    }   
    $dupe->parentNode->removeChild($dupe);
}

Full demo.

As you can see in the demo, this is independent to textnodes and commments. If you don't want that, e.g. actual texts, the expression to count children needs to stretch over all node types. But I don't know if that is your exact need. If it is, this makes the count of children across all node types:

body//*/child::*[name(.)=name(..) and count(../child::node())=1]

If you did not normalize empty textnodes upfront (remove empty ones), then this too strict. Choose the set of tools you need, I think normalizing plus this strict rule might be the best choice.



回答2:

Seems like you have almost everything you need here. Where you have // Process here? do something like this:

if ($node->parentNode->nodeName == $node->nodeName 
  && $node->parentNode->childNodes->length == 1) {
  $node->parentNode->removeChild($node);
}

Also, you're currently using recursion in dom_remove_duplicate_notes() which can be computationally expensive. It is possible to iterate over every node in the document without recursion using an approach like this: https://github.com/elazar/domquery/blob/master/trunk/DOMQuery.php#L73



回答3:

The following is an almost-working snippet. While it does remove duplicate, nested nodes - it changes the source order because of the ->appendChild().

<?php
header('Content-Type: text/plain');

libxml_use_internal_errors(TRUE);

$html = "<div>\n<div>\n<div>\n<p>Some text here</p>\n</div>\n</div>\n</div>";

$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadHTML($html);

function dom_remove_duplicate_nodes($node)
{
    //var_dump($node);

    if($node->hasChildNodes())
    {
        $newNode = NULL;
        for($i = 0; $i < $node->childNodes->length; $i++)
        {
            $child = $node->childNodes->item($i);

            dom_remove_duplicate_nodes($child);

            if($newNode === FALSE) continue;

            // If there is a parent to check against
            if($child->nodeName == $node->nodeName)
            {
                // Did we already find the same child?
                if($newNode OR $newNode === FALSE)
                {
                    $newNode = FALSE;
                }
                else
                {
                    $newNode = $child;
                }
            }
            elseif($child->nodeName == '#text')
            {
                // Something other than whitespace?
                if(trim($child->nodeValue))
                {
                    $newNode = FALSE;
                }
            }
            else
            {
                $newNode = FALSE;
            }
        }

        if($newNode)
        {
            // Does not transfer $newNode children!!!!
            //$node->parentNode->replaceChild($newNode, $node);

            // Works, but appends in reverse!!
            $node->parentNode->appendChild($newNode);
            $node->parentNode->removeChild($node);
        }
    }
}

print $dom->saveHTML(). "\n\n\n";
dom_remove_duplicate_nodes($dom);
print $dom->saveHTML();


标签: php html dom