Confused Development

I develop software and I often get confused in the process. I usually find the answers after a while, but a month later I can't remember them. So from now on, I will write them down here.

Thursday, October 27, 2011

HTML Output in Puelia/Linked Data API

Puelia is a PHP implementation of the Linked Data API to facilitate publishing of Linked Data from a SPARQL endpoint in the form of a RESTful API. Puelia can give back data representations in various different formats, such as JSON, XML, Turtle or HTML. This is done by implementing so-called "formatter" modules, which are responsible for creating the actual representation. However, while there are a built-in formatters available for a range of formats, there is none available for HTML.

So, how do we get an HTML representation? There are some hints in the Linked Data API spec, but not a complete howto. The answer is, we need to create an instance of the built-in XSLT formatter, tell it to respond to the relevant mimetypes to enable content negotiation (text/html and application/xhtml+xml) and point it to an appropriate XSLT stylesheet (there are a handful available out of the box in Puelia, but we might want to modify them for our needs). We do this in the Puelia configuration file (which uses Turtle syntax) as follows:

<#HTMLFormatter> a api:XsltFormatter ;
  api:name "html" ;
  api:mimeType "text/html" , "application/xhtml+xml" ;
  api:stylesheet "views/xslt-styles/sample.xsl" ;
.

If you're wondering what XML the stylesheet will operate on: it's the XML generated by the built-in XML formatter (api:xmlFormatter). One other thing we have to do is to register our new HTML formatter in a relevant location in the config file. This could either be the API object, or an endpoint object. Below is an example for the API itself, for which we register the built-in JSON formatter and our new HTML formatter:

<#FooAPI> a api:API ;
  rdfs:label "A RESTful API for the Foo Dataset"@en ;
  # ...
  api:formatter api:JsonFormatter, <#HTMLFormatter> ;
  # ...
.

Labels: , ,