Extensions to schema.org to support semantic, composable, parameterize-able and executable documents
This is the Stencila Schema, an extension to schema.org to support semantic, composable, parameterize-able and executable documents. It also provides implementations of schema.org types (and our extensions) for several languages including JSON Schema, Typescript, Python and R. It is a central part of our platform that is used widely throughout our open-source tools as the data model for executable documents.
Schema.org is "a collaborative, community activity with a mission to create, maintain, and promote schemas for structured data on the Internet, on web pages, in email messages, and beyond.". Schema.org is is used by most major search engines to provide richer, more semantic, search results. More and more web sites are using the schema.org vocabulary and there is increasing uptake in the research community e.g. bioschemas.org, codemeta.github.io
The schema.org vocabulary encompasses many varied concepts and topics. Of particular relevance to Stencila are types for research outputs such as
SoftwareSourceCode and their associated meta data e.g.
However, schema.org does not provide types for the content of research articles. This is where our extensions come in. This schema adds types (and some properties to existing types) to be able to represent a complete executable, research article. These extensions types include "static" nodes such as
Figure, and "dynamic" nodes involved in execution such as
An important aspect of schema.org and similar vocabularies are that they really just define a shared way of naming things. They are format agnostic. As schema.org says, it can be used with "many different encodings, including RDFa, Microdata and JSON-LD".
We extend this philosophy to the encoding of executable articles, allowing them to be encoded in several existing document formats. For example, the following very small
Article, containing only one
Paragraph, and with no metadata, can be represented in Markdown:
as a Jupyter Notebook,
or as HTML with Microdata,
This repository does not deal with format conversion per se. Please see Encoda for that. However, when developing our schema.org extensions, we aimed to not reinvent the wheel and maintain consistency and compatibility with existing schemas for representing document content. Those include:
Despite its name, schema.org does not define strong rules around the shape of data, as say a database schema or XML schema does. All the properties of schema.org types are optional, and although they have "expected types", this is not enforced. In addition, properties can be singular values or array, but always have a singular name. For example, a
Article has a
author property which could be undefined, a string, a
Person or an
Organization, or an array of
This flexibility makes a lot of sense for the primary purpose of schema.org: semantic annotation of other content. However, for use as an internal data model, as in Stencila, it can result in a lot of defensive code to check exactly which of these alternatives a property value is. And writing more code than you need to is A Bad Thing™.
Instead, we wanted a schema that placed some restrictions on the shape of executable documents. This has flow on benefits for developer experience such as type inference and checking. To achieve this the Stencila Schema defines schema.org types using JSON Schema. Yes, that's a lot of "schemas", but bear with us...
In Stencila Schema, when we define a type of document node, either a schema.org type, or an extension, we define it,
- as a JSON Schema document, with restrictions on the marginality, type and shape of it's properties
- using schema.org type and property names, pluralized as appropriate to avoid confusion
For example, an
Article is defined to have an optional
authors property (note the
s this time) which is always an array whose items are either a
To keep things simpler, this is a stripped down version of the actual
With a JSON Schema, we are able to:
- use a JSON Schema validator to check that content meets the schema
- generate types (i.e.
classelements) matching the schema in other languages.
JSON can be quite fiddly to write by hand. And JSON Schema lacks a way to easily express parent-child relationships between types. For these reasons, we define types using YAML with custom keywords such as
extends and generate JSON Schema and ultimately bindings for each language from those.
Binding for this schema, in the form of installable packages, are currently generated for:
Depending on the capabilities of the host language, these packages expose type definitions as well as utility functions for constructing valid Stencila Schema nodes. Each packages has its own documentation auto-generated from the code.