使用jena读取RDF文件时getLiteral returns null

getLiteral returns null when reading RDF file using jena

这些天我一直在阅读有关 OWL ontology 和 RDF 文件的内容。还是看不懂。

假设我使用 Protege 创建了一个简单的 ontology。它有一个名为 Review 的 class,具有两个数据属性,即评论和评级。

现在我想创建一个单独的用 xml 编写的 RDF 文件,其中有一些注释。我创建的文件看起来像

<?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 模型,阅读此评论并在我的 ontology 中创建个人。创建我已经想通的个人部分。但我无法从这些评论中获得评分和评论值。

我试过的密码是

  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();
        }

以下行 returns 为空。

Literal name = soln.getLiteral("review");

这里面有什么问题?

rdf:ID 需要解析为绝对 IRI,但您的文件中没有 xmlns:base。添加一个或使用 rdf:about 和绝对 IRI,例如 "c:me".