0

I know its possible to remove empty XML tags using XPath (as seen here - Remove empty tags from a XML with PHP)

$xpath = new DOMXPath($doc);

foreach( $xpath->query('//*[not(node())]') as $node ) {
    $node->parentNode->removeChild($node);
}

$doc->formatOutput = true;
echo $doc->savexml();

but is it possible to use a similar method to still remove empty tags but keep ones that have attributes?

e.g.

<range starts_at="2012-11-22" ends_at="2012-11-26"></range>

1 Answer 1

3

Try with this XPath

'//*[not(node()) and not(@*)]'
Sign up to request clarification or add additional context in comments.

1 Comment

Do you know how include nodes with just void attributes too ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.