I´m trying to query a repository using SPARQL and Sesame 2.7 but when I run my code I get the following error

org.openrdf.http.client.SesameHTTPClient - Server reports problem: org.openrdf.query.parser.sparql.ast.VisitorException: QName 'viagem:nome' uses an undefined prefix

The problem is that, I have the prefix "viagem" under the Namespaces tab for that repository on openrdf-workbench, also when I use the method getNamespaces() it shows up...

The only way I get the query to run is to add the PREFIX manually on every query, but that sounds wrong...

Is there anything that I´m missing on how to use this properly?

--- Edited with more information

Code not working:

String queryString = "SELECT ?name \n" +
"WHERE {?Aeroporto viagem:nome ?name.\n" +
"?Aeroporto rdf:type viagem:Aeroporto}";
        TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
        TupleQueryResult result = tupleQuery.evaluate();
        try {
            List<String> bindingNames = result.getBindingNames();
            while (result.hasNext()) {
                BindingSet bindingSet = result.next();
                Value firstValue = bindingSet.getValue(bindingNames.get(0));
                System.out.println(firstValue);
        }
        } finally {
            result.close();

        }
...

This code work if I change queryString to

 String queryString = "PREFIX viagem:<http://teste.com.br/tut/Viagem.owl#> SELECT ?name \n" +
"WHERE {?Aeroporto viagem:nome ?name.\n" +
"?Aeroporto rdf:type viagem:Aeroporto}";

I was not sure if I should add the PREFIX for every query that I'm going to execute (if that it´s the normal behavior it´s ok...)

Also if I run the following code I get the prefix and the name correctly

RepositoryResult<Namespace> listaNamespace = meuRepositorio.getConnection().getNamespaces();

    while(listaNamespace.hasNext()){
        Namespace atual = listaNamespace.next();
        System.out.println("Name " + atual.getName() + " Prefix " + atual.getPrefix());
    }

the output is:

Name http://www.w3.org/2000/01/rdf-schema# Prefix rdfs
Name http://www.w3.org/2003/11/swrl# Prefix swrl
...
Name http://www.w3.org/1999/02/22-rdf-syntax-ns# Prefix rdf
Name http://teste.com.br/tut/Viagem.owl# Prefix viagem
有帮助吗?

解决方案

Although Sesame stores namespace declarations in the repository, there is no mechanism in place to automatically add these namespaces to a SPARQL query. It is up to you as a user to make sure the SPARQL query is correct and complete.

However, the Workbench application has an advanced SPARQL editor with autocomplete support, which automatically adds namespace declarations when you use a prefix. So you do not have to type them in manually when using Workbench. Note that this is simply a convenience of the client application, not of the actual SPARQL query engine.

Update although, as stated above, Sesame does not read namespace definitions from your Repository when parsing/executing a query, it does allow you to use prefixed names for a limited number of standard vocabularies without explicitly declaring them. These are the 'rdf', 'rdfs', 'owl', 'xsd', 'fn', and 'sesame' prefixes. If you use those in a SPARQL query without declaring them, Sesame's SPARQL engine automatically replaces them with the standard namespace to which those prefixes map (note that it does not use the namespaces in your repository for this, it uses predefined constants).

However, having said all that, it's still good practice as a writer of a SPARQL query to make sure your query is complete. Prefix declarations are an integral part of a SPARQL query, without them your query is simply not syntactically valid, and therefore not portable.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top