使用耶拿读取RDF文件时,getLiteral返回null [英] getLiteral returns null when reading RDF file using jena
问题描述
这些天来,我一直在阅读有关OWL本体和RDF文件的信息.我仍然不明白这一点.
I have been reading about OWL ontology and RDF files all these days. I still can't understand this.
假设我使用Protege创建了一个简单的本体.它具有一个称为Review的类,具有两个数据属性,分别是注释和等级.
Let's say I created a simple ontology using Protege. It has single class called Review with two data properties which are comment and rating.
现在,我想创建一个单独的以xml编写的RDF文件,其中包含一些注释.我创建的文件看起来像
Now I want to create a separate RDF file written in xml which has some comments. The file I created look like
<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:c="http://review-analyzer.local/ontologies/reviews_2.owl#">
<rdf:Description rdf:ID="me">
<c:review>Display looks amazing!</c:review>
<c:raitng>5</c:raitng>
</rdf:Description>
<rdf:Description rdf:ID="me2">
<c:review>Display is great!</c:review>
<c:raitng>5</c:raitng>
</rdf:Description>
</rdf:RDF>
现在,我想将此文件读入jena模型,阅读此评论并在我的本体中创建个人.创建单个零件我已经弄清楚了.但是我无法从这些评论中获得评分和评论值.
Now I want to read this file into jena model, read this reviews and create individuals in my ontology. Creating individual part I already have figured out. But I can't get rating and comment values from these reviews.
我尝试过的代码是
Model model = ModelFactory.createOntologyModel();
model.read("./rdf/119.rdf", "RDF/XML");
String queryString =
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\r\n" +
"PREFIX ns: <http://review-analyzer.local/ontologies/reviews_2.owl#>"+
"select *\r\n" +
"where {\r\n" +
" ?Comment ns:review ?review .\r\n" +
" ?Comment ns:raitng ?raitng .\r\n" +
"}";
Query query = QueryFactory.create(queryString);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
try {
ResultSet results = qexec.execSelect();
List<String> varNames = results.getResultVars();
while (results.hasNext()) {
QuerySolution soln = results.nextSolution();
Literal name = soln.getLiteral("review");
System.out.println(soln);
}
} finally {
qexec.close();
}
以下行返回null.
Literal name = soln.getLiteral("review");
这是什么问题?
推荐答案
rdf:ID需要解析为绝对IRI,但文件中没有xmlns:base.要么添加一个,要么对绝对IRI使用rdf:about,例如"c:me".
rdf:ID needs to be resolved to an absolute IRI but there is no xmlns:base in your file. Either add one or use rdf:about with absolute IRIs, e.g., "c:me".
这篇关于使用耶拿读取RDF文件时,getLiteral返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!