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
SPARQL contains capabilities for querying required and optional graph patterns along with their conjunctions and disjunctions. SPARQL also supports aggregation, subqueries, negation, creating values by expressions, extensible value testing, and constraining queries by source RDF graph.
SPARQL and SQL have very similar UNION and MINUS operators, which respectively add and remove solutions from a solution set. Because the datatypes of an SQL table are assumed to be uniform across all rows, care must be taken to align the datatypes of the SELECT.
The CONSTRUCT query form returns an RDF graph. The graph is built based on a template which is used to generate RDF triples based on the results of matching the graph pattern of the query.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With