May 12, 2006
Applying XSLT to SPARQL Results
Using the XSLT service its possible to apply a transformation to any XML document, including the results of SPARQL queries.
The Jena RDF/XML-ABBREV output which can be used to serialize the results of CONSTRUCT and DESCRIBE queries can be processed with XSLT as its more regular than the default RDF/XML format. For example Masahide Kanzaki has as stylesheet that can be used to transform RDF calendar data to an HTML calendar view.
However its the SPARQL results format which is most amenable to post-processing with XSLT. It's a very regular and simple format that can be easily transformed into many different vocabularies.
As a convenience feature I've updated the SPARQL query service to accept an extra xslt-uri parameter. The parameter should indicate an XSLT stylesheet that will automatically be applied to the results of any SELECT query. Only this response format is supported at present, although I'll expand it to cover the other options when I get chance.
Any non-SPARQL protocol parameters present in the query string, i.e. anything not listed in the documentation, will be automatically made available to the XSLT stylesheet engine (Saxon 8.6). This means the stylesheet can be parameterised to take in additional data.
To set the mime type for the response, ensuring that you use the xsl:output element in the stylesheet, and configure an appropriate media-type attribute.
Morten Frederiksen has previously demonstrated how to convert SPARQL results to RSS. I've taken his stylesheet and made a few tweaks (bringing it up to date, renaming a few parameters) and made it available on my server. So using this stylesheet you can transform the results of any SPARQL query to RSS 1.0.
There are a couple of rules to follow:
- Ensure there are variables called
rsstitleandrsslinkin your response. These will form therss:titleandrss:linktags of each item. Note: rsslink MUST be bound to a URI in the query. - Provide a
titleparameter in the URL to set therss:titleof the feed - The
channelanddescriptionparameters are similarly mapped to the their respective top-level channel elements
Here's an example of a SPARQL query that provides results in the correct format. It selects the 10 most recently tagged items from 2 users of del.icio.us. Here's the results in HTML. And here's the results with the stylesheet applied.
The nice aspect to this approach is that you can serialize any result set that conforms to the expectations of the stylesheet. "Shape" your query to match expectations and you can do all sorts of interesting transformations.
