在 php 中加载 xml 时出错(不是绝对的)


Error loading xml in php (not absolute)

我正在尝试加载具有多个命名空间声明的XML文档我的php是:

<?php 
$doc = new DOMDocument('1.0','UTF-8'); 
$doc->load( 'UBLCatalog.xml' ); 
$Items = $doc->getElementsByTagNameNS( "UBLCommonAggregateComponents","Item" ); 
foreach( $Items as $Item ) 
{ 
 $descriptions = $Item->getElementsByTagNameNS( "UBLCommonBasicComponents","Description" ); 
 $description = $descriptions->item(0)->nodeValue;  
 echo "<b>$description'n</b><br>"; 
 } 
?> 

错误是:

xmlns: URI UBLCatalogDocument 不是绝对的 file:///C:/wamp/www/XMLExperiments/UBLCatalog.xml,

我正在获得输出,但错误很烦人。

逐字错误是: 注意: DOMDocument::load() [domdocument.load]: xmlns: URI UBLCatalogDocument 在 file:///C:/wamp/www/XMLExperiments/UBLCatalog.xml 中不是绝对的,第 4 行在 C:''wamp''www''XMLExperiments''ItemsXml.php 在第 3 行

而且,如果我删除默认命名空间(xmlns="UBLCatalogDocument"),错误就会消失

PHP XML

扩展(DOM、XMLReader、SimpleXml 等)使用 libxml 库来解析 XML 文件。

此错误的原因是libxml期望绝对URL xmlns属性(即 xmlns="http://example.com/some-unique-url" ),但仅获取文本 ( xmlns="UBLCatalogDocument" )。

从 libxml 网页:

命名空间值必须是绝对 URL,但 URL 不是 必须指向 Web 上的任何现有资源。

因此,如果可能,请将xmlns属性更改为某个绝对网址。

使用 XPath 查找节点可能会消除错误:

$xpath = new DOMXpath($doc);
$Items = $xpath->query('//item[@xmlns="UBLCommonAggregateComponents"]');