获取xml的xmlSchema?

时间:2011-05-30 13:01:38

标签: php xml xsd

我有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....>示例xmlns中获取此信息?

<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">

1 个答案:

答案 0 :(得分:3)

使用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', )

文档中声明的所有命名空间的数组。

相关问题