dsvyc66464 2013-08-05 05:21
浏览 53
已采纳

使用PHP删除空XML标记但忽略带有属性的标记

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条回答 默认 最新

  • dongshaidu2456 2013-08-05 07:42
    关注

    Try with this XPath

    '//*[not(node()) and not(@*)]'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?