xml - 扩展站点地图

标签 xml xsd sitemap

我有一个包含 sitemap.xml 文件的网站。目前,我的 sitemap.xml 文件如下所示:

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

正如我上面的代码片段所示,我正在尝试扩展我的站点地图。我在 extending the sitemaps protocol 中使用 sitemaps.org 中详述的方法部分。

我创建了一个名为 blog.xsd 的 .xsd 文件。该文件位于 http://www.mysite.com/data/blog.xsd .该文件如下所示:

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

我想知道如何在我的站点地图文件中引用 blog.xsd。目前,Google 网站管理员工具会用警告标记我的 sitemap.xml。我的警告说:“无法识别此标签。请修复它并重新提交。”。警告引用标题、描述、作者和 authorUrl 标签。我怀疑这是因为我的 sitemap.xml 文件没有引用 blog.xsd。但是,我不知道该怎么做。有人可以举个例子吗? sitemaps.org 上的文档不是很清楚。谢谢!

最佳答案

我的建议是通读 XML 和 namespace ;它将帮助您更好地理解此类主题。

正如您在 sitemap documentation 中看到的那样您可以在自己的命名空间中使用自己的元素扩展站点地图。您缺少 xml 中的一个关键部分:尽管您使用了 namespace 前缀 blog:在你的元素上,你从未声明过命名空间前缀 blog .

在您看到的站点地图文档中:

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

这是最后一部分, xmlns:example="http://www.example.com/schemas/example_schema" 这很重要。

您需要为博客前缀提供命名空间 uri。它只需要看起来像一个 URL,实际上并不需要存在。让我们使用 http://www.mysite.com/data/blog/1.0 - 您也可以使用其他任何东西。

然后你的站点地图变成:

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

根据站点地图文档,这应该足够了。

如果您还希望能够使用 XML 模式验证器来验证站点地图 XML,您可以更改 <urlset到:

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

关于xml - 扩展站点地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21552535/

相关文章:

c++ - 如何根据 libxml++ 中的 relaxNG 模式验证 xml 文档

xml - 我可以重新定义 XSD 架构以更改某个类型的子项的 maxOccurs 属性吗?

当模式来自类路径时,Java XML 验证不起作用

seo - 如何创建 google xml 站点地图和 robots.txt 文件?

xml - 为 YouTube 嵌入视频创建视频站点地图

java - 如何修改 mvn.cmd 以在 MANIFEST.MF 文件中获取正确的 jdk 构建版本

php - 无法使用 PHP Simplexml 添加带有命名空间前缀的属性

xml - XSD 子元素可选/必需,基于父属性名称

xsd - 如何记录 XML 文件的结构

mysql - 如何使用 Sphinx 搜索检索所有文档 ID