使用 PHP 从 XML 中删除命名空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1245902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove namespace from XML using PHP
提问by jchimpo
I have an XML document that looks like this:
我有一个如下所示的 XML 文档:
<Data
xmlns="http://www.domain.com/schema/data"
xmlns:dmd="http://www.domain.com/schema/data-metadata"
>
<Something>...</Something>
</Data>
I am parsing the information using SimpleXML in PHP. I am dealing with arrays and I seem to be having a problem with the namespace.
我正在使用 PHP 中的 SimpleXML 解析信息。我正在处理数组,但命名空间似乎有问题。
My question is: How do I remove those namespaces? I read the data from an XML file.
我的问题是:如何删除这些命名空间?我从 XML 文件中读取数据。
Thank you!
谢谢!
采纳答案by null
If you're using XPath then it's a limitation with XPath and notPHP look at this explanation on xpath and default namespacesfor more info.
如果您使用的是 XPath,那么它是 XPath 的限制,而不是PHP,请查看有关xpath 和默认命名空间的解释以获取更多信息。
More specifically its the xmlns=""attribute in the root node which is causing the problem. This means that you'll need to register the namespace then use a QNamethereafter to refer to elements.
更具体地说,它xmlns=""是导致问题的根节点中的属性。这意味着您需要注册命名空间,然后使用QName来引用元素。
$feed = simplexml_load_file('http://www.sitepoint.com/recent.rdf');
$feed->registerXPathNamespace("a", "http://www.domain.com/schema/data");
$result = $feed->xpath("a:Data/a:Something/...");
Important: The URI used in the registerXPathNamespacecall must be identical to the one that is used in the actual XML file.
重要提示:registerXPathNamespace调用中使用的 URI必须与实际 XML 文件中使用的 URI相同。
回答by Chris Lawrence
I found the answer above to be helpful, but it didn't quite work for me. This ended up working better:
我发现上面的答案很有帮助,但对我来说并不奏效。这最终效果更好:
// Gets rid of all namespace definitions
$xml_string = preg_replace('/xmlns[^=]*="[^"]*"/i', '', $xml_string);
// Gets rid of all namespace references
$xml_string = preg_replace('/[a-zA-Z]+:([a-zA-Z]+[=>])/', '', $xml_string);
回答by Alex
The following PHP code automatically detects the default namespace specified in the XML file under the alias "default". No all xpath queries have to be updated to include the prefix default:
以下 PHP 代码自动检测 XML 文件中别名“default”下指定的默认命名空间。无需更新所有 xpath 查询以包含前缀 default:
So if you want to read XML files rather they contain an default NS definition or they don't and you want to query all Somethingelements, you could use the following code:
因此,如果您想读取 XML 文件而不是它们包含默认 NS 定义,或者它们不包含并且您想查询所有Something元素,则可以使用以下代码:
$xml = simplexml_load_file($name);
$namespaces = $xml->getDocNamespaces();
if (isset($namespaces[''])) {
$defaultNamespaceUrl = $namespaces[''];
$xml->registerXPathNamespace('default', $defaultNamespaceUrl);
$nsprefix = 'default:';
} else {
$nsprefix = '';
}
$somethings = $xml->xpath('//'.$nsprefix.'Something');
echo count($somethings).' times found';
回答by null
To remove the namespace completely, you'll need to use Regular Expressions (RegEx). For example:
要完全删除命名空间,您需要使用正则表达式 (RegEx)。例如:
$feed = file_get_contents("http://www.sitepoint.com/recent.rdf");
$feed = preg_replace("/<.*(xmlns *= *[\"'].[^\"']*[\"']).[^>]*>/i", "", $feed); // This removes ALL default namespaces.
$xml_feed = simplexml_load_string($feed);
Then you've stripped any xml namespaces before you load the XML (be careful with the regex through, because if you have any fields with something like:
然后,您在加载 XML 之前剥离了任何 xml 命名空间(小心使用正则表达式,因为如果您有任何字段,例如:
<![CDATA[ <Transfer xmlns="http://redeux.example.com">cool.</Transfer> ]]>
Then it will strip the xmlns from inside the CDATA which may lead to unexpected results.
然后它将从 CDATA 内部剥离 xmlns,这可能会导致意外结果。

