Html 最佳 JSON-LD 实践:使用多个 <script> 元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30723531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best JSON-LD practices: using multiple <script> elements?
提问by Stu Furlong
I'm curious about the best practice for applying JSON-LD onto a site for schema.org.
我很好奇将 JSON-LD 应用到 schema.org 站点的最佳实践。
If I have a page with an Article
and I also want to define WebSite
on my page, I would have this:
如果我有一个带有 的页面Article
并且我也想WebSite
在我的页面上定义,我会有这个:
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebSite",
"url": "http://www.example.com/",
"potentialAction": {
"@type": "SearchAction",
"target": "http://www.example.com/search?&q={query}",
"query-input": "required"
}
}
</script>
<!- … -->
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Article",
"author": "John Doe",
"interactionCount": [
"UserTweets:1203",
"UserComments:78"
],
"name": "How to Tie a Reef Knot"
}
</script>
Is this correct or wrong? Is there any benefit or need to merge these into the same script or array of items?
这是正确的还是错误的?是否有任何好处或需要将它们合并到相同的脚本或项目数组中?
采纳答案by unor
It's valid. You can have as many data blocks (= script
elements) as you wish.
这是有效的。您可以根据需要拥有任意数量的数据块(=script
元素)。
A possible benefit of using only one script
element: it allows to make relationships between multiple items easier (e.g., should you decide to use hasPart
or mainEntity
), as you simply have to nest the items.
But making these relationships is of course also possible when using separate data blocks, by referencing the URI of the item with @id
(thanks, @ Gregg Kellogg).
只使用一个script
元素的一个可能的好处:它允许使多个项目之间的关系更容易(例如,如果您决定使用hasPart
或mainEntity
),因为您只需嵌套这些项目。
但是,在使用单独的数据块时,当然也可以通过使用@id
(谢谢,@ Gregg Kellogg)引用项目的 URI 来建立这些关系。
(For reference, adding two or more top-level items in a single script
is possible with @graph
.)
(作为参考,在一个单一的添加两种以上顶级项目script
是可能的@graph
。)
回答by teaforchris
There is no benefit in having single or multiple data blocks, other than limitations around how you might store and manage schema data in your website.
除了限制您在网站中存储和管理架构数据的方式之外,拥有单个或多个数据块没有任何好处。
For example, you might need them separate if different components within your website are responsible for generating each data block independently. Alternatively, if your website is able to manage all schemas for one page in one place, it may be simpler to manage a single data block and render this as a single script
element.
例如,如果您网站中的不同组件负责独立生成每个数据块,您可能需要将它们分开。或者,如果您的网站能够在一个地方管理一个页面的所有架构,那么管理单个数据块并将其呈现为单个script
元素可能会更简单。
You can combine these into a single script by listing each schema as an array like this:
您可以通过将每个模式列为一个数组来将它们组合成一个脚本,如下所示:
<script type="application/ld+json">
[
{
"@context": "http://schema.org",
"@type": "WebSite",
"url": "http://www.example.com/",
"potentialAction": {
"@type": "SearchAction",
"target": "http://www.example.com/search?&q={query}",
"query-input": "required"
}
},
{
"@context": "http://schema.org",
"@type": "Article",
"author": "John Doe",
"interactionCount": [
"UserTweets:1203",
"UserComments:78"
],
"name": "How to Tie a Reef Knot"
}
]
</script>