我一直在尝试使用sparql查询获取标签或文字值 . 我当前的查询是构造查询 . 它已经非常正确地运作了 . 我正在使用jena模型来读取结果集 .
String queryString = "PREFIX owl: <http://www.w3.org/2002/07/owl#>"
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
+ "PREFIX foaf: <http://xmlns.com/foaf/0.1/>" + "PREFIX dc: <http://purl.org/dc/elements/1.1/>"
+ "PREFIX dbr: <http://dbpedia.org/resource/>" + "PREFIX dbpedia2: <http://dbpedia.org/property/>"
+ "PREFIX dbpedia: <http://dbpedia.org/>" + "PREFIX skos: <http://www.w3.org/2004/02/skos/core#>"
+ "PREFIX dbo: <http://dbpedia.org/ontology/>" +
"CONSTRUCT {<http://dbpedia.org/resource/Karim_Benzema> ?p ?o} where {"
+ "<http://dbpedia.org/resource/Karim_Benzema> ?p ?o BIND(datatype(?o) as ?dt) FILTER(IF(isliteral(?o) && !bound(?dt), langMatches(lang(?o),'en'), true))" +
"OPTIONAL{?o rdfs:label ?label . FILTER(langMatches(lang(?label),'en'))}}";
Query query = QueryFactory.create(queryString);
QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
FileOutputStream oFile;
try {
oFile = new FileOutputStream("output4.ttl", false);
Model result21 = qexec.execConstruct();
result21.write(oFile, "TTL");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
qexec.close();
FileManager.get().addLocatorClassLoader(Main.class.getClassLoader());
//Model model = FileManager.get().loadModel("output4.xml", null, "RDF/XML");
try {
Model model=ModelFactory.createDefaultModel();
model.read(new FileInputStream("output4.ttl"),null,"TTL");
StmtIterator iter = model.listStatements();
try {
while (iter.hasNext()) {
Statement stmt = iter.next();
Resource s = stmt.getSubject();
Resource p = stmt.getPredicate();
RDFNode o = stmt.getObject();
//System.out.println(s.toString());
//System.out.println(p.toString());
System.out.println(o.toString());
listOfString.add(o.toString());
// if(o.isLiteral()){System.out.println(o.asLiteral().getLexicalForm());} //else {System.out.println(o);}
//System.out.println("");
}
} finally {
if (iter != null)
iter.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我正在检索的谓词和对象如下: -
http://dbpedia.org/property/nationalteam Portugal U15 @ en http://dbpedia.org/property/caps 196 ^^ http://www.w3.org/2001/XMLSchema#integer http:// www .w3.org / 1999/02 / 22-rdf-syntax-ns #type http://schema.org/Person http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://dbpedia.org/class/yago/FootballPlayer110101634 http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://dbpedia.org/class/yago/ManchesterUnitedF . C.Players http://www.w3.org/2002/07/owl#sameAs http://sw.opencyc.org/concept/Mx4rpQzh81h_Ed-LXQAhm0kILA http://dbpedia.org/property/youthyears 1997 ^^ http ://www.w3.org/2001/XMLSchema#integer http://dbpedia.org/property/nationalteamUpdate 2015-09-07 ^^ http://www.w3.org/2001/XMLSchema#date http:/ /dbpedia.org/property/nationalteam http://dbpedia.org/resource/Portugal_national_under-17_football_team http://purl.org/voc/vrank#hasRank 5a1e723ab34fab6f86528369689e6f7f http://dbpedia.org/property/name Cristiano Ronaldo @ en http://dbpedia.org/property/width 31.0 ^^ http://dbpedia.org/datatype/perCe NT
如何仅获取标签或值而不是URI?
根据AKSW更新查询:
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX dbo: <http://dbpedia.org/ontology/>
CONSTRUCT {
<http://dbpedia.org/page/Cristiano_Ronaldo> ?p ?o
}
WHERE {
<http://dbpedia.org/page/Cristiano_Ronaldo> ?p ?o BIND(datatype(?o) as ?dt) FILTER(IF(isliteral(?o) && !bound(?dt), langMatches(lang(?o),'en'), true))
OPTIONAL{?o rdfs:label ?label . FILTER(langMatches(lang(?label),'en'))}
}