我可以在 JSON-LD 中使用变量吗? [英] Can I use variables in JSON-LD?

查看:52
本文介绍了我可以在 JSON-LD 中使用变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 FAQ 页面中实现 Schema.org,但在架构中重写所有问题和答案似乎是重复的.

所以我想知道是否可以在我的 JSON-LD 中调用一个变量或一个类来检索正文中的问题和答案.

类似于下面的代码:

<脚本类型=应用程序/ld+json">{"@context": "https://schema.org","@type": "FAQPage","url": "https://www.example.com",主实体":[{"@type": "问题",姓名":{{问题}},接受的答案":{"@type": "答案",文本":{{answer}}}}]}<身体><h3 class="question">这是第一个问题?</h3><p id="答案">这是对 Q1 的回答.<h3 class="question">这是第 2 个问题吗?</h3><p class="answer">这是 Q2 的答案.</p>

解决方案

不,JSON-LD 不允许这样做.

如果您不想重复您的内容,您可以使用 Microdata 或 RDFa.使用这些语法,您可以向现有 HTML 元素添加属性(详细信息).

使用 RDFa,它可能看起来像:

<link property="schema:url" href="https://www.example.com/"/><article property="schema:mainEntity" typeof="schema:Question"><h3 property="schema:name">这是第一个问题?</h3><div property="schema:acceptedAnswer" typeof="schema:Answer"><p property="schema:text">这是 Q1 的答案.</p>

</文章>

(通过使用 RDFa 的 vocab 属性,例如在 html 元素上,您可以省略 schema: 前缀;通过使用 RDFa 的 prefix 属性,你可以定义一个不同的,例如更短的前缀)

I'm trying to implement Schema.org in a FAQ page, but it seems repetitive to re-write all the questions and answers in the schema.

So I was wondering if I could call a variable or a class in my JSON-LD to retrieve the questions and answers from the body.

Something like the code below:

<head>
<script type="application/ld+json">
    {
      "@context": "https://schema.org",
      "@type": "FAQPage",
      "url": "https://www.example.com",
      "mainEntity": [{
        "@type": "Question",
        "name": {{question}},
        "acceptedAnswer": {
          "@type": "Answer",
          "text": {{answer}}
        }
      }]
      }
    </script>
</head>
<body>
    <h3 class="question">This is question number one?</h3>
    <p id="answer"> This is the answer to Q1.</p>

    <h3 class="question">This is question number 2?</h3>
    <p class="answer">This is the answer to Q2.</p>
</body>

解决方案

No, JSON-LD doesn’t allow this.

If you don’t want to repeat your content, you can use Microdata or RDFa. With these syntaxes, you add attributes to your existing HTML elements (details).

With RDFa, it could look like:

<body typeof="schema:FAQPage">

  <link property="schema:url" href="https://www.example.com/" />

  <article property="schema:mainEntity" typeof="schema:Question">

    <h3 property="schema:name">This is question number one?</h3>

    <div property="schema:acceptedAnswer" typeof="schema:Answer">
      <p property="schema:text">This is the answer to Q1.</p>
    </div>

  </article>

</body>

(by using RDFa’s vocab attribute e.g. on the html element, you could omit the schema: prefix; by using RDFa’s prefix attribute, you could define a different, e.g. shorter, prefix)

这篇关于我可以在 JSON-LD 中使用变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