我正在使用以下代码,在其中传递dom,这将返回没有空节点的XML
/**
* Remove Empty Tags
*
* @return void
* @author Fahad Sheikh
**/
public function remove_empty_tags($dom)
{
// Remove Empty Tags
$xpath = new DOMXPath($dom);
foreach( $xpath->query('//*[not(node())]') as $node ) {
$node->parentNode->removeChild($node);
}
}
但是这会返回以下XML,由于某种原因,它将合并嵌套的XML标签值并删除其标签。
<formxml>
<type>Potential customer</type>
<origin>Content</origin>
<source>test source<medium>test medium</medium>
<campaign>test campaign</campaign>
<matchtype>terms</matchtype>
<test>valueonevaluetwo</test>
<keyword>test term</keyword>
<ad>test</ad>
</formxml>
相反,它应该是:
<formxml>
<type>Potential customer</type>
<origin>Content</origin>
<source>test source<medium>test medium</medium>
<campaign>test campaign</campaign>
<matchtype>terms</matchtype>
<test>
<testvarone>valueone</testvarone>
<testvartwo>valuetwo</testvartwo>
</test>
<keyword>test term</keyword>
<ad>test</ad>
</formxml>
此处提到了此解决方案: