简体   繁体   English

简单的Sparql Select在耶拿不起作用

[英]Simple Sparql Select not working in Jena

The following query, when run on the following data, is supposed to show the "foaf:name" from the data, but it is not showing anything. 在以下数据上运行时,以下查询应该显示数据中的“ foaf:name”,但不显示任何内容。 Is there any problem with the query? 查询有问题吗?

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>

The prefix in your query is wrong. 查询中的前缀错误。 You have 你有

http://www.xmlns.com/foaf/0.1

That means that when you use foaf:name in the query, it expands to 这意味着当您在查询中使用foaf:name时,它将扩展为

http://www.xmlns.com/foaf/0.1name

In the data, however, the foaf prefix is 但是,在数据中,foaf前缀为

http://xmlns.com/foaf/0.1/

so that foaf:name is 所以foaf:name

http://xmlns.com/foaf/0.1/name

That is, you need to add a final slash, and you need to remove the initial www . 也就是说,您需要添加最后一个斜杠,并且需要删除初始的www Thus, you'd end up with a query and results like this: 因此,您最终会得到如下查询和结果:

prefix foaf: <http://xmlns.com/foaf/0.1/>

select * where {
  ?s foaf:name ?o .
}
------------------------------
| s             | o          |
==============================
| _:b0          | "Charlie"  |
| <data.rdf#me> | "George V" |
------------------------------

Note that the data doesn't specify any xml:base. 请注意,数据未指定任何xml:base。 Since the document uses a relative URI for the subject in <#me> foaf:name "George V" , you may see a different URI as the subject than what I've shown in my results here. 由于文档在<#me> foaf:name "George V"为主题使用了相对URI,因此您可能会看到与我在此处的结果中显示的主题不同的URI作为主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM