Extending a Sitemap

左心房为你撑大大i 提交于 2019-12-10 10:37:33

问题


I have a website that contains a sitemap.xml file. Currently, my sitemap.xml file looks like the following:

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

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <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>

As my snippet above shows, I'm trying to extend my sitemap. I'm using the approach detailed at sitemaps.org in the extending the sitemaps protocol section.

I've created an .xsd file called blog.xsd. That file is located at http://www.mysite.com/data/blog.xsd. That file looks like the following:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="title">
    <xs:restriction base="xs:string" />
  </xs:simpleType>

  <xs:simpleType name="description">
    <xs:restriction base="xs:string" />
  </xs:simpleType>

  <xs:simpleType name="author">
    <xs:restriction base="xs:string" />
  </xs:simpleType>

  <xs:simpleType name="authorUrl">
    <xs:restriction base="xs:string" />
  </xs:simpleType>  
</xs:schema>

I am trying to figure out how to reference blog.xsd in my sitemap file. Currently, the Google webmaster tool flags my sitemap.xml with warnings. My warning says: "This tag was not recognized. Please fix it and resubmit.". The warning is in reference to the title, description, author, and authorUrl tags. I suspect this is because my sitemap.xml file does not reference blog.xsd. However, I do not know how to do that. Can someone please provide an example? The documentation on sitemaps.org is not very clear. Thank you!


回答1:


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">


来源:https://stackoverflow.com/questions/21552535/extending-a-sitemap

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