Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove empty tags from a XML with PHP

Tags:

php

xml

tags

Question

How can I remove empty xml tags in PHP?

Example:

 $value1 = "2";
 $value2 = "4";
 $value3 = "";

 xml = '<parentnode>
        <tag1> ' .$value1. '</tag1>
        <tag2> ' .$value2. '</tag2>
        <tag3> ' .$value3. '</tag3>
       </parentnode>';

XML Result:

<parentnode>
    <tag1>2</tag1>
    <tag2>4</tag2>
    <tag3></tag3> // <- Empty tag
</parentnode>

What I want!

    <parentnode>
            <tag1>2</tag1>
            <tag2>4</tag2> 
    </parentnode>

The XML without the empty tags like "tag3"

Thanks!

like image 762
Jimmy Avatar asked Dec 22 '11 11:12

Jimmy


4 Answers

This works recursively and removes nodes that:

  • contain only spaces
  • do not have attributes
  • do not have child notes
// not(*) does not have children elements
// not(@*) does not have attributes
// text()[normalize-space()] nodes that include whitespace text
while (($node_list = $xpath->query('//*[not(*) and not(@*) and not(text()[normalize-space()])]')) && $node_list->length) {
    foreach ($node_list as $node) {
        $node->parentNode->removeChild($node);
    }
}
like image 192
Gajus Avatar answered Sep 28 '22 12:09

Gajus


You can use XPath with the predicate not(node()) to select all elements that do not have child nodes.

<?php
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->loadxml('<parentnode>
    <tag1>2</tag1>
    <tag2>4</tag2>
    <tag3></tag3>
    <tag2>4</tag2>
    <tag3></tag3>
    <tag2>4</tag2>
    <tag3></tag3>
</parentnode>');

$xpath = new DOMXPath($doc);

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

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

prints

<?xml version="1.0"?>
<parentnode>
  <tag1>2</tag1>
  <tag2>4</tag2>
  <tag2>4</tag2>
  <tag2>4</tag2>
</parentnode>
like image 38
VolkerK Avatar answered Sep 28 '22 11:09

VolkerK


The solution that worked with my production PHP SimpleXMLElement object code, by using Xpath, was:

/*
 * Remove empty (no children) and blank (no text) XML element nodes, but not an empty root element (/child::*).
 * This does not work recursively; meaning after empty child elements are removed, parents are not reexamined.
 */
foreach( $this->xml->xpath('/child::*//*[not(*) and not(text()[normalize-space()])]') as $emptyElement ) {
    unset( $emptyElement[0] );
}

Note that it is not required to use PHP DOM, DOMDocument, DOMXPath, or dom_import_simplexml().

//this is a recursively option

do {
    $removed = false;
    foreach( $this->xml->xpath('/child::*//*[not(*) and not(text()[normalize-space()])]') as $emptyElement ) {
        unset( $emptyElement[0] );
        $removed = true;
    }
} while ($removed) ;
like image 40
Jeff Sheffel Avatar answered Sep 28 '22 11:09

Jeff Sheffel


$dom = new DOMDocument;

$dom->loadXML($xml);

$elements = $dom->getElementsByTagName('*');

foreach($elements as $element) {

   if ( ! $element->hasChildNodes() OR $element->nodeValue == '') {
       $element->parentNode->removeChild($element);
   }

} 

echo $dom->saveXML();

CodePad.

like image 31
alex Avatar answered Sep 28 '22 11:09

alex