- From: Nathan <nrixham@gmail.com>
- Date: Sun, 13 Dec 2009 02:54:47 +0000
- To: Damian Steer <d.steer@bristol.ac.uk>
- CC: Jeni Tennison <jeni@jenitennison.com>, Linked Data community <public-lod@w3.org>
Damian Steer wrote: > On 12 Dec 2009, at 21:42, Jeni Tennison wrote: > >> Hi, >> >> As part of the linked data work the UK government is doing, we're looking at how to use the linked data that we have as the basis of APIs that are readily usable by developers who really don't want to learn about RDF or SPARQL. > >> # SPARQL Results # > > >> { >> "head": { >> "vars": [ "book", "title" ] >> }, >> "results": { >> "bindings": [ >> { >> "book": { >> "type": "uri", >> "value": "http://example.org/book/book6" >> }, >> "title": { >> "type": "literal", >> "value", "Harry Potter and the Half-Blood Prince" >> } >> }, >> { >> "book": { >> "type": "uri", >> "value": "http://example.org/book/book5" >> }, >> "title": { >> "type": "literal", >> "value": "Harry Potter and the Order of the Phoenix" >> } >> }, >> ... >> ] >> } >> } >> >> a normal developer would want to just get: >> >> [{ >> "book": "http://example.org/book/book6", >> "title": "Harry Potter and the Half-Blood Prince" >> },{ >> "book": "http://example.org/book/book5", >> "title": "Harry Potter and the Order of the Phoenix" >> }, >> ... >> ] > with regards sparql query results in json - these can be handled no problem on the client side (10 minutes passes) to illustrate here's a PHP 5 class to do exactly as your example above specified: class SparqlResultsHandler { private static $__instance; const TYPE_URI = 'uri'; const TYPE_BNODE = 'bnode'; const TYPE_LITERAL = 'literal'; const DATATYPE_XML = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral'; const DATATYPE_DATE = 'http://www.w3.org/2001/XMLSchema#dateTime'; const DATATYPE_STRING = 'http://www.w3.org/2001/XMLSchema#string'; const DATATYPE_FLOAT = 'http://www.w3.org/2001/XMLSchema#float'; const DATATYPE_BOOL = 'http://www.w3.org/2001/XMLSchema#boolean'; const DATATYPE_INT = 'http://www.w3.org/2001/XMLSchema#int'; public static function getValue( $value ) { if( !is_object($value) || !isset($value->value) ) return $value; if( $value->type == 'bnode' ) return null; if( !isset($value->datatype) ) return $value->value; switch( $value->datatype ) { case self::DATATYPE_BOOL: return (bool)$value->value; break; case self::DATATYPE_DATE: return strtotime($value->value); break; case self::DATATYPE_FLOAT: return (float)$value->value; break; case self::DATATYPE_INT: return (int)$value->value; break; case self::DATATYPE_STRING: return (string)$value->value; break; case self::DATATYPE_XML: $value = (string)$value->value; $value = str_replace( ' xmlns:xhtml="http://www.w3.org/1999/xhtml"' , '' , $value ); $value = str_replace( 'xhtml:' , '' , $value ); $value = str_replace( '<br></br>' , '<br />' , $value ); $value = str_replace( '></img>' , ' />' , $value ); return $value; break; default: return $value->value; break; } } public static function getBindings( $sparql_results ) { if( !is_object($sparql_results) ) { $sparql_results = json_decode( $sparql_results ); } return $sparql_results->results->bindings; } public static function handleResults( $sparql_results ) { $results = self::getBindings( $sparql_results ); $tiny = array(); foreach( $results as $result ) { $tempResult = new stdClass(); foreach( $result as $name => $param ) { $tempResult->{$name} = self::getValue($param); } $tiny[] = $tempResult; } return $tiny; } } $json = '{ "head": { "link": [], "vars": ["counter"] }, "results": { "distinct": false, "ordered": true, "bindings": [ { "counter": { "type": "typed-literal", "datatype": "http://www.w3.org/2001/XMLSchema#integer", "value": "3021" }} ] } }'; print_r( SparqlResultsHandler::handleResults($json) ); output of the above: Array ( [0] => stdClass Object ( [counter] => 3021 ) ) ... back to text ... I guess what I'm trying to say is, I'm a normal developer and handling sparql results really isn't a problem; RDF is a different matter that can also be handled with just a little bit of client side code and the use of a lib or two, but that's a different matter. many regards, nathan
Received on Sunday, 13 December 2009 10:48:46 UTC