I am trying to make sitemap for a website but the problem is when i just use three tags it shows no error
<loc>
<changefreq>
<priority>
When i add news:name
tag it shows with red big box. "This page contains the following errors: error on line 4 at column 11: Namespace prefix news on name is not defined Below is a rendering of the page up to the first error."
Any help would be appreciated:
I've done like this
header("Content-Type: application/xml; charset=utf-8");
echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . PHP_EOL;
$base_url = "www.example.com";
while($row = mysqli_fetch_array($sitegrab))
{
echo '<url>' . PHP_EOL;
echo '<news:name>'.$row['Website'].'</news:name>' . PHP_EOL;
echo '<loc>'.$base_url.'</loc>' . PHP_EOL;
echo '<changefreq>daily</changefreq>' . PHP_EOL;
echo '<priority>0.5</priority>' . PHP_EOL;
echo '</url>' . PHP_EOL;
}
echo '</urlset>' . PHP_EOL;
Specify xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
in url set. and solve this issue.
news:
is an namespace prefix/alias. You need a namespace definition for it.
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
defines a default namespace, elements without an prefix in the name can be read as {http://www.sitemaps.org/schemas/sitemap/0.9}urlset
, {http://www.sitemaps.org/schemas/sitemap/0.9}loc
, ...
name
is not an element of the "normal" sitemap namespace, but the the Google News sitemap namespace. You need something like <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">
, with http://www.google.com/schemas/sitemap-news/0.9
being the namespace the name
element belongs to.
Tipp: Make sure to properly escape special characters in your variables while building XML as text.
Tipp: Use XMLWriter API to create the XML.