duanfan5012 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;
$domxml->loadXML($this->response);
$this->response = $domxml->saveXML($domxml->documentElement);

Anyone know of a better way to do this?

  • 写回答

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.

    Xpath

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

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

    Put together:

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

    Output:

    <?xml version="1.0"?>
    <foo>
      <bar>123</bar>
      <bar foo="123"/>
      <bar>
        <!-- test -->
      </bar>
    </foo>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低