Simple Sparql选择在Jena中不起作用

时间:2014-08-24 16:30:15

标签: java rdf sparql 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>

1 个答案:

答案 0 :(得分:3)

查询中的前缀错误。

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。