Schema.org HTML标记:我可以只使用元标记吗?

时间:2017-10-06 16:31:29

标签: html schema.org microdata

所以我在看Schema.org。我是否需要将<html>标记更改为此

<html itemscope itemtype="http://schema.org/Article">

或者我可以只使用<head></head>块中的元标记吗?

<meta itemprop="name" content="The Name or Title Here">
<meta itemprop="description" content="This is the page description">
<meta itemprop="image" content="http://www.example.com/image.jpg">

4 个答案:

答案 0 :(得分:7)

属性需要属于某个项目。您可以使用itemscope属性创建项目(itemtype属性可以为此项目指定类型)。

如果没有itemscope,则您的标记示例无效。

可以仅在head元素内提供微数据,但不建议这样做有两个原因:

  • Microdata旨在用于您现有的标记。虽然在metawith itemref, see an example)中包含某些link / head元素通常很有意义,但大多数内容通常都在body中。如果您只想使用head中的元素,则必须复制大部分内容。但是,如果你想走这条路,你might prefer to use JSON-LD

  • 由于head不允许使用分组元素(如div),因此表达微数据变得复杂。您必须对每个属性使用itemref,并对每个项目滥用style等元素(例如,请参阅the first snippet in this answer)。

您的示例可能如下所示:

<head>
  <style itemscope itemtype="http://schema.org/Article" itemref="p1a p1b p1c"></style>
  <meta id="p1a" itemprop="name" content="The Name or Title Here">
  <meta id="p1b" itemprop="description" content="This is the page description">
  <link id="p1c" itemprop="image" href="http://www.example.com/image.jpg">
</head>

如果您可以在itemscope元素上使用head,那就更好了:

<head itemscope itemtype="http://schema.org/Article">
  <meta itemprop="name" content="The Name or Title Here">
  <meta itemprop="description" content="This is the page description">
  <link itemprop="image" href="http://www.example.com/image.jpg">
</head>

但是,只要您需要多个项目(通常情况就是这样,例如,对于Organization / Person author等等,这不是'再工作了,你需要一个类似于我的第一个片段的解决方案。

请注意,允许在meta元素中使用link / body元素作为微数据。这样可以更轻松,因为div可以使用itemscope元素。因此,即使您复制内容而不是标记现有内容,最好在body中执行此操作:

<div itemscope itemtype="http://schema.org/Article">
  <meta itemprop="name" content="The Name or Title Here">
  <meta itemprop="description" content="This is the page description">
  <link itemprop="image" href="http://www.example.com/image.jpg">
</div>

(我使用meta元素替换了image属性的link元素,因为using meta is invalid for this purpose。)

答案 1 :(得分:1)

关于Microdatawhatwg.org的部分并未明确提及meta - HTML文档的head - 元素中的元素。总之,您需要itemscope才能拥有有效的标记。

如果您查看schema.org处的说明,则会找到有关 WebPages 的此部分:

  

隐式假设每个网页都声明为WebPage类型,因此可以使用有关该网页的各种属性,例如面包屑。如果指定了这些属性,我们建议使用显式声明,但如果在itemscope之外找到它们,则会假定它们与页面有关。

有趣的是:

  

[...]如果在[{1}}之外找到[属性],,则会假定它们与页面有关。

根据这一点,你不需要itemscopeitemscope - 元素。但它更像是一个建议,然后是明确的规范。

您也可以在Google Structured Data Testing Tool中看到此行为。当您运行此代码时:

html

...你会发现它没有捕获任何数据。只要您添加<!DOCTYPE html> <html lang="en"> <head> <meta itemprop="name" content="The Name or Title Here"> <meta itemprop="description" content="This is the page description"> <meta itemprop="image" content="http://www.example.com/image.jpg"> </head> </html> 导致未指定类型而没有任何错误,它就会开始捕获数据:

Screenshot from Google's tool

使用itemscope将失败或至少抛出错误,因为类型itemtype="http://schema.org/Article"的属性太少。

所有不同的类型都可以在scheme.org上的WebPage overview找到。也许一个适合整个页面。

答案 2 :(得分:1)

根据scheme.org给出的例子,对谷歌来说,两者都是强制性的。

要验证您的数据是否正确捕获,您可以使用此工具:

Structured data testing tool

通过使用上述工具,您可以看到如果省略itemtype="http://schema.org/Article",则无法捕获数据。

答案 3 :(得分:0)

您可以安全地使用<html itemscope itemtype="http://schema.org/Article">。这使您可以在<head>元素内定义itemprop的内容。

Google Rich Test Snippets和w3验证器都验证了这种方法,因为我已经对其进行了测试。