hiccup-rdfa: semantic markup for Clojure web apps

RDFa is one of the best specifications ever recommended by the W3C. It offers a pragmatic approach to an incredible powerful but intimidating technology like RDF, and it makes it allowing people to build on their experience with previous technologies like microformats.

Last week, I have built hiccup-rdfaa very small Clojure library that makes easy to use RDFa in hiccup templates rendered by a Ring Clojure application, for example, a Compojure web app.

This application just exposes a URL showing a listing of people with some links. It uses hiccup to build the markup for the application. The markup used try to be an example of semantic markup. It exposes information about the semantics of the data rather than the visual rendering of the page.

Can we provide a better semantic markup for this trivial sample web app? We could think about adding some kind of microformat, but instead of that, I will show how to use RDFa to accomplish this task. Furthermore, using RDFa will open some interesting possibilities for our sample web application.

The first thing to do is to look for a vocabulary suitable for the description of the semantics of our data. A very good candidate is Friend of a Friend (FOAF) that includes terms for describing people and their relationships. After choosing the vocabulary, we will have to embed this vocabulary in the HTML of our application.

The following example shows how we can modify the previous web application to embed the FOAF semantics in the markup:

The rdfa-geeks-template is a variation of the geeks-template function using RDFa. Hiccup-rdfa needs first a description of a vocabulary, it includes already a FOAF vocabulary, so we can use it directly calling to the register-vocabulary function.

After calling to register-vocabulary, all the classes and properties (RDFS and OWL) are transformed into a collection of functions that can be used inside the hiccup template. In this example, as a side effect of the call to register-vocabulary with the FOAF vocabulary description, the functions foaf-Person, foaf-name, foaf-nick and link-foaf-homepage-to used in the template have been generated. HTML attributes can be passed as a parameter to the generated functions as a map. These maps can also include a :tag value with the kind of tag that will be used instead of span that is used by default. Functions link-[property]-to are also generated that can be used to generate links (by default) or anchors to the URL of the resource linked by a RDF property.

The expected output of this template can be seen in a browser:

but we can see how the semantics of the information contained in the page is explicitly stated. Using RDFa is an excellent way of achieving semantic markup.

The use of RDFa has another important implication. Thanks to RDFa a web page can be automatically parsed as data by a HTTP agent supporting RDF, turning a HTML web page into a real “web service”.

We could use a service like the W3C RDFa distiller to feed the source code of the page and extracts the RDF triples embedded into it:

If you find a vocabulary you want to use for the semantic description of your data, you can use it in hiccup-rdfa just importing the vocabulary from the URL with the OWL or RDFS description of the terms. For example, this function call will import the SIOC vocabulary: