Skip to content
This repository has been archived by the owner on Nov 11, 2019. It is now read-only.

Commit

Permalink
add JSON-LD conversion
Browse files Browse the repository at this point in the history
fix #29

Note this just uses the changes @lanthaler said were needed back in
2012, so it should be checked before merging.
  • Loading branch information
chaals committed Jul 20, 2017
1 parent 4e9ae52 commit 42f0364
Showing 1 changed file with 191 additions and 1 deletion.
192 changes: 191 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,192 @@ <h3>Associating names with items</h3>
<section id="converting-html-to-other-formats">
<h2>Converting Microdata to other formats</h2>

<section id="jsonLD">
<h3>JSON-LD</h3>

<p>Given a list of nodes <var>nodes</var> in a <code>Document</code>, a user agent must
run the following algorithm to extract the Microdata from those nodes into a JSON-LD:</p>

<ol>

<li><p>Let <var>result</var> be an empty object.</p></li>

<li><p>Let <var>items</var> be an empty array.</p></li>

<li><p>For each <var>node</var> in <var>nodes</var>, check if the element is a
<a>top-level Microdata item</a>, and if it is then <a>get the object</a> for that element and add it to <var>items</var>.</p></li>

<li><p>Add an entry to <var>result</var> called "<code>items</code>" whose
value is the array <var>items</var>.</p></li>

<li><p>Add an entry to <var>result</var> called "<code>@context</code>" whose value is the following object:</p>
<pre class="html">{ "@vocab" : "" }</pre></li>

<li><p>Return the result of serializing <var>result</var> to JSON. [[JSON]]</p></li>

</ol>

<p class="note">This algorithm returns an object with a single property that is an array, instead
of just returning an array, so that it is possible to extend the algorithm in the future if
necessary.</p>

<p>When the user agent is to <dfn>get the object</dfn> for an item <var>item</var>,
potentially together with a list of elements <var>memory</var>, it must run the following
substeps:</p>

<ol>

<li><p>Let <var>result</var> be an empty object.</p></li>

<li><p>If no <var>memory</var> was passed to the algorithm, let <var>memory</var> be an empty list.</p></li>

<li><p>Add <var>item</var> to <var>memory</var>.</p></li>

<li><p>If the <var>item</var> has any <a>item types</a>, add an entry to <var>result</var>
called "<code>@type</code>" whose value is an array listing the
<a>item types</a> of <var>item</var>, in the order they were specified on the
<code><a>itemtype</a></code> attribute.</p>

</li><li><p>If the <var>item</var> has a <a>global identifier</a>, add an entry to
<var>result</var> called "<code>@id</code>" whose value is the <a>global
identifier</a> of <var>item</var>.</p></li>

<li><p>Let <var>properties</var> be an empty object.</p></li>

<li>

<p>For each element <var>element</var> that has one or more <a>property names</a>
and is one of <a data-lt="item properties">the properties of the item</a>
<var>item</var>, in the order those elements are given by the algorithm that returns
<a data-lt="item properties">the properties of an item</a>, run the following substeps:</p>

<ol>

<li><p>Let <var>value</var> be the
<a>property value</a> of <var>element</var>.</p></li>

<li><p>If <var>value</var> is an <a data-lt="concept item">item</a>, then:
If <var>value</var> is in <var>memory</var>, then let <var>value</var> be
the string "<code>ERROR</code>". Otherwise, <a>get the object</a> for
<var>value</var>, passing a copy of <var>memory</var>, and then replace <var>value</var>
with the object returned from those steps.</p></li>

<li>

<p>For each name <var>name</var> in <var>element</var>'s
<a>property names</a>, run the following substeps:</p>

<ol>

<li><p>If there is no entry named <var>name</var> in <var>result</var>,
then add an entry named <var>name</var> to <var>result</var> whose
value is an empty array.</p></li>

<li><p>Append <var>value</var> to the entry named <var>name</var> in <var>result</var>.</p></li>

</ol>

</li>

</ol>

</li>

<li><p>Return <var>result</var>.</p></li>

</ol>

<div class="example">

<p>For example, take this markup:</p>

