Extending a Sitemap

余生颓废 提交于 2019-12-05 20:38:24

My tip is to read up on XML and namespaces in general; it will help you understand topics like this better.

As you can see in the sitemap documentation you can extend a sitemap with your own elements in their own namespace. You are missing one crucial part in your xml: although you used the namespace prefix blog: on your elements, you never declared a namespace prefix blog.

In the sitemap documentation you see:

<urlset 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"
     xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
     xmlns:example="http://www.example.com/schemas/example_schema"> <!-- namespace extension -->

It is this last part, xmlns:example="http://www.example.com/schemas/example_schema" that is crucial.

You need to come up with a namespace uri for your blog prefix. It only has to look like a URL, it doesn't actually need to exist. Let's use http://www.mysite.com/data/blog/1.0 - you can use anything else as well.

Then your sitemap becomes:

<?xml version="1.0" encoding="utf-8"?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 
        xmlns:blog="http://www.mysite.com/data/blog/1.0">
  <url>
    <loc>http://www.mysite.com/about/blog/post-1</loc>
    <lastmod>2013-08-13</lastmod>
    <changefreq>never</changefreq>
    <blog:title>This is the title of the blog post</blog:title>
    <blog:description>This is the description of the blog post</blog:description>
    <blog:author>Some person</blog:author>
    <blog:authorUrl>https://www.mysite.com/people/some-person</blog:authorUrl>    
  </url>
</urlset>

That should be enough, according to the sitemap documentation.

If you also want to be able to validate the sitemap XML using an XML Schema validator, you can change the <urlset to:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" 
        xmlns:blog="http://www.mysite.com/data/blog/1.0"
        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
                            http://www.mysite.com/data/blog/1.0 
                            http://www.mysite.com/data/blog.xsd">
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!