com.hp.hpl.jena.query.QueryParseException:未解析的前缀名称

问题描述 投票:2回答:1

首先,我发现了这个相关的问题和答案:

如果我在SPARQL查询中添加PREFIX,它就可以工作。但是,我不想复制所有SPARQL查询中的所有前缀,而只是定义它们一次。我尝试以编程方式为rdfs:做:

model.setNsPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
query.setPrefix("rdfs", "http://www.w3.org/2000/01/rdf-schema#");

并且它有效,但是如果我尝试用我自己的本体来做它它不起作用:

model.setNsPrefix("myOnt", "http://example.com/ontologies/myOnt#");
query.setPrefix("myOnt", "http://example.com/ontologies/myOnt#");
sparql jena ontology
1个回答
1
投票

您可以设置Prologue以包含前缀映射,然后使用您的特定SPARQL查询将其传递到QueryFactory

使用Apache Jena 3.0.0为我工作的东西:

Prologue queryPrologue = new Prologue();
queryPrologue.setPrefix("skos", "http://www.w3.org/2004/02/skos/core#");

String sparql = "SELECT (COUNT ?s) WHERE { ?s a skos:Concept . }"
Query query = QueryFactory.parse(new Query(queryPrologue), sparql, null, null);

try(QueryExecution queryExec = QueryExecutionFactory.create(query, dataset)) {
    // ...
}
© www.soinside.com 2019 - 2024. All rights reserved.