<pre class="html">&lt;!DOCTYPE HTML&gt;
&lt;title&gt;My Blog&lt;/title&gt;
&lt;article itemscope itemtype="https://schema.org/BlogPosting"&gt;
&lt;header&gt;
&lt;h1 itemprop="headline"&gt;Progress report&lt;/h1&gt;
&lt;p&gt;&lt;time itemprop="datePublished" datetime="2013-08-29"&gt;today&lt;/time&gt;&lt;/p&gt;
&lt;link itemprop="url" href="?comments=0"&gt;
&lt;/header&gt;
&lt;p&gt;All in all, he's doing well with his swim lessons. The biggest thing was he had trouble
putting his head in, but we got it down.&lt;/p&gt;
&lt;section&gt;
&lt;h1&gt;Comments&lt;/h1&gt;
&lt;article itemprop="comment" itemscope itemtype="https://schema.org/Comment" id="c1"&gt;
&lt;link itemprop="url" href="#c1"&gt;
&lt;footer&gt;
&lt;p&gt;Posted by: &lt;span itemprop="creator" itemscope itemtype="https://schema.org/Person"&gt;
&lt;span itemprop="name"&gt;Greg&lt;/span&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;time itemprop="dateCreated" datetime="2013-08-29"&gt;15 minutes ago&lt;/time&gt;&lt;/p&gt;
&lt;/footer&gt;
&lt;p&gt;Ha!&lt;/p&gt;
&lt;/article&gt;
&lt;article itemprop="comment" itemscope itemtype="https://schema.org/Comment" id="c2"&gt;
&lt;link itemprop="url" href="#c2"&gt;
&lt;footer&gt;
&lt;p&gt;Posted by: &lt;span itemprop="creator" itemscope itemtype="https://schema.org/Person"&gt;
&lt;span itemprop="name"&gt;Charlotte&lt;/span&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;time itemprop="dateCreated" datetime="2013-08-29"&gt;5 minutes ago&lt;/time&gt;&lt;/p&gt;
&lt;/footer&gt;
&lt;p&gt;When you say "we got it down"...&lt;/p&gt;
&lt;/article&gt;
&lt;/section&gt;
&lt;/article&gt;</pre>

<p>It would be turned into the following JSON by the algorithm above (supposing that the page's
URL was <code>http://blog.example.com/progress-report</code>):</p>

<pre>{
"items": [
{
"type": [ "https://schema.org/BlogPosting" ],
"properties": {
"headline": [ "Progress report" ],
"datePublished": [ "2013-08-29" ],
"url": [ "http://blog.example.com/progress-report?comments=0" ],
"comment": [
{
"type": [ "https://schema.org/Comment" ],
"properties": {
"url": [ "http://blog.example.com/progress-report#c1" ],
"creator": [
{
"type": [ "https://schema.org/Person" ],
"properties": {
"name": [ "Greg" ]
}
}
],
"dateCreated": [ "2013-08-29" ]
}
},
{
"type": [ "https://schema.org/Comment" ],
"properties": {
"url": [ "http://blog.example.com/progress-report#c2" ],
"creator": [
{
"type": [ "https://schema.org/Person" ],
"properties": {
"name": [ "Charlotte" ]
}
}
],
"dateCreated": [ "2013-08-29" ]
}
}
]
}
}
]
}</pre>

</div>
</section>


<section id="json">
<h3>JSON</h3>

Expand Down Expand Up @@ -1653,6 +1839,7 @@ <h2>Changes</h2>
and the <a href="https://www.w3.org/TR/2017/WD-microdata-20170626/">second Working Draft</a>:</p>

<ul>
<li>Add a section describing how to convert <a href="#jsonLD">microdata to JSON-LD</a>.</li>
<li><a>itemid</a> may be specified on an element with an <a>itemscope</a> attribute, not just a <a>typed item</a></li>
</ul>

Expand Down Expand Up @@ -1699,14 +1886,17 @@ <h2>Acknowledgements</h2>
Christine Runnegar,
Gregg Kellogg,
Ivan Herman,
Jeni Tennison,
Jens Oliver Meiert,
Léonie Watson,
Manu Sporny,
Marcos Caceres,
Marcos Cáceres,
Markus Lanthaler,
Nick Doty,
Philippe Le Hégaret,
Ralph Swick,
Robin Berjon,
Shane McCarron,
Tab Atkins,
Tavis Tucker,
Tobie Langel,
Expand Down

0 comments on commit 42f0364

Please sign in to comment.