This turorial is part of the RAP - Rdf API for PHP documentation.
Tobias Gauss <tobias.gauss@web.de>
Chris Bizer <chris@bizer.de>
January 2006
SPARQL is a query language for RDF, which is currently being standardized by the W3C. This part of the RAP documentation describes RAP's SPARQL engine and its usage. For a detailed description of the SPARQL query language please refer to W3C SPARQL working draft. RAP's SPARQL package allows you to execute SPARQL queries against RDF graphs and RDF datasets which may be stored in memory or in a relational database. We have also included SPARQL as additional query language into RAP's NetAPI RDF server, which allows you to query remote RDF repositories using W3C SPARQL protocol. RAP's SPARQL package requires PHP version 5.1 or higher.
RAP's SPARQL engine supports most language features that are currently defined in W3C working draft. The engine passes the following testcases. There are still some problems with evaluating complex constraints and constraints wich contain type casts, which. we will fix in the next months.
The usage of the SPARQL engine is very similar to the RDQL engine. A sparql query can be executed against an RDF dataset, a MemModel or DbModel. The necessary parts of the SPARQL package are loaded automatically when needed.
The usage is very simple:
|
This line executes the SPARQL query $querystring
against the Dataset or Model $datasetOrModel
. The query returns an array $result
witch contains the matching variable bindings for the SELECT variables. If you want the result to be returned in the SPARQL Query Results XML Format, simply add an additional 'xml'
parameter to the function call:
|
To clearify the usage here is a simple example. We start with including the RAP package and loading a document into a MemModel:
|
Once the model has been created, we can perform our first query which will be: "Find the full name of all employees".
So, we create a variable $querystring
and assign the corresponding query string:
|
To execute the query, we call the method sparqlQuery()
on the MemModel and pass the variable holding the query string as parameter:
|
The result of the query is an two dimension array of variable bindings ($result[]['?varname'] ). The values of these variables are RAP objects (Resource
,
Literal
or BlankNode
) or an empty string if the variable is unbound . The following code loops over the result set and prints out all bindings of the variable ?fullName
.
|
Another, even more convenient way to display the results of a query is to use the writeQueryResultAsHtmlTable()
method of the SPARQL engine. All we have to do is to pass the query result to this method:
|
Which will result in the following output in the browser window:
No. | ?fullName |
1. | Literal: Bill Parker |
2. | Literal: George Simpson |
3. | Literal: Monica Murphy |
If we want the engine to return the query result in SPARQL Query Results XML Format, we have to add 'xml' as second parameter to the function call:
|
Now the variable $result2 contains a string which represents the results in SPARQL Query Results XML Format:
<?xml version="1.0"?> |
The result of an ASK query is a boolean value.
The result of a CONSTRUCT query is a MemModel:
|
A DESCRIBE query also returns a MemModel. Which properties are included into the description can be specified in constants.php:
|
Meaning that descriptions of instances of foaf:Person will contain the person's name, homepage and mailbox.
For more SPARQL query examples please refer to the W3C SPARQL Working Draft.