2016-04-26 124 views
1

我正在使用jena对本体进行查询。我得到的结果,当我运行查询来获取有关分配数据属性的值对象,如下所示Student1如何从sparql结果中提取数据属性值?

"Ashutosh"^^http://www.w3.org/2001/XMLSchema#string is not a URI node 

,我应该怎么做,如果我想结果如下

"Ashutosh" or `Ashutosh` 

这是我的Java码。如果我想要如上所述的答案,我该怎么办?

import com.hp.hpl.jena.query.Query; 
import com.hp.hpl.jena.query.QueryFactory; 
import com.hp.hpl.jena.query.QueryExecution; 
import com.hp.hpl.jena.query.QueryExecutionFactory; 
import com.hp.hpl.jena.query.QuerySolution; 
import com.hp.hpl.jena.query.ResultSet; 
import com.hp.hpl.jena.rdf.model.Model; 
import com.hp.hpl.jena.rdf.model.RDFNode; 
import com.hp.hpl.jena.util.FileManager; 


public class abc { 
static String avg; 
static String sq="Charlie"; 
static String tq="'"; 
static String data; 


public static void main(String[] args) { 

    // TODO Auto-generated method stub 

sparqltest();} 


static void sparqltest() 
{ 

    FileManager.get().addLocatorClassLoader(abc.class.getClassLoader()); 
    Model model= FileManager.get().loadModel("C:/Users/avg/workspacejena32/Jena/bin/ashu.rdf"); 
    //System.out.println("Connected..."); 
    String queryString="prefix avg:<http://www.semanticweb.org/avg/ontologies/2016/3/untitled-ontology-14#>" + 
         "prefix rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + 
         "SELECT * WHERE {" + 
         "avg:Student1 avg:Name ?x . filter isLiteral(?x)" + 
         "}"; 
    //System.out.println("Connected..."); 


    Query query= QueryFactory.create(queryString); 
    QueryExecution qexec=QueryExecutionFactory.create(query, model); 

try { 
    ResultSet rs = qexec.execSelect(); 
    for (; rs.hasNext() ;) 
    { 
    QuerySolution soln = rs.nextSolution() ; 
    RDFNode a = soln.get("x") ; 
    data = a.asNode().getLocalName(); 


} } 


    catch(Exception e) 
{ 
    System.out.println(e.getMessage()); 
} 
} 
} 
+1

现在有什么问题?在没有看到代码的情况下很难提供帮助,但我想你不要在结果集上使用'getLiteral(“x”)'。 – AKSW

+0

这是我的代码给出below.It给输出作为'“Ashutosh”^^ http://www.w3.org/2001/XMLSchema#string不是一个URI节点,但我只希望'Ashutosh'作为答案。 –

回答

2

我想你想做的事可以做这样的事情来取得的成就:

String aS = a.asNode().getLiteral(); 

这会给你的文字中没有报价。像

"Ashutosh"^^http://www.w3.org/2001/rdf-schema#Literal 

如果你想在文字的纯词汇形式要使用这样的:

String aS = a.asNode().getLiteralLexicalForm(); 

这将返回节点的只有文字没有数据类型。 (但是要注意,它只在节点真的是文字时才起作用)

+0

先生的工作,但部分。给我结果'Ashutosh ^^ http:// www.w3.org/2000/01/rdf-schema#文字',我只希望'Ashutosh'结果。 –

+0

我的答案并不遥远。我错误地记住了功能。为了获得纯粹的**词法**表单,你必须使用'a.asNode()。getLiteralLexicalForm();'我会更新我的答案来说明清楚。 – oole

+0

yeahhh cool.Thanks先生。非常感谢你。简单地给你。 –