【发布时间】:2013-12-05 18:01:03
【问题描述】:
我正在使用 Jena 编写 SPARQL 查询以从作为方法参数接收的 URI 中获取 rdfs:label 属性。该方法只接收如下 URI:http://pt.dbpedia.org/.. 它应该返回 rdfs:label,但它不会返回任何东西。我检查了一下,它没有输入应该迭代结果的while block。我什至用 URI 做了一个测试:<http://pt.dbpedia.org/resource/Brasil>,但是没有用。
可能是什么问题?
public String getLabel(String uri, String label) {
Model model = ModelFactory.createDefaultModel().read( uri );
RDFNode node;
String queryString = "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 distinct ?label WHERE { " +
"?resource owl:sameAs <" + uri + "> ;" +
"rdfs:label ?label ." +
"filter( langMatches(lang(?label),'pt')) }";
Query query = QueryFactory.create(queryString);
QueryExecution qe = QueryExecutionFactory.create(query, model);
ResultSet r = qe.execSelect();
while( r.hasNext() ) {
QuerySolution querySolution = r.next();
node = querySolution.get("label");
label = node.toString();
}
return label;
}
SPARQL 查询是这样的:
SELECT distinct ?label WHERE {
?brasil owl:sameAs <http://pt.dbpedia.org/resource/Brasil> ;
rdfs:label ?label .
filter( langMatches(lang(?label),"pt") )
}
谢谢!
【问题讨论】:
-
我不知道是不是你遇到的问题,(看起来不像),而是使用字符串连接来进行查询,你真的应该考虑一个参数化的 sparql 字符串。这个答案(stackoverflow.com/a/16739846/1281433)有一个例子。
标签: rdf sparql jena semantic-web dbpedia