Warning: The microdata syntax has changed (e.g.
item="foo" is now
itemscope itemtype="foo") since this blog post was written. Don’t copy the examples.
The three candidates were microformats, microdata and RDFa. We began with plain HTML:
I'm Philip Jägenstedt at
The simple task at hand is to make my name and homepage machine-readable using each of these formats. What follows is a more elaborate version of the reasoning we went through while evaluating the strengths and weaknesses of each alternative.
I'm Philip Jägenstedt at <a href="http://foolip.org/">foolip.org</a>.
Microformats are “a set of simple, open data formats”, i.e. predefined vocabularies under centralized control. In this example I’ve used the hCard microformat. One “feature” of microformats is that it is valid HTML 4.01/XHTML 1.0, which is why the
class attribute is used in novel ways. Although HTML 4.01 mentions that
class may be used “for general purpose processing by user agents” it’s normally only used “as a style sheet selector”, i.e. for CSS. What this means is that we are working in a single global namespace which is already polluted with all the CSS class names ever used.
The only thing that distinguishes microformats from random CSS classes is the tree structure. This structure is quite a limitation though, because it means that you have to find or make a common ancestor element to all of the data in a single hCard. For a data interchange format, it all seems insane and simply too brittle. Emil put it rather bluntly when he tweeted:
Microformats. Pile of shite that just increases our systematic technical debt.
Still, I have great respect for some of the people behind microformats and the down-to-earth philosophy. They openly state that microformats aren’t “infinitely extensible and open-ended” or “a panacea for all taxonomies, ontologies, and other such abstractions”. As microformats was never intended to solve our use case it is no surprise that it really doesn’t.
Certainly anyone can use
class="foo" to mean anything they like without going through the microformats process – such data formats are cleverly called poshformats (Plain Old Semantic HTML). All things considered though, the whole approach seems outdated and I hope it won’t still be around 5 years from now. Microformats has shown the need for HTML-embedded machine-readable data, now let’s find a better solution.
To understand RDFa you first need some understanding of RDF. The RDF model is basically a somewhat roundabout way of describing graphs using subject-predicate-object triples. An example is the best way to illustrate:
This graph represents me, my name, my homepage and the relationships between them. I’m using the FOAF vocabulary because it already has the concepts of “name” and “homepage”. In N3 syntax this corresponds to these two triples:
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<#me> <foaf:name> "Philip Jägenstedt" .
<#me> <foaf:homepage> <http://foolip.org> .
<brackets> is a URI and because URIs tend be long prefixes are used:
<foaf:name> actually means
<http://xmlns.com/foaf/0.1/name>. I’ve used
#me to represent myself, but this should really be resolved to a full URI.
As you can see, the subject is
#me in both statements. The relationships in the graph are the predicates, i.e.
foaf:homepage. The object is either another URI or a string literal. Adding RDF triples equates to adding more nodes and relationships to the graph. This is general enough that you can model almost anything you want with it.
Back to RDFa. The “a” refers to how attributes in XHTML are used to serialize RDF:
I'm Philip Jägenstedt at
The use of XML namespaces here is a bit odd. Prefixes in XML are used on element and attribute names, but here it’s only used in the attribute value. These are actually CURIEs, another URL shortening scheme. Jeni Tennison recently wrote an excellent post about the use of prefixes in RDFa which I encourage everyone to read. I also chatted briefly with Henri Sivonen about the problems with
xmlns and would recommend reading his mails on those issues.
If we return to RDFa syntax for a bit, notice how
rev are used for the exact same purpose (setting the predicate) in different contexts. The intention was probably to mimic existing practices such as rel=”next”, but the net result is just more room for confusion. While I won’t claim that it’s just too hard I certainly think it could have been simpler without loosing much expressive power.
RDFa began in the now discontinued XHTML2 WG and seems strongly rooted in the Semantic Web (now Linked Data) community and that stack of technologies and tools. It was later made into a module for XHTML 1.1, but there is no W3C-sanctioned way of embedding RDFa in plain HTML. Getting into HTML5 would guarantee RDFa’s survival in the web ecosystem, so its proponents approached the WHATWG/HTML WG suggesting that RDFa be included. There was much heated discussion, the drama of which was my sole source of entertainment for weeks at a time. I’ll again refer to Jeni’s summary of the clash of priorities and “fruitless discussion”. I particularly want to emphasize this conclusion:
It’s just not going to happen for HTML5
I don’t hate RDF(a). I can certainly see the appeal of the RDF model after taking the time to understand it. It may just be a very verbose way of describing graphs, but as a data interchange format it seems to do a good job. However, being able to express arbitrary RDF in HTML in a compact way is not an actual use case for most web developers. If it’s possible without added complexity that’s fine, but HTML is not a triplestore.
As a result of gathering use cases and other input from the big RDFa discussion, suddenly one day HTML5 microdata section sprung into existence along with a very long announcement to the WHATWG list from Ian Hickson (our editor). Within 3 hours there was a demo and not long after another. This is it:
I'm Philip Jägenstedt at
This looks very similar to the microformats example, but the new
itemprop attributes are used instead of
class. The model used is nested groups of name-value pairs, where the name-value pairs are given by the elements with
There are some predefined item types (used above), but it’s possible to use either URLs (
http://foolip.org/footype) or reversed DNS identifiers (
org.foolip.footype) to define your own types without any risk of namespace pollution. Note however that there are no prefixes or other URL shortening schemes. I don’t think I’m crazy to suggest that services like bit.ly and tr.im have shown a way out of the “long URL” problem. If microdata gains any traction, I think communities will create vocabularies with clever shorthands like
subject attribute can be used to avoid the “common ancestor” problem we had with microformats by simply referring to the item element by id:
I'm Philip Jägenstedt.
<!-- stuff -->
Microdata is quite straightforward and feels much more native to HTML than RDFa. As Jeni explains, microdata can’t express RDF triples using datatypes or XML literals. I’ll also add that using a blank node as object isn’t possible. Other than that, RDF triples can be expressed by using the
about type to give the subject of the name-value (predicate-object) pair. Here’s my FOAF example from earlier:
Philip Jägenstedt at
It is quite ugly, so if there’s any way to make it simpler I’m sure such suggestions are welcome. In general though, it seems like a better idea to use simple microdata structures and map that against a RDF vocabulary if possible. In fact, the spec already defines how to extract some RDF (and JSON) from microdata so I’m sure it’s not difficult to do.
var props = document.getItems("vcard").properties;
var fn = props.namedItem("fn").content;
var url = props.namedItem("url").content;
alert("Name: " + fn + "; URL: " + url);
Unsurprisingly there are some issues with the API which I’ve sent feedback on and expect to be fixed to my satisfaction eventually, but the basic functionality is sound. I imagine scripts making dynamic pie charts from tables, providing page-specific autocomplete suggestions and making shiny animated SVG visualizations of the RDF graphs hidden in the tag soup…
Google is now offering to do usability testing of the microdata syntax to see if it can be improved, so if you have any suggestions be sure to bring those to the WHATWG now.
The examples I’ve used are overly simplistic and may utterly fail to show the strengths and weaknesses of each syntax. Still, this is my best effort to make sense of the issues at hand and I haven’t intentionally misrepresented any technology or community. I assume that there is much more debate to come before the dust settles on this issue and perhaps I’ll even change my mind after experimenting more with real-world implementation. I leave you with this unambiguous summary of my views:
- Microformats, you’re a
class attribute kludge
- RDFa, HTML is not your triplestore
- Microdata, I like you but you need more review
- Shelley Powers wrote about RDFa and HTML5′s microdata from the perspective of the RDFa/Semantic Web community. It’s quite a different view from mine, so read that before believing my propaganda.
- Following James Graham’s suggestion, I have registered mantic.se for fun reverse DNS identifiers like
se.mantic.banana. Mostly for fun, don’t take it too seriously…
- I misunderstood Jeni’s post about expressing RDF in microdata and have fixed that section to be more accurate.
Disclaimer: this post is the result of excess spare time and not part of my work at Opera Software. I know nothing about Opera’s plans (or lack thereof) for microformats, RDFa or microdata.
Read Full Post »