-->

Right way to set microdata for Q&A HTML

2020-07-10 10:13发布

问题:

Say one has a simple question and answer HTML and would like to add microdata, how should one proceed?

<h2>My Question</h2>
<p>My Answer</p>

I am aware of the schema.org example, but I don't find it very clear. It looks like overkill. I need a simple solution. Can I proceed this way?

<h2 itemscope itemtype="http://schema.org/Question">My Question</h2>
<p itemscope itemtype="http://schema.org/Answer">My Answer</p>

I just want to tell what the question is and what the answer is. Is this enough for search engines? Or should I have something more sophisticated like:

<div itemscope itemtype="http://schema.org/Question">
  <h2 itemprop="name">My Question</h2>
  <p itemscope itemtype="http://schema.org/Answer">My Answer</p>
</div>

Is using itemprop="name" the right way to tell what the question is? What is the difference between itemprop="name" and itemprop="text" in the schema.org example mentioned above?

回答1:

From your first example, Microdata parsers will only learn that there is a Question and an Answer item, without any further content. Microdata doesn’t specify that the content of HTML elements with itemscope attributes has to be considered, it only cares about property values.

Testing your example with some online Microdata parsers:

  • W3C’s Microdata to RDF Distiller extracts this RDF (in Turtle):

    <> md:item ( [ a schema:Question ] [ a schema:Answer ] );
        rdfa:usesVocabulary schema: .
    
  • The Structured Data Linter has almost the same output.

  • Yandex’s Structured data validator has similar output.

  • Google’s Structured Data Testing Tool doesn’t extract anything.


name vs. text

For this very question, the name would be "Right way to set microdata for Q&A HTML" and the text would be the question body ("Say one has a simple question …").

If the whole question consists only of such a single, short line, I’d use the text property instead of name (*). name could, principally, also be something like "Question 1", if you want/need it.

But you could also use both properties for a short question, i.e., itemprop="name text", but this is maybe not very elegant (but it can make sense especially if you know that some data consumer makes use of the name property).

* The example for Answer also uses text (and has no name).


You might also want to use Question’s suggestedAnswer property and/or Answer’s parentItem property to relate these two items.

So for a short question it could look like:

<section itemscope itemtype="http://schema.org/Question">
  <h2 itemprop="name text">My Question</h2>
  <div itemprop="suggestedAnswer" itemscope itemtype="http://schema.org/Answer">
    <p itemprop="text">My Answer</p>
  </div>
</section>