如何删除所有节点,如xml:space ="保留"来自XML,以获得干净的结果
旧XML
<table>
<actor xml:space="preserve"> </actor>
</table>
我希望结果像这样
<table>
<actor> </actor>
</table>
修改
这是php代码function produce_XML_object_tree($raw_XML) {
libxml_use_internal_errors(true);
try {
$xmlTree = new SimpleXMLElement($raw_XML);
} catch (Exception $e) {
// Something went wrong.
$error_message = 'SimpleXMLElement threw an exception.';
foreach(libxml_get_errors() as $error_line) {
$error_message .= "\t" . $error_line->message;
}
trigger_error($error_message);
return false;
}
return $xmlTree;
}
$xml_feed_url = "www.xmlpage.com/web.xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_feed_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
curl_close($ch);
$cont = produce_XML_object_tree($xml);
echo json_encode($cont);
答案 0 :(得分:1)
使用xpath表达式找到属性并将其删除。
//$xml = your xml string
$dom = new DOMDocument();
$dom->loadXML($xml);
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//@xml:space') as $attr) {
$attr->ownerElement->removeAttributeNode($attr);
}
echo $dom->saveXML();
<?xml version="1.0"?>
<table>
<actor> </actor>
</table>
这将删除任何xml:space属性。如果您只想定位值为“preserve”的xml:space属性,请将查询更改为//@xml:space[.="preserve"]
。
答案 1 :(得分:0)
$ string = str_ireplace(&#39; xml:space =&#34;保留&#34;&#39;,&#39;&#39;,$ string);
function produce_XML_object_tree($raw_XML) {
libxml_use_internal_errors(true);
try {
$xmlTree = new SimpleXMLElement($raw_XML);
} catch (Exception $e) {
// Something went wrong.
$error_message = 'SimpleXMLElement threw an exception.';
foreach(libxml_get_errors() as $error_line) {
$error_message .= "\t" . $error_line->message;
}
trigger_error($error_message);
return false;
}
return str_ireplace('xml:space="preserve"','',$xmlTree;);
}
$xml_feed_url = "www.xmlpage.com/web.xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_feed_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
curl_close($ch);
$cont = produce_XML_object_tree($xml);
echo json_encode($cont);
答案 2 :(得分:0)
只要您关注删除所有带有命名空间前缀的属性节点,就可以通过xpath选择它们并从XML文档中删除它们。
可以通过将名称(前缀和本地名称)与本地名称(仅限本地名称)进行比较来获取具有前缀的所有属性的xpath查询。如果它不同,你就有了匹配:
//@*[name(.) != local-name(.)]
使用SimpleXML和XPath查询特定节点以删除它们outlined earlier as an answer问题为Remove a child with a specific attribute, in SimpleXML for PHP (Nov 2008),使用 SimpleXML-Self-Reference 非常简单:
$xml = simplexml_load_string($buffer);
foreach ($xml->xpath('//@*[name(.) != local-name(.)]') as $attr) {
unset($attr[0]);
}
此处的自我引用是通过$attr
删除属性$attr[0]
。
完整示例:
$buffer = <<<XML
<table>
<actor class="foo" xml:space="preserve"> </actor>
</table>
XML;
$xml = simplexml_load_string($buffer);
foreach ($xml->xpath('//@*[name(.) != local-name(.)]') as $attr) {
unset($attr[0]);
}
echo $xml->asXML();
示例输出:
<?xml version="1.0"?>
<table>
<actor class="foo"> </actor>
</table>