如果xml节点没有子节点且为空,则如何删除它

I am creating xml nodes with conditions and want to delete a node if it is having no child nodes:

<main>
  <tags> 
    <tag></tag>
    <tag>hello</tag>
    <tag>hello1</tag>
  </tags>
</main>

I want this output before saving the xml:

<main>
  <tags> 
    <tag>hello</tag>
    <tag>hello1</tag>
  </tags>
</main>

please ignore node names : if I delete it is deleting but end tag is not removed like </tag> is still present after remove:

$doc = new DOMDocument;
$doc->load($path);

$element = $doc->documentElement;

$elementtodelete = $element->getElementsByTagName('OthersInc')->item(0);

if(! $elementtodelete->hasChildNodes()) {
    $oldelement = $elementtodelete->parentNode->removeChild($elementtodelete);
}

check this code

<?php
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->loadxml('<main>
      <tags> 
        <tag></tag>
        <tag>hello</tag>
        <tag>hello1</tag>
      </tags>
    </main>');

$xpath = new DOMXPath($doc);

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

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

check your desired output