Search code examples
javasparqljenadbpedia

Querying for Norwegian born people results in undefined namespace prefix


I'm trying to make a basic DBpedia application (just for testing). When I use this query:

SELECT ?person WHERE {
  ?person dbo:birthPlace:Norway ?person
}

I get the following message:

SPARQL compiler, line 6: Undefined namespace prefix at '' before '}'

Could anybody perhaps tell me what I am doing wrong? It works on the DBpedia query tester.

Edit: Java Code from Pastebin

package jena_test;

import java.io.Writer;
import java.sql.ResultSet;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class Dbpedia {

    String test = "123"; 
    static String sparqlQueryString="PREFIX dbo:<http://dbpedia.org/ontology/> "
            + "SELECT ?person "
            + "WHERE { ?person dbo:birthPlace dbo:Norway } ";
    public static void main(String[] args) {
        // TODO Auto-generated method stub





        Model model = ModelFactory.createDefaultModel();
                         Query query = QueryFactory.create(sparqlQueryString);

                         QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", sparqlQueryString);


                         try {
                             com.hp.hpl.jena.query.ResultSet results = qexec.execSelect();
                             for ( ; results.hasNext() ; )
                         {
                             QuerySolution soln = results.nextSolution() ;
                             String x = soln.get("Concept").toString();

                             System.out.print(x +"\n");


                         }

                         }

                         finally { qexec.close() ; }

                         }

    }

Solution

  • I couldn't get your example to work on dbpedia and assume you meant to define the dbo namespace somewhere because it complains about not knowing what dbo is.

    For example

    PREFIX dbo: <http://dbpedia.org/ontology/>
    PREFIX dbpedia: <http://dbpedia.org/resource/>
    SELECT ?person WHERE { ?person dbo:birthPlace dbpedia:Norway }
    

    Which works. But does it return what you want?