HTML5 Microdata - itemref to another itemscope (Person works for Organization)

送分小仙女□ 提交于 2019-11-28 18:47:53

Well, actually your last code snippet looks fine. Maybe with Yandex Validator the output will be more clear

person
  itemType = http://schema.org/Person
  worksfor
    organization
      itemType = http://schema.org/Organization
      name = Sun Industries
  name = John Doe
  jobtitle = Sales Manager

Couple of other working examples.

<body>
  <div id="organization" itemscope itemtype="http://schema.org/Organization" itemref="employee-1">
    <span itemprop="name">Sun Industries</span>,
    <span itemprop="location" itemscope itemtype="http://schema.org/Place">
      <span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
        <span itemprop="streetAddress">Technologies Street 42</span>,
        <span itemprop="addressLocality">Venustown</span>
        <span itemprop="postalCode">98765</span>
      </span>
    </span>
  </div>
  <div id="employee-1" itemprop="employee" itemscope itemtype="http://schema.org/Person">
    <span itemprop="name">John Doe</span>,
    <span itemprop="jobTitle">Sales Manager</span>
  </div>
</body>

Gives the following:

organization
  itemType = http://schema.org/Organization
  employee
    person
      itemType = http://schema.org/Person
      name = John Doe
      jobtitle = Sales Manager
  name = Sun Industries
  location
    place
      itemType = http://schema.org/Place
      address
        postaladdress
          itemType = http://schema.org/PostalAddress
          streetaddress = Technologies Street 42
          addresslocality = Venustown
          postalcode = 98765

Or this

<body>
  <div id="employee-1" itemscope itemtype="http://schema.org/Person">
    <span itemprop="name">John Doe</span>,
    <span itemprop="jobTitle">Sales Manager</span>
    <meta itemprop="worksFor" itemscope itemtype="http://schema.org/Organization"  itemref="organization">
  </div>
  <div id="organization">
    <span itemprop="name">Sun Industries</span>,
    <span itemprop="location" itemscope itemtype="http://schema.org/Place">
      <span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
        <span itemprop="streetAddress">Technologies Street 42</span>,
        <span itemprop="addressLocality">Venustown</span>
        <span itemprop="postalCode">98765</span>
      </span>
    </span>
  </div>
</body>

That results in

person
  itemType = http://schema.org/Person
  name = John Doe
  jobtitle = Sales Manager
  worksfor
    organization
    itemType = http://schema.org/Organization
    name = Sun Industries
    location
      place      
        itemType = http://schema.org/Place
        address
          postaladdress
            itemType = http://schema.org/PostalAddress
            streetaddress = Technologies Street 42
            addresslocality = Venustown
            postalcode = 98765

Spec is not very clear about using itemref but example helps

<div itemscope id="amanda" itemref="a b"></div>
<p id="a">Name: <span itemprop="name">Amanda</span></p>
<div id="b" itemprop="band" itemscope itemref="c"></div>
<div id="c">
 <p>Band: <span itemprop="name">Jazz Band</span></p>
 <p>Size: <span itemprop="size">12</span> players</p>
</div>
unor

Your last example is correct.
(Google’s testing tool no longer gives the mentioned error. Back then they were probably not up to date with new additions to the Schema.org vocabulary.)

Specification

Links to the itemref specifications:

tl;dr:

  1. You specify itemref on the element (with itemscope) to which you want to add properties.
  2. You specify id on the element (with itemprop) which you want to add.

Examples

A minimal example:

<div itemprop="worksFor" itemscope itemtype="http://schema.org/Organization" id="org">
  <!-- this property (worksFor) gets added to the Person item below -->
</div>

<div itemscope itemtype="http://schema.org/Person" itemref="org">
  <!-- looks for the element with the ID "org" -->
</div>

This is equivalent to:

<div itemscope itemtype="http://schema.org/Person">

  <div itemprop="worksFor" itemscope itemtype="http://schema.org/Organization">
  </div>

</div>

Other examples:

To keep in mind

  • The itemref attribute can only be used for elements in the same document.

  • You can reference multiple elements from one itemref attribute (separate the ID tokens with space characters).

  • The referenced element may be a container for multiple properties.

  • You have to make sure that the referenced elements are not children of an element with itemscope, otherwise their properties will also be added to this item (but you can circumvent this by adding a dummy itemscope).

There are 2 ways to links schema data together.

  1. itemid: Link 2 complete objects (ie Organisation & Person)
  2. itemref: Link 1 complete object to 1 incomplete object (ie Article & Comments)

1st one is easy. All you do is add the itemid property onto item you wish to link and add a link on other item:

<div itemid='#org' itemscope itemType='http://schema.org/Organization'>
    <!-- ..... -->
</div>

<article itemscope itemType='http://schema.org/Article'>
    <link itemprop='publisher' href='#org'>
</article>

Second one is not so easy. What if the comments for your blog post are somewhere far away. How do you connect them to your blog post? You can create an empty item with an id and then connect it to your blog post like so:

<div id="comments" itemscope>
    <span itemprop="commentCount">0</span>
</div>

<div id="words" itemscope>
    <span itemprop="wordCount">0</span>
</div>

We don't need to give comments an itemType. All we need is to add itemscope. This way we get no validation errors. Now we can link the comments back to blog post like so:

<div itemscope itemtype="http://schema.org/BlogPosting" itemref="comments words">
    <!-- .... -->
</div>

Tada! We even managed to import wordCount as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!