0
votes

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.

2
Have you defined the prefix : somewhere in your query?UninformedUser
In my opinion query is fine. It's working preaty good on dbpedia.org/snorql and simply, "hello world", jena application.Cezary
Thank you for suggestion. In my opinion query is fine. It's working preaty good on dbpedia.org/snorql and simply, "hello world", jena application. It's one off examples query from this, official site: wiki.dbpedia.org/OnlineAccess#1 Querying DBpedia. However, another popular on-line application - SPARQL Query Validator have some errors. I'am working on this. I have one idea to fix this annoying problem.Cezary
It's strange. Query is fine but prefix in dbpedia.org/snorql have problem with "Berlin". At the moment, I am trying to use standard prefix from dbpedia.org/snorql + PREFIX dbo: <dbpedia.org/ontology>. Maybe something is wrong in another prefix. I'm using this application to discover wrong part off code: sparql.org/query-validator.html.Cezary
In your query you have not defined the prefix :UninformedUser

2 Answers

0
votes

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/');
0
votes

Ok. Error is over. When can you use PHP with EasyRDF to querying DBpedia, you should remember about key prefix. It is PREFIX : <http://dbpedia.org/resource/>. You can use this simply query with only one declaration - EasyRdf_Namespace::set('', 'http://dbpedia.org/resource/'). But better solution is to use some additional PREFIX to harder tasks.

With EasyRDF, I suggest this code:

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('skos', 'http://www.w3.org/2004/02/skos/core#');
EasyRdf_Namespace::set('dbo', 'http://dbpedia.org/ontology/');