使用jena读取RDF文件时,getLiteral返回null

时间:2018-04-17 05:31:58

标签: sparql rdf jena ontology

我这些天一直在阅读有关OWL本体和RDF文件的内容。我仍然无法理解这一点。

让我们说我使用Protege创建了一个简单的本体。它有一个名为Review的单个类,它有两个数据属性,即注释和评级。

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

我试过的代码是

  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");

这个问题是什么?

1 个答案:

答案 0 :(得分:1)

rdf:ID需要解析为绝对IRI,但文件中没有xmlns:base。添加一个或使用rdf:about与绝对IRI,例如,&#34; c:me&#34;。