XML declaration allowed only at the start of the document

Posted by

Error:

Sitemap can be read, but has errors
Unsupported file format
1 instance
Your Sitemap does not appear to be in a supported format. Please ensure that it meets our Sitemap guidelines and resubmit.

Solution:

Common Issues with XML Sitemaps

  • Incorrect XML Schema: The XML file must adhere to the sitemap protocol as defined by sitemaps.org.Mistakes in XML Tags: Typos or incorrect casing in tags can cause the file to be rejected.
  • Invalid URLs: Ensure all URLs are fully qualified and correctly encoded.
  • Improper Date Format: Dates should be in W3C Datetime format (e.g., 2020-01-01T12:00:00+00:00).

Example of a Valid XML Sitemap Structure

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/hospitals/sitemaps/hospital.xsl"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
 <url>
  <loc>http://mhn-core-ms/hospitals/hospitals/american-samoa</loc>
  <lastmod>2024-02-05T12:40:57+00:00</lastmod>
 </url>
 <url>

Adjustments to my PHP and XSL

protected function generateIndividualCountryFiles($hospitals)
{
    $countries = $hospitals->groupBy('country_name');

    foreach ($countries as $countryName => $hospitalsInCountry) {
        $countrySlug = strtolower(str_replace(' ', '-', $countryName));
        $directoryPath = public_path("hospitals");
        if (!file_exists($directoryPath)) {
            mkdir($directoryPath, 0777, true);
        }
        $filePath = $directoryPath . "/{$countrySlug}.xml";

        $xmlWriter = new XMLWriter();
        $xmlWriter->openUri($filePath);
        $xmlWriter->startDocument('1.0', 'UTF-8');
        $xmlWriter->setIndent(true);
        $xmlWriter->writePI('xml-stylesheet', 'type="text/xsl" href="/hospitals/sitemaps/hospital.xsl"');
        $xmlWriter->startElement('urlset');
        $xmlWriter->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');

        foreach ($hospitalsInCountry as $hospital) {
            $xmlWriter->startElement('url');
            $xmlWriter->writeElement('loc', rtrim(config('app.url'), '/') . "/hospitals/{$countrySlug}");
            $xmlWriter->writeElement('lastmod', $hospital->updated_at->toAtomString());
            $xmlWriter->writeElement('changefreq', 'monthly');
            $xmlWriter->writeElement('priority', '0.8');
            $xmlWriter->endElement();  // end url element
        }

        $xmlWriter->endElement();  // end urlset element
        $xmlWriter->endDocument();
        $xmlWriter->flush();
    }
}

Structure: This updated PHP code ensures each hospital entry includes elements like <loc>, <lastmod>, <changefreq>, and <priority>, all of which are part of the standard sitemap protocol.

Validation: Validate your XML sitemap using an online XML validator or Google Search Console to ensure it is free from syntax and structural errors.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x