2016-04-26 70 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先生。非常感謝你。簡單地給你。 –