Home > Software engineering >  Warning: SimpleXMLElement::__construct(): namespace error : Namespace prefix xhtml on link is not de
Warning: SimpleXMLElement::__construct(): namespace error : Namespace prefix xhtml on link is not de

Time:07-28

I am trying to get sitemap data, gzdecode it and create SimpleXMLElement. Got the error "Warning: SimpleXMLElement::__construct(): namespace error : Namespace prefix xhtml on link is not defined in".

PHP:

$urlget=  $curl->getPage($url);    
$gz= gzdecode($urlget);    
$d2= new SimpleXMLElement($gz);

Blueprint of sitemap:

<?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
    <loc></loc>
    <xhtml:link rel="alternate" hreflang="es-ad" href="" />
</url>
<url>
    <loc></loc>
    <image:image> 
    <image:loc></image:loc>
    <image:title></image:title>
    <image:caption></image:caption>
    </image:image>
</url>

Thanks in advice.

CodePudding user response:

The message is telling you that your XML file is incorrectly formatted: there is no definition of what namespace the prefix "xhtml:" refers to.

There should be an attribute somewhere like xmlns:xhtml="...", similar to the one that is there for xmlns:image="..." which defines the "image:" prefix.

Without that definition, pretty much any parser will reject the document as invalid XML. If possible, you should raise this with whoever is generating the document, and get them to fix it; if not, you'll have to write some hacky code to manipulate it.

  • Related