String queryString2 = "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}. "
+ " } " + "}";
Query query = QueryFactory.create(queryString2);
大家好,
当使用Jena API运行上述查询时,我收到以下错误。有人可以帮我这个。
com.hp.hpl.jena.query.QueryParseException: Lexical error at line 1, column 315. Encountered: "%" (37), after : ""
答案 0 :(得分:3)
根据SPARQL 1.1建议书,SPARQL中的变量以问号(或美元符号)开头,但不是百分号:
4.1.3 Syntax for Query Variables
使用“?”标记查询变量或“$”; “?”要么 “$”不是变量名称的一部分。在查询中,$ abc和?abc 识别相同的变量。给出了变量possible names 在SPARQL grammar。
作为关于构造将被解析为某些东西的字符串的一般说明(在这种情况下,SPARQL查询,但这通常是成立的),最好向它们添加换行符\n
,以便更容易识别解析错误发生的位置。如果您在查询中添加了换行符,则会收到有关第七行的错误
?entity a owl:Class; rdfs:subClassOf %s; rdfs:label ?categoryname.
这会使这更容易检测。例如,如果您复制此查询:
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}.
}
}
进入validator at sparql.org,您将获得更有用的输出:
输入:
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 }
语法错误:
Lexical error at line 7, column 42. Encountered: "%" (37), after : ""
如果您尝试使用在查询结果中将被忽略的值,则只需在select
变量列表中指定它就足够了。如果您真的不想将它绑定到查询中的变量,您可以使用空白节点简单地说?entity
是rdfs:subClassOf
某事:
?entity a owl:Class; rdfs:subClassOf []; rdfs:label ?categoryname.
另一方面,如果您计划用某些特定资源替换%s
,那么您应该考虑使用Jena的ParameterizedSparqlString,这已在