SPARQL Calendar Demo: A SPARQL JavaScript Library

| 1 Comment | 2 TrackBacks

This is the fourth in a series of entries about the SPARQL calendar demo. If you haven't already, you can read the previous entry.

A key component of the calendar demo is our SPARQL JavaScript library. Leigh Dodds blogged about his SPARQL AJAX client a few months back. As one of our motivations for the calendar demo was to explore the JSON serialization of SPARQL queries, though, we whipped up our own library for SPARQL queries. This library features:

  • ...issuing SPARQL SELECT or ASK queries using the SPARQL Protocol for RDF extended with a parameter named output. Joseki as deployed on SPARQLer currently supports:
    • No output specified; results are returned in the SPARQL Query Results XML Format with a MIME type of application/sparql-results+xml.
    • output=xml or output=sparql; results are returned in the SPARQL Query Results XML Format with a MIME type of text/plain.
    • output=json; results are returned via the JSON serialization with a MIME type of text/javascript.
    • output=any-other-value; results are returned in RDF/XML with MIME type text/plain as a graph using the DAWG's result-set vocabulary for test cases.
  • ...automatically validating and parsing JSON return values into JavaScript objects.
  • ...providing several query wrapper methods and accompanying result transformations to enable direct access to single-valued query results, vectors of query results, and boolean results (for ASK queries). This mechanism could be easily extended to support parsing the XML result format.
  • ...allowing either HTTP GET or HTTP POST to be used when sending queries.
  • ...providing distinct service and query objects such that dataset graphs, prefixes, and other settings can be set service-wide or on a per-query basis.

The library currently has an unmotivated dependency on the Yahoo! connection manager, but this dependency could (and likely will) be easily removed.

Finally, some example usages of the library's API:

var sparqler = new SPARQL.Service("");

// graphs and prefixes defined here
// are inherited by all future queries
sparqler.setPrefix("foaf", ""); 
sparqler.setPrefix("rdf", "");
// "json" is the default output format

var query = sparqler.createQuery();

// these settings are for this query only

// query wrappers:

// passes standard JSON results object to success callback
query.setPrefix("ldf", "");
  "SELECT ?who ?mbox WHERE { ldf:LDF foaf:knows ?who . ?who foaf:mbox ?mbox }",
  {failure: onFailure, success: function(json) { for (var x in json.head.vars) { ... } ...}}

// passes boolean value to success callback
  "ASK ?person WHERE { ?person foaf:knows [ foaf:name "Dan Connolly" ] }",
  {failure: onFailure, success: function(bool) { if (bool) ... }}

// passes a single vector (array) of values 
// representing a single column of results 
// to success callback
query.setPrefix("ldf", "");
var addresses = query.selectValues(
  "SELECT ?mbox WHERE { _:someone foaf:mbox ?mbox }",
  {failure: onFailure, success: function(values) { for (var i = 0; i < values.length; i++) { ... values[i] ...} } }

// passes a single value representing a single 
// row of a single column (variable) to success callback
query.setPrefix("ldf", "");
var myAddress = query.selectSingleValue(
  "SELECT ?mbox WHERE {ldf:LDF foaf:mbox ?mbox }",
  {failure: onFailure, success: function(value) { alert("value is: " + value); } }
// shortcuts for all of the above 
// (w/o ability to set any query-specific graphs or prefixes)

Feel free to download and use the library as you see fit. I'll post here when there's any substantive updates to it. In the next entry, I'll start delving into the specific SPARQL queries that drive the calendar demo.

2 TrackBacks

I've released some updates to the SPARQL query service. The code is now running on ARQ so should now be conforming to the SPARQL Candidate Recommendation. I've also fixed some bugs and issues with data set creation that was stopping Read More

I'm currently at XTech in Amsterdam and earlier this morning I gave my talk, "SPARQLing Services", an overview of SPARQL, with an emphasis on the SPARQL protocol and how SPARQL can benefit Web 2.0/AJAX applications. The full paper is online at the conf... Read More

1 Comment


I am using BOCA2 and we are in the processing building our Model.
What ever documentation i have gone thru so far it seems SPARQL doesn't support GROUP BY , COUNT() and other arithmetic functions like SQL does. So if i want to get Data from the RDF Repository for an Object and also info reg the Related Objects acorss the named Graphs do i need to query multiple times ?? If this is the case how can we achieve reasonable performace with so many Sequential dependent queries..!!