Search code examples
sparqlsemantic-webdbpediavirtuosolinked-data

EasyRDF - SPARQL querying DBpedia - "Undefined namespace prefix" error


It's my first question. I am making a simple program to query DBpedia. I use PHP API + EasyRdf RDF Library for PHP.

The SPARQL query is correct; it's working fine on http://dbpedia.org/snorql. I can use query for API example; it is also correct. I have prefix with dbo, foaf, rdfs...

But when I use this query with this condidion ?person dbo:birthPlace :Berlin ., I have this error:

Fatal error:

Uncaught exception 'EasyRdf_Exception' with message 'HTTP request for SPARQL query failed:

Virtuoso 37000 Error SP030: SPARQL compiler, line 4: Undefined namespace prefix at '' before '.' SPARQL query: define sql:big-data-const 0 PREFIX foaf: PREFIX rdfs: PREFIX dbo: SELECT ?name ?person WHERE { ?person a dbo:MusicalArtist . ?person dbo:birthPlace :Berlin . ?person foaf:name ?name . ?person rdfs:comment ?description . } ORDER BY ?name' in D:\xampp\htdocs\HelloComposer\lib\EasyRdf\Sparql\Client.php:290 Stack trace: #0 D:\xampp\htdocs\HelloComposer\lib\EasyRdf\Sparql\Client.php(120): EasyRdf_Sparql_Client->request('query', 'SELECT ?name ?p...') #1 D:\xampp\htdocs\dbpedia\index.php(43): EasyRdf_Sparql_Client->query('SELECT ?name ?p...') #2 {main} thrown in D:\xampp\htdocs\HelloComposer\lib\EasyRdf\Sparql\Client.php on line 290

My PHP code --

<?php



require_once('D:\xampp\htdocs\HelloComposer\lib\EasyRdf.php');
require_once ('D:\xampp\htdocs\HelloComposer\lib\html_tag_helpers.php');

//PREFIX

EasyRdf_Namespace::set('category', 'http://dbpedia.org/resource/Category:');
EasyRdf_Namespace::set('dbpedia', 'http://dbpedia.org/resource/');
EasyRdf_Namespace::set('dbo', 'http://dbpedia.org/ontology/');
EasyRdf_Namespace::set('dbp', 'http://dbpedia.org/property/');
EasyRdf_Namespace::set('foaf', 'http://xmlns.com/foaf/0.1');
EasyRdf_Namespace::set('rdfs', 'http://www.w3.org/2000/01/rdf-schema#');




$sparql = new EasyRdf_Sparql_Client('http://dbpedia.org/sparql');
?>

<html>
<head>
  <title>EasyRdf Basic Sparql Example</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>EasyRdf Basic Sparql Example</h1>

<h2>List of artists</h2>
<ul>
<?php
    $result = $sparql->query(
        'SELECT ?person ?name ?description WHERE {'.
        '  ?person a dbo:MusicalArtist .'.
        '  ?person dbo:birthPlace :Berlin .'.
        '  ?person foaf:name ?name .'.
        '  ?person rdfs:comment ?description . '.
        '  FILTER (LANG(?description) = "en") .'.
        '} ORDER BY ?name'
    );
    foreach ($result as $row) {
        echo "<li>".link_to($row->name, $row->person)."</li>\n";
    }
?>
</ul>
<p>Total number of artists: <?= $result->numRows() ?></p>

</body>
</html>

Please... help me.


Solution

  • When testing through the DBpedia SNORQL interface, several prefixes are automatically predefined, to make CURIes easier --

    PREFIX      owl: <http://www.w3.org/2002/07/owl#>
    PREFIX      xsd: <http://www.w3.org/2001/XMLSchema#>
    PREFIX     rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX      rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX     foaf: <http://xmlns.com/foaf/0.1/>
    PREFIX       dc: <http://purl.org/dc/elements/1.1/>
    PREFIX         : <http://dbpedia.org/resource/>
    PREFIX dbpedia2: <http://dbpedia.org/property/>
    PREFIX  dbpedia: <http://dbpedia.org/>
    PREFIX     skos: <http://www.w3.org/2004/02/skos/core#>
    

    Confusion may come when you try to use your query through another tool or interface, including but not limited to EasyRDF, as most will not have predefinitions for all these prefixes -- and some may have different expansions of the same prefix string!

    You must ensure that your query includes the same definitions as SNORQL for any and all prefixes you've used from SNORQL's list.

    For this query, you only need three from SNORQL's list (but including all ten won't cause any problems), plus dbo: --

    PREFIX     foaf: <http://xmlns.com/foaf/0.1/>
    PREFIX         : <http://dbpedia.org/resource/>
    PREFIX     rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    
    PREFIX      dbo: <http://dbpedia.org/ontology/>
    

    The declarations in EasyRDF look a bit different, but do the same thing as those above do in SPARQL/SNORQL. First, the minimum necessary for your query --

    EasyRdf_Namespace::set('foaf', 'http://xmlns.com/foaf/0.1/');
    EasyRdf_Namespace::set('', 'http://dbpedia.org/resource/');
    EasyRdf_Namespace::set('rdfs', 'http://www.w3.org/2000/01/rdf-schema#');
    
    EasyRdf_Namespace::set('dbo', 'http://dbpedia.org/ontology/');
    

    -- and second, the full set you need --

    EasyRdf_Namespace::set('owl', 'http://www.w3.org/2002/07/owl#');
    EasyRdf_Namespace::set('xsd', 'http://www.w3.org/2001/XMLSchema#');
    EasyRdf_Namespace::set('rdfs', 'http://www.w3.org/2000/01/rdf-schema#');
    EasyRdf_Namespace::set('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
    EasyRdf_Namespace::set('foaf', 'http://xmlns.com/foaf/0.1/');
    EasyRdf_Namespace::set('dc', 'http://purl.org/dc/elements/1.1/');
    EasyRdf_Namespace::set('', 'http://dbpedia.org/resource/');
    EasyRdf_Namespace::set('dbpedia2', 'http://dbpedia.org/property/');
    EasyRdf_Namespace::set('dbpedia', 'http://dbpedia.org/');
    EasyRdf_Namespace::set('skos', 'http://www.w3.org/2004/02/skos/core#');
    
    EasyRdf_Namespace::set('dbo', 'http://dbpedia.org/ontology/');