仅从SPARQL查询中获取“#”后的名称

时间:2012-10-09 13:29:37

标签: java sparql jena ontology owl

我有这个java代码,使用apache jena api查询比萨本体

    String queryStr =
"prefix pizza: <" + PIZZA_NS + "> "               +
"prefix rdfs: <" + RDFS.getURI() + "> "           +
"prefix owl: <" + OWL.getURI() + "> "             +
"select ?pizza where {?pizza a owl:Class ; "      +
"rdfs:subClassOf ?restriction. "                  +
"?restriction owl:onProperty pizza:hasTopping ;"  +
"owl:someValuesFrom pizza:PeperoniSausageTopping" +
"}";


Query query = QueryFactory.create(queryStr);
QueryExecution qe = QueryExecutionFactory.create(query, model);
ResultSet rs = qe.execSelect();


ArrayList rsList = (ArrayList)ResultSetFormatter.toList(rs);
for(int i=0;i<rsList.size();i++){
    out.println(rsList.get(i).toString());
}

它返回:

( ?pizza = <http://www.co-ode.org/ontologies/pizza/pizza.owl#AmericanHot> )
( ?pizza = <http://www.co-ode.org/ontologies/pizza/pizza.owl#FourSeasons> )
( ?pizza = <http://www.co-ode.org/ontologies/pizza/pizza.owl#American> )

但我只需要

  

AmericanHot

     

FourSeasons

     

美国

如何获得此结果?

2 个答案:

答案 0 :(得分:5)

我以这种方式解决了它。

for ( ; rs.hasNext() ; ){
  QuerySolution soln = rs.nextSolution() ;
  RDFNode x = soln.get("pizza") ;
  out.println(x.asNode().getLocalName());
}

对于谁会有同样的问题,其他信息可以从here

中检索

答案 1 :(得分:3)

SPARQL 1.1函数STRAFTER可以提供帮助:

SELECT ?pizza (strafter(str(?pizza), "#") AS ?localName)
WHERE

但是客户端解决方案同样适用于SPARQL 1.0。