[英]Simple Sparql Select not working in Jena
在以下數據上運行時,以下查詢應該顯示數據中的“ foaf:name”,但不顯示任何內容。 查詢有問題嗎?
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf:<http://www.xmlns.com/foaf/0.1>
select * where {
?person foaf:name ?x .
}
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.util.FileManager;
public class Sparql {
public static void main(String[] args) {
sparqlTest();
}
static void sparqlTest()
{
FileManager.get().addLocatorClassLoader(Sparql.class.getClassLoader());
Model model=FileManager.get().loadModel("C:/dataTest.rdf");
String queryString="PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>"+
"PREFIX foaf:<http://www.xmlns.com/foaf/0.1>"+
"select * where {"+
"?person foaf:name ?x ."+
"}";
Query query=QueryFactory.create(queryString);
QueryExecution qexec=QueryExecutionFactory.create(query,model);
try{
ResultSet results=qexec.execSelect();
while(results.hasNext() )
{
QuerySolution soln=results.nextSolution();
Literal name=soln.getLiteral("x");
System.out.println(name);
}
}
finally{
qexec.close();
}
}
}
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:admin="http://webns.net/mvcb/"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<foaf:PersonalProfileDocument rdf:about="">
<foaf:maker>
<foaf:Person rdf:about="#me">
<foaf:mbox_sha1sum>b01b5835fa8ae7b7582968a7ecacb9b85503a6c9</foaf:mbox_sha1sum>
<foaf:phone rdf:resource="tel:12345"/>
<foaf:givenname>George</foaf:givenname>
<foaf:workInfoHomepage rdf:resource="urn:development"/>
<foaf:title>Dr.</foaf:title>
<foaf:name>George V</foaf:name>
<foaf:homepage rdf:resource="urn:betacoding.net"/>
<foaf:workplaceHomepage rdf:resource="urn:work"/>
<foaf:knows>
<foaf:Person>
<foaf:name>Charlie</foaf:name>
<foaf:mbox_sha1sum>27f94c268f1a1c6004be361f4045d43c3745c0de</foaf:mbox_sha1sum>
</foaf:Person>
</foaf:knows>
<foaf:schoolHomepage rdf:resource="urn: a school"/>
<foaf:family_name>V</foaf:family_name>
<foaf:nick>Jorch</foaf:nick>
</foaf:Person>
</foaf:maker>
<foaf:primaryTopic rdf:resource="#me"/>
<admin:generatorAgent rdf:resource="http://www.ldodds.com/foaf/foaf-a-matic"/>
<admin:errorReportsTo rdf:resource="mailto:leigh@ldodds.com"/>
</foaf:PersonalProfileDocument>
</rdf:RDF>
查詢中的前綴錯誤。 你有
http://www.xmlns.com/foaf/0.1
這意味着當您在查詢中使用foaf:name時,它將擴展為
http://www.xmlns.com/foaf/0.1name
但是,在數據中,foaf前綴為
http://xmlns.com/foaf/0.1/
所以foaf:name是
http://xmlns.com/foaf/0.1/name
也就是說,您需要添加最后一個斜杠,並且需要刪除初始的www
。 因此,您最終會得到如下查詢和結果:
prefix foaf: <http://xmlns.com/foaf/0.1/>
select * where {
?s foaf:name ?o .
}
------------------------------
| s | o |
==============================
| _:b0 | "Charlie" |
| <data.rdf#me> | "George V" |
------------------------------
請注意,數據未指定任何xml:base。 由於文檔在<#me> foaf:name "George V"
為主題使用了相對URI,因此您可能會看到與我在此處的結果中顯示的主題不同的URI作為主題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.