Error loading xml in php (not absolute)

不问归期 提交于 2019-12-24 06:48:17

问题


I am trying tom load an XML document with multiple namespace declarations My php is:

<?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>"; 
 } 
?> 

The error is:

xmlns: URI UBLCatalogDocument is not absolute in file:///C:/wamp/www/XMLExperiments/UBLCatalog.xml,

I am getting output, but the error is annoying.

The verbatim error is: Notice: DOMDocument::load() [domdocument.load]: xmlns: URI UBLCatalogDocument is not absolute in file:///C:/wamp/www/XMLExperiments/UBLCatalog.xml, line: 4 in C:\wamp\www\XMLExperiments\ItemsXml.php on line 3

And, if I remove the default namespace (xmlns="UBLCatalogDocument") the error goes away


回答1:


PHP XML extenstions (DOM, XMLReader, SimpleXml etc.) use libxml library to parse XML files.

The reason of this error is that libxml expects absolute URL in xmlns atttibute (i.e. xmlns="http://example.com/some-unique-url"), but gets only text (xmlns="UBLCatalogDocument").

From the libxml webpage:

The namespace value has to be an absolute URL, but the URL doesn't have to point to any existing resource on the Web.

So if it's possible, change your xmlns attribute to some absolute URL.




回答2:


Finding your nodes using XPath might get rid of your error:

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


来源:https://stackoverflow.com/questions/10986913/error-loading-xml-in-php-not-absolute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!