2018-07-03 18:42
浏览 53

PHP - 删除XML空节点

I found this code to remove empty nodes from and XML file but it isn't working correctly. It leaves an empty node that really needs to be removed. Yes, it is empty, just white space in it.

$domxml = new DOMDocument('1.0');
$domxml->preserveWhiteSpace = false;
$domxml->formatOutput = true;
$this->response = $domxml->saveXML($domxml->documentElement);

Anyone know of a better way to do this?

图片转代码服务由CSDN问答提供 功能建议

我发现此代码从XML文件中删除空节点但它无法正常工作。 它留下了一个真正需要删除的空节点。 是的,它是空的,只是空格。

  $ domxml = new DOMDocument('1.0'); 
 $ domxml-> preserveWhiteSpace = false; 
  $ domxml-> formatOutput = true; 
 $ domxml-> loadXML($ this-> response); 
 $ this-> response = $ domxml-> saveXML($ domxml-> documentElement);  


  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dongzhenge2014 2018-07-04 08:13

    In other words you would like to remove any element node that has no text content, no attribute, no children with text content or attributes and have a parent element node (are not the document element).

    Here is an Xpath function normalize-space() that converts any whitespace sequences to single spaces and strips them from the start/end. Any whitespace only content will result in an empty string.


    //* fetches any element node in the document in a list. You just need to add conditions.

    • Has no text content
      normalize-space(.) = ""
    • No attributes
    • No descendant node with content (includes comments, ...)
      not(.//node()[normalize-space(.) != ""])
    • No descendant element nodes with attributes
    • Has a parent element node

    Put together:

    $xml = <<<'XML'
      <bar foo="123"></bar>
      <bar><foo>   </foo></bar>
      <bar><!-- test --></bar>
    $document = new DOMDocument();
    $document->preserveWhiteSpace = FALSE;
    $document->formatOutput = TRUE; 
    $xpath = new DOMXpath($document);
    $expression = 
        normalize-space(.) = "" and 
        not(@*) and  
        not(.//node()[normalize-space(.) != ""]) and 
        not(.//*[@*]) and
    $nodes = $xpath->evaluate($expression);
    for ($i = $nodes->length - 1; $i >= 0; $i--) {
    echo $document->saveXml();


    <?xml version="1.0"?>
      <bar foo="123"/>
        <!-- test -->
    解决 无用
    打赏 举报

相关推荐 更多相似问题