According to the SPARQL 1.1 Recommendation, variables in SPARQL begin with question marks (or dollar signs), but not percent signs:
A query variable is marked by the use of either "?" or "$"; the "?" or
"$" is not part of the variable name. In a query, $abc and ?abc
identify the same variable. The possible names for variables are given
in the SPARQL grammar.
As a general note about constructing strings that will be parsed as something (in this case, SPARQL queries, but this holds in general), it's good practice to add newlines \n
to them, so that it's easier to recognize where parse errors happen. If you had added newlines to your query, you'd have received an error about line seven,
?entity a owl:Class; rdfs:subClassOf %s; rdfs:label ?categoryname.
which would have made this easier to detect. For instance, if you copy this query:
PREFIX service: <http://www.usa.gov/services/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT ?entity ?categoryname ?servicename ?description WHERE {
{
?entity a owl:Class; rdfs:subClassOf %s; rdfs:label ?categoryname.
OPTIONAL {?entity rdfs:description ?description}.
}
UNION
{
?entity rdf:type %s; service:name ?servicename.
OPTIONAL {?entity service:description ?description}.
}
}
into the validator at sparql.org, you'll get the more useful output:
Input:
1 PREFIX service: <http://www.usa.gov/services/>
2 PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
3 PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
4 PREFIX owl: <http://www.w3.org/2002/07/owl#>
5 SELECT ?entity ?categoryname ?servicename ?description WHERE {
6 {
7 ?entity a owl:Class; rdfs:subClassOf %s; rdfs:label ?categoryname.
8 OPTIONAL {?entity rdfs:description ?description}.
9 }
10 UNION
11 {
12 ?entity rdf:type %s; service:name ?servicename.
13 OPTIONAL {?entity service:description ?description}.
14 }
15 }
Syntax error:
Lexical error at line 7, column 42. Encountered: "%" (37), after : ""
If you were trying to use a value that would be ignored in the query results, simply not specifying it in the select
list of variables is sufficient. If you really don't want to even bind it to a variable in the query, you could use a blank node to simply say ?entity
is an rdfs:subClassOf
something:
?entity a owl:Class; rdfs:subClassOf []; rdfs:label ?categoryname.
If, on the other hand, you planned on replacing %s
with some particular resource, then you should consider using Jena's ParameterizedSparqlString, which have been discussed in