Simple javascript RDF Parser and query thingy

Javascript/ECMAScript RDF Parser
Latest Version 0.34
25th May 2006
Jim Ley, Jibbering.com

This RDF parser is designed to run in a web-browser or SVG browser, allowing you to process RDF on the client. The parser isn't complete, there's no support for various bits of the spec, and isn't all that fast, especially with large XML/RDF files. I've found it quite useful though for simple querying.

The parser is generally tested and expected to work in Adobe SVG Viewer, Batik's Squiggle, Internet Explorer 5.5+ and Mozilla Family, Opera 8+ and Safari browsers, Ice Softs Ice Browser is probably the only other browser with a large enough API for it to work, but it's never been tested there - don't expect it to work elsewhere..

rdfs:subProperty is supported, if you include triples which detail subProperties then triples with that property will be duplicated in the output list allowing queries to work on either property.

owl:sameAs is also supported, if you include triples which contain owl:sameAs information, then triples are again duplicated to enable querying, if you don't want this, or the subProperty in your code, it's probably wise to remove the two calls to the functions doSubProperties(); and doOwlSameAs();.

RDF datatypes and languages are supported in the parser, but the query engine provides no way of querying against it currently, should be quite simple for anyone who wants to look at the querying parts, they really are pretty simple.

How to use the parser

The RDF Object has a number of methods:

getRDFURL(url,func,followSeeAlso)
Downloads and parses RDF from a url.
url is the url to recieve the RDF from, use the full url, not a relative one, or the base url will be wrong.
func is a javascript function to call when the rdf has been processed.
followSeeAlso is a an optional boolean telling the parser to follow rdf:seeAlso links, browser security will of course have this follow if any of the seeAlso's are to a different domain.
getTriples()
Returns an array of triples
Match(triples,subject,predicate,object)
Return's an array of triples that match the subject/predicate/object pattern.
triples is an array of triples to search, or null to use all of them.
subject is the subject to look for.
predicate is the predicate to look for.
object is the object to look for (generally useless, but if you do have a value, it will return "" if it does not exist.)
getSingleObject(triples,subject,predicate,object)
Return's the value of the object in the collection of triples that matches the subject/predicate/object pattern, or an empty string if not found.
triples is an array of triples to search, or null to use all of them.
subject is the subject to look for.
predicate is the predicate to look for.
object is the object to look for.
getSingleObjectResource(triples,subject,predicate,object)
Return's the value of the object in the collection of triples that matches the subject/predicate/object pattern, or an empty string if not found, but only if it is a Resource, not a Literal.
triples is an array of triples to search, or null to use all of them.
subject is the subject to look for.
predicate is the predicate to look for.
object is the object to look for.

All arrays of NTriples also have a toNTriples() method prototyped on them. Other methods also exist in the file, read the source for these, they are not necessarily current and fully working!

Example Code

The code below queries my foaf data, for my email address, given my name.


 // declare foaf Namespace
 foafNS="http://xmlns.com/foaf/0.1/"

 // Create RDF object
 myRDF=new RDF()

 // Get foaf rdf
 myRDF.getRDFURL('http://jibbering.com/foaf2.rdf',callback)

 function callback() {

  // Return array of triples which have a foaf:name of "Jim Ley"
  name=myRDF.Match(null,null,foafNS+"name","Jim Ley")

  // Get the object which has a subject returned previously
  // and the predicate of foaf:mbox
  mbox=myRDF.getSingleObject(name,name[0].subject,foafNS+"mbox",null)

  // alert the mailbox
  alert(mbox)

 }

For more complicated examples see the source, of RDF description of an image processed into SVG or RDF graph drawn in SVG or Airport Routes from London Stansted on budget carriers from RDF data..

You can download the parser locally and use it freely on your site, distributed under a BSD licence. Please report bugs, and feel free to ask any questions or request improvements, any fixes please let me know!

There's an old basic version with comments, which may make things clearer if you want to understand, or bugfix, or build on the parser, but that code is out of date, so just use it if you want to understand my crappy style.

Known Bugs / Unsupported Things

That's not the only bug, nor should you think that other tests pass, you can use my local Test page to test the RDF tests stored locally.

Bugs fixed...