Rdf 他的查询是不正确的),但我在评论中看到,您稍后为其添加了前缀声明(提示:更新您的问题)

Rdf 他的查询是不正确的),但我在评论中看到,您稍后为其添加了前缀声明(提示:更新您的问题),rdf,sparql,jena,Rdf,Sparql,Jena,因此,您有属性http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#knows在数据库中,但您需要查询http://xmlns.com/foaf/0.1/knows。看到问题了吗?一切正常,您只是查询了错误的属性 更一般地说:简单地从web复制粘贴代码并不是学习如何使用这些工具的最佳方法。我建议你通过耶拿的一个教程,教你更多关于如何和为什么的知识。从长远来看,这将使您的生活更加轻松。您忘记了包含代码和查询。为

因此,您有属性
http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#knows
在数据库中,但您需要查询
http://xmlns.com/foaf/0.1/knows
。看到问题了吗?一切正常,您只是查询了错误的属性




更一般地说:简单地从web复制粘贴代码并不是学习如何使用这些工具的最佳方法。我建议你通过耶拿的一个教程,教你更多关于如何和为什么的知识。从长远来看,这将使您的生活更加轻松。

您忘记了包含代码和查询。为什么还有第二个
前缀
没有指定前缀?我在web上找到了此代码,但它不起作用!第一个前缀声明后可能缺少空格。我不太清楚SPARQL规范。您遇到了什么错误?我已经删除了第二个前缀,但它不起作用。您忘记了包含代码和查询。为什么还有第二个
前缀
,但没有指定前缀?我在web上找到了此代码,但它不起作用!第一个前缀声明后可能缺少空格。我不太清楚SPARQL规范。您遇到了什么错误?我已经删除了第二个前缀,但它不起作用。您忘记了包含代码和查询。为什么还有第二个
前缀
,但没有指定前缀?我在web上找到了此代码,但它不起作用!第一个前缀声明后可能缺少空格。我不太清楚SPARQL规范。您遇到了什么错误?我已经删除了第二个前缀,但它不起作用。您忘记了包含代码和查询。为什么还有第二个
前缀
,但没有指定前缀?我在web上找到了此代码,但它不起作用!第一个前缀声明后可能缺少空格。我不太清楚SPARQL规范。你有什么错误?我已经删除了第二个前缀,但它不起作用。谢谢你的回答,你是对的。我已经用rdf“PREFIX rdf:<?xml version="1.0"?> <rdf:RDF xmlns="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#" xml:base="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73" xmlns:swp2="http://semwebprogramming.org/2009/ont/chp2#" xmlns:untitled-ontology-73="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#" xmlns:protege="http://protege.stanford.edu/plugins/owl/protege#" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:xsp="http://www.owl-ontologies.com/2005/08/07/xsp.owl#" xmlns:ns66675="http://www.w3.org/2004/02/skos/core#" xmlns:cmp="http://ld.thomsonreuters.com/ContentMarketplace/2013-10-15/#" xmlns:ns36343="http://bioportal.bioontology.org/ontologies/umls/" xmlns:Ontology1395931632451="http://www.semanticweb.org/ontologies/2014/2/Ontology1395931632451.owl#" xmlns:ns20477="http://www.w3.org/2000/01/rdf-schema#" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:ns28717="http://purl.bioontology.org/ontology/HL7/" xmlns:ns91299="http://www.w3.org/2002/07/owl#" xmlns:schema="http://schema.org/" xmlns:swrl="http://www.w3.org/2003/11/swrl#" xmlns:owl2xml="http://www.w3.org/2006/12/owl2-xml#" xmlns:dct="http://purl.org/dc/terms/" xmlns:admin="http://webn.net/mvcb/" xmlns:mds="http://ld.thomsonreuters.com/MetadataSystems/2013-10-15/#" xmlns:swrlb="http://www.w3.org/2003/11/swrlb#" xmlns:base="http://ld.thomsonreuters.com/MetadataSystems/2013-10-15/" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <ns91299:Ontology rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73"/> <!-- /////////////////////////////////////////////////////////////////////////////////////// // // Object Properties // /////////////////////////////////////////////////////////////////////////////////////// --> <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#knowns --> <ns91299:ObjectProperty rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#knowns"> <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#ReflexiveProperty"/> <ns20477:range rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/> <ns20477:domain rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/> </ns91299:ObjectProperty> <!-- /////////////////////////////////////////////////////////////////////////////////////// // // Data properties // /////////////////////////////////////////////////////////////////////////////////////// --> <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#name --> <ns91299:DatatypeProperty rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#name"> <ns20477:domain rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/> <ns20477:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/> </ns91299:DatatypeProperty> <!-- /////////////////////////////////////////////////////////////////////////////////////// // // Classes // /////////////////////////////////////////////////////////////////////////////////////// --> <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person --> <ns91299:Class rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"> <ns20477:subClassOf> <ns91299:Restriction> <ns91299:onProperty rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#knowns"/> <ns91299:someValuesFrom rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/> </ns91299:Restriction> </ns20477:subClassOf> </ns91299:Class> <!-- /////////////////////////////////////////////////////////////////////////////////////// // // Individuals // /////////////////////////////////////////////////////////////////////////////////////// --> <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#amina --> <ns91299:NamedIndividual rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#amina"> <rdf:type rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/> <name>amina</name> <knowns rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#george"/> <knowns rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#john"/> </ns91299:NamedIndividual> <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#george --> <ns91299:NamedIndividual rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#george"> <rdf:type rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/> <name>george</name> <knowns rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#amina"/> <knowns rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#nacira"/> </ns91299:NamedIndividual> <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#john --> <ns91299:NamedIndividual rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#john"> <rdf:type rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/> <name>john</name> </ns91299:NamedIndividual> <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#nacira --> <ns91299:NamedIndividual rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#nacira"> <rdf:type rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/> <name>nacira</name> </ns91299:NamedIndividual> </rdf:RDF> <!-- Generated by the OWL API (version 3.4.5-SNAPSHOT) http://owlapi.sourceforge.net -->
package firstProject;

import java.util.Iterator;
//import com.hp.hpl.jena.ontology.OntModel;
//import com.hp.hpl.jena.ontology.OntModelSpec;
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.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.util.FileManager;

public class main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    sparqltest();}

static void sparqltest()
{

    FileManager.get().addLocatorClassLoader(main.class.getClassLoader());
    Model model= FileManager.get().loadModel("C:/Users/acer/workspace/firstProject/src/firstProject/onto1.rdf");

String queryString=
       "PREFIX rdf:<http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#>"+
             "SELECT * WHERE {"+
      "?person foaf:name ?x. "+
          "?person foaf:knows ?person2."+
          "?person2 foaf:name ?y."+
       "FILTER( ?y = \"john\")"+
       "}";
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();
    }}}