微数据与 RDFa
我有一个关于 RDFa 和 Microdata 的简单问题。
我目前的理解是,RDFa 是将 RDF 实现为 HTML,但对于像我这样的新开发人员来说很复杂,Microdata 似乎非常容易且快速实现。
这两种语义格式还有哪些其他优点和缺点?
I have a quick question about RDFa and Microdata.
My current understanding is that RDFa is RDF implemented into HTML but is complicated for new developers like myself, Microdata seems really easy and quick to implement.
What are the other advantages and disadvantages around these two semantic formats ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Microdata 和 RDFa 之间的差异
虽然存在许多(技术上的、较小的)差异,但以下是我认为重要的一些差异(使用我的答案以网站管理员为基础)。
规范
作为 W3C 的 HTML WG 未找到志愿者 编辑 Microdata 规范,它是 现在只是W3C 小组说明(请参阅历史),这意味着没有计划对其进行任何进一步的工作。
因此 WHATWG 的“HTML Living Standard”中的微数据部分是唯一的微数据可能发展的地方。根据更改的内容,它们的微数据可能会与 W3C 的 HTML5 不兼容。
更新:2017 年,工作再次开始,目标是将微数据发布为 W3C 建议。
RDFa 发布为W3C 建议。< /p>
适用性
微数据只能在 (X)HTML5(分别为 WHATWG 定义的 HTML)中使用。
RDFa 可用于各种宿主语言,即多种 (X)HTML 变体和 XML(因此也可用于 SVG、MathML、Atom 等)。
并且可以支持新的主机语言,如 RDFa Core “是用任何标记语言表达结构化数据的属性规范”。
使用多个词汇表
在 Microdata 中,对同一内容使用多个词汇表更加困难,有时甚至是不可能。
由于使用了前缀,RDFa 允许混合词汇表。
使用反向属性
Microdata 不提供使用反向属性的方法。对于未定义逆属性的词汇表(例如,它们仅定义
parent
而不是parent
和child
),您需要这个。流行的 Schema.org 就是这样一个词汇表(只有一些较旧的例外)。虽然 W3C 注释Microdata to RDF定义了实验性的
itemprop-reverse
,此属性不属于 W3C 或 WHATWG 的一部分Microdata。RDFa 支持使用反向属性(使用
rev
属性)。语义网
通过使用微数据,您并没有直接参与语义网(并且据我所知微数据并不打算这样做),主要是因为它没有定义为 RDF 序列化(尽管有一些方法从 Microdata 中提取 RDF)。
RDFa 是一个 RDF 序列化,而 RDF 是 W3C 语义网。
规范RDFa Core 和HTML+RDFa 可能比 HTML 微数据,但它是这不是一个“公平”的比较,因为它们提供了更多功能。
与 Microdata 类似的是 RDFa Lite (它“确实可以满足大多数日常需求”),而且这个规范,至少在我看来,比 Microdata 简单得多。
该怎么办?
如果您想支持特定的使用者(例如,搜索引擎和浏览器插件),您应该检查他们有关支持语法的文档。
如果您只想学习一种语法并且没有特定的消费者,(注意,主观意见!)请选择 RDFa。 为什么?
无法决定?两者都用。
请注意,您还可以对同一内容使用多种语法,这样您就可以拥有微数据和 RDFa(和微格式、和 JSON-LD、和...)以实现最大兼容性。
这是一个简单的微数据片段:
这是使用 RDFa(Lite)的相同片段:
这里两种语法一起使用:
但通常没有必要/建议走这条路。
Differences between Microdata and RDFa
While there are many (technical, smaller) differences, here’s a selection of those I consider important (used my answer on Webmasters as a base).
Specifications
As W3C’s HTML WG found no volunteer to edit the Microdata specification, it is now merely a W3C Group Note (see history), which means that there are no plans for any further work on it.
So the Microdata section in WHATWG’s "HTML Living Standard" is the only place where Microdata may evolve. Depending on what gets changed, it may happen that their Microdata becomes incompatible to W3C’s HTML5.
Update: In 2017, work started again, with the aim to publish Microdata as W3C Recommendation.
RDFa is published as W3C Recommendation.
Applicability
Microdata can only be used in (X)HTML5 (resp. HTML as defined by the WHATWG).
RDFa can be used in various host languages, i.e. several (X)HTML variants and XML (thus also in SVG, MathML, Atom etc.).
And new host languages can be supported, as RDFa Core "is a specification for attributes to express structured data in any markup language".
Use of multiple vocabularies
In Microdata, it’s harder, and sometimes impossible, to use several vocabularies for the same content.
Thanks to its use of prefixes, RDFa allows to mix vocabularies.
Use of reverse properties
Microdata doesn’t provide a way to use reverse properties. You need this for vocabularies that don’t define inverse properties (e.g., they only define
parent
instead ofparent
&child
). The popular Schema.org is such a vocabulary (with only a few older exceptions).While the W3C Note Microdata to RDF defines the experimental
itemprop-reverse
, this attribute is not part of W3C’s nor WHATWG’s Microdata.RDFa supports the use of reverse properties (with the
rev
attribute).Semantic Web
By using Microdata, you are not directly playing part in the Semantic Web (and AFAIK Microdata doesn’t intend to), mostly because it’s not defined as RDF serialization (although there are ways to extract RDF from Microdata).
RDFa is an RDF serialization, and RDF is the foundation of W3C’s Semantic Web.
The specifications RDFa Core and HTML+RDFa may be more complex than HTML Microdata, but it’s not a "fair" comparison because they offer more features.
Similar to Microdata would be RDFa Lite (which "does work for most day-to-day needs"), and this spec, at least in my opinion, is way less complex than Microdata.
What to do?
If you want to support specific consumers (for example, a search engine and a browser add-on), you should check their documentation about supported syntaxes.
If you want to learn only one syntax and have no specific consumers in mind, (attention, subjective opinion!) go with RDFa. Why?
Can’t decide? Use both.
Note that you can also use several syntaxes for the same content, so you could have Microdata and RDFa (and Microformats, and JSON-LD, and …) for maximum compatibility.
Here’s a simple Microdata snippet:
Here’s the same snippet using RDFa (Lite):
And here both syntaxes are used together:
But it’s typically not necessary/recommended to go down this route.
从任何语义格式中获得的主要优势是消费者能够重用您的数据。
例如,像 Google 这样的搜索引擎是重复使用您的数据来显示丰富摘要的消费者,例如:
为了确定哪种形式最好,您需要知道您想要定位哪些消费者。例如,Google 在常见问题解答中表示他们只会处理微数据(尽管测试工具现在可以使用 RDFa,因此他们有可能接受 RDFa)。
除非您知道您的目标消费者只接受 RDFa,否则您最好使用微数据。虽然许多 RDFa 消费服务(例如语义搜索引擎 Sindice)也接受微数据,但微数据消费服务不太可能接受 RDFa。
The main advantage you get from any semantic format is the ability for consumers to reuse your data.
For example, search engines like Google are consumers that reuse your data to display Rich Snippets, such as this one:
In order to decide which format is best, you need to know which consumers you want to target. For example, Google says in their FAQ that they will only process microdata (though the testing tool does now work with RDFa, so it is possible that they accept RDFa).
Unless you know that your target consumer only accepts RDFa, you are probably best going with microdata. While many RDFa-consuming services (such as the semantic search engine Sindice) also accept microdata, microdata-consuming services are less likely to accept RDFa.
我不确定 unor 使用 Microdata 和 RDFa 的建议是否是一个好主意。如果您在他的示例中使用 Google 的结构化数据测试工具(或其他类似工具),它会显示重复的数据,这似乎意味着 Google 机器人会在网页上找到两个名为 John Doe 的人,而不是最初的意图。
因此,我假设对给定项目使用一种语法是一个更好的主意(只要它们描述单独的实体,您仍然应该能够混合语法)。
尽管我很高兴在这一点上被证明是错误的。
I'm not certain if unor's suggestion to use both Microdata and RDFa is a good idea. If you use Google's Structured Data Testing Tool (or other similar tools) on his example it shows duplicate data which seems to imply that the Google bot would pick up two people named John Doe on the webpage instead of one which was the original intention.
I'm assuming therefore that using one syntax for a given item is a better idea (you should still be able to mix syntaxes as long as they describe separate entities).
Though I would be happy to be proven wrong on this.
我想说这很大程度上取决于用例:对于科学用例,RDF 很常见并用于不同方面。
为了丰富网站,现在推荐使用 JSON-LD,例如 Google。
I would say it largely depends on the use case: For Scientific use cases, RDF is common and used in different aspects.
For enriching Websites, JSON-LD is now recommended, for example by Google.