搜索结果中的微数据站点名称:“元素链接此时不允许使用属性itemprop。”

3

来自Google开发者,我选了这个微数据示例,用于在搜索结果中包含网站名称:

<head itemscope itemtype="http://schema.org/WebSite">
    <title itemprop='name'>Your WebSite Name</title>
    <link rel="canonical" href="https://example.com/" itemprop="url">
</head>

使用W3C验证器时,出现以下错误信息:

错误:在此处不允许在link元素上使用itemprop属性。

正确的标记应该是什么?

1个回答

5
根据WHATWG的HTML标准,link元素不能有relitemprop属性。根据W3C的Microdata(已不再更新的注释),这两个属性可以一起使用。具体详情,请参见我的问题'itemprop' and 'rel' attributes on same element
因此,如果你想符合这两个规范,你必须复制link元素:
<head itemscope itemtype="http://schema.org/WebSite">
    <title itemprop="name">Your WebSite Name</title>
    <link itemprop="url" href="https://example.com/">
    <link rel="canonical" href="https://example.com/">
</head>

好的,Google的示例是错误的吗?Visual Studio 给我一个警告,说<link>需要一个rel属性,但它仍然可以工作。将"canonical"分离到第二个<link>中有意义吗? - roland
@roland:根据一个Microdata规范,Google的示例是错误的,但根据另一个规范则是有效的。-- Visual Studio报告没有'rel'的'link'无效是一个错误:在HTML5+Microdata中,没有'rel'属性是完全可以的。-- 如果你想符合WHATWG版本的Microdata(如果你不关心该版本,你可以继续使用一个'link'),那么有一个单独的'link'是有意义的。 - unor
1
为了明确W3C HTML检查器在处理Microdata方面的行为:它检查HTML Living Standard中的Microdata要求,这是现在唯一规范性断言任何Microdata要求的规范。因此,可以准确地说明旧的W3C Microdata文档不再相关(作为一个注释,它不断言任何规范性要求),因此,如果您使用Microdata并希望符合当前唯一的规范性要求,则必须遵循HTML Living Standard中的规则。 - sideshowbarker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接