Search code examples
phpxmlphpquery

Remove empty XML elements with phpQuery


As mentioned in title, I'd like remove all empty elements from XML document.

By empty I mean elements that don't have any text nodes in it or in its children.

Is it possible to do that with phpQuery?


Solution

  • I used Gordon's code from answer in this topic: Reg expression to remove empty Tags (any of them)?

    Firstly I tried just to put his XPath query into phpQueryObject::find() method, but it gave me a warning saying it's incorrect query. Don't know why since it's using DOMXPath and should work.

    Anyway the solution was still quite simple.

    $pqDoc = phpquery::newDocument() // phpQueryObject created some way. Doesn't matter here.
    $xp = new DOMXPath($pqDoc->getDOMDocument());
    foreach($xp->query('//*[not(node()) or normalize-space() = ""]') as $node) {
        $node->parentNode->removeChild($node);
    }
    

    Now you have removed empty elements and you still can use your changed phpQueryObject since it has actually working on DOMDocument's reference.