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. 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
http://www.xmlns.com/foaf/0.1name
In the data, however, the foaf prefix is
http://xmlns.com/foaf/0.1/
so that foaf:name is
http://xmlns.com/foaf/0.1/name
That is, you need to add a final slash, and you need to remove the initial 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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.