问题描述
|
我有file.xml包含:
<?xml version=\"1.0\"?>
<Document xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:iso:std:iso:20022:tech:xsd:test.001.001.01\">
<book>
<author>Jack Herrington</author>
<title>PHP Hacks</title>
<publisher>O\'Reilly</publisher>
</book>
</Document>
有人知道如何在<document....>
示例the2ѭ中获得此信息吗?
<Document xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:iso:std:iso:20022:tech:xsd:test.001.001.01\">
解决方法
使用SimpleXML获取名称空间,如下所示:
$xmlstr = <<<XML
<?xml version=\"1.0\"?>
<Document xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:iso:std:iso:20022:tech:xsd:test.001.001.01\">
<book>
<author>Jack Herrington</author>
<title>PHP Hacks</title>
<publisher>O\'Reilly</publisher>
</book>
</Document>
XML;
$xml = simplexml_load_string($xmlstr);
$namespaces = $xml->getDocNamespaces();
var_export($namespaces);
该代码将产生:
array ( \'xsd\' => \'http://www.w3.org/2001/XMLSchema\',\'xsi\' => \'http://www.w3.org/2001/XMLSchema-instance\',\'\' => \'urn:iso:std:iso:20022:tech:xsd:test.001.001.01\',)
文档中声明的所有名称空间的数组。