Over a million developers have joined DZone.

rdfQuery-Process RDF Data in your browser

DZone's Guide to

rdfQuery-Process RDF Data in your browser

· ·
Free Resource

rdfQuery is a JavaScript library which helps us to process RDF data in the browser itself. It helps in parsing RDFa data embedded in the webpage and is even used to reason the RDF content on the webpage and discover interesting relations. It can be used to create interfaces for editing the semantic web. rdfQuery is available in three flavor :-

  1. Core rdfQuery – Helps one to create a triplestores and query it.
  2. rdfQuery with RDFa- Helps parsing RDFa and add it to your page.
  3. rdfQuery with rules- Helps to reason over the triplestores using rules.

To get started we will cover each of the above. I have divided the entire tutorial into three parts so that one could go across the different features of rdfQuery separately and would be able to create something useful out of it.

Core rdfQuery(Manual Creation of DataBank)

Core rdfQuery provides with methods to create databanks using triples. These triples can be just strings in turtle format. It also allows to add namespace for vocabulary such as foaf  etc. I have also included some custom namespace for my data. In this tutorial we would just build a small application which would make a databank manually using rdfQuery and then query the databank using simple SPARQL and print the data onto a table. So without more delay lets start with it.

Download a copy of rdfQuery Core library along with the latest copy of JQuery. After downloading these library include these into the html page.(You could also use JSFiddle to do this where you can include the rdfQuery library in the resource area.)

The syntax to create a DataBank using rdfQuery is

$.rdf.databank.(array [,options]);

The array above which you pass to the databank function can be a string of RDF data or can be an array of $.rdf.triple objects. I planned to make an RDF of myself along with my friends. So the code goes somewhat like this 

var rdfDataBank= $.rdf.databank([
'fbook:shivkumarganesh a foaf:Person',
'fbook:shivkumarganesh foaf:name "Shiv Kumar Ganesh" .',
'fbook:shivkumarganesh foaf:firstName "Shiv" .',
'fbook:shivkumarganesh foaf:lastName "Ganesh" .',
'fbook:shivkumarganesh foaf:family_name "Ganesh" .',
'fbook:shivkumarganesh foaf:nick "Shivy" .',
'fbook:shivkumarganesh foaf:knows fbook:Priyank',
'fbook:shivkumarganesh foaf:knows fbook:Karuna',
'fbook:Priyank a foaf:Person .',
'fbook:Priyank foaf:name "Priyank Chaube" .',
'fbook:Karuna a foaf:Person .',
'fbook:Karuna foaf:name "Karunakaran K" .'

], {
base: 'http://shivkumarganesh.in/',
namespaces: {
foaf: 'http://xmlns.com/foaf/0.1/',
fbook: 'http://facebook.com/',
rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'

Lets review some major part of the code :-

  • In line 1 var rdfDataBank is an databank object which stores the rdf data and this is the object upon which we would be calling the SPARQL queries.
  • Line 16 refers to the point where the options begin in the $.rdf.databank(array [,options]) .
  • Options refer to the base URI along with and array of namespace that are used in the above RDF description eg: rdf, fbook, foaf.

I have also described about few of my friends in these rdf triples. They just have their name described. This entire thing as mentioned above can be done using $.rdf.triple object too.We can code it as follows :-

$.rdf.triple('subject predicate object .' , {
namespaces:{//The valid namespace used for any of the above subject,predicate,object}

Now since we know how to make a data bank manually. Lets go ahead and Query the data bank object which in this case is rdfDataBank.

var dataBankQuery = $.rdf({
databank: rdfDataBank
}).where('?s ?p ?o');
var dataBankBindings = dataBankQuery.select();
for(var i=0;i{

In the above code, in line no. 1 we declare an object(dataBankQuery) which contains the query results.After this in Line 4 we call select() method on the dataBankQuery this select method helps to return an array that contains objects representing the query bindings as JavaScript objects.So that after this the dataBankBindings variable can be queried for individual data.

We should also note that $.rdf( argument ) also takes in databank as an argument. It does that to refer the databank where the data is populated as in to differentiate the databank in use. I have also tried to traverse the entire object containing the binding to show the rdf triples in a tabular format.

If we want to export the triples we can do that by using a function called dump() called on the databank.

var dataBankJSON=rdfDataBank.dump();

This is how you would make a data bank manually using rdfQuery and query it using SPARQL. In the next tutorial we would cover another method of using rdfQuery where we use to generate data banks from the information on the page itself i.e the RDFa information.

The demo can be found on fiddle 

Image: Courtesy rdfQuery (Google Codes)


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}