使用Jena遍历RDF文件中的特定资源

使用Jena遍历RDF文件中的特定资源

本文介绍了使用Jena遍历RDF文件中的特定资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apache Jena读取RDF文件,如下所示:

I'm using Apache Jena to read a RDF file, which looks like this:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:dcat="http://www.w3.org/ns/dcat#"
    xmlns:skos="http://www.w3.org/2004/02/skos/core#"
    xmlns:foaf="http://xmlns.com/foaf/0.1/"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:dct="http://purl.org/dc/terms/"
    xmlns:dctypes="http://purl.org/dc/dcmitype/"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <dcat:Catalog rdf:about="http://uri/">
    <dcat:dataset>
      <dcat:Dataset rdf:about="http://url/bop2262008322pdf/">
        <dct:publisher>
          <foaf:Organization>
            <foaf:homepage rdf:resource="http://url"/>
            <dct:title xml:lang="ca">Neme</dct:title>
          </foaf:Organization>
        </dct:publisher>
        <dcat:distribution>
          <dcat:Download>
            <dct:modified rdf:datatype="http://www.w3.org/2001/XMLSchema#date"
            >2012-11-09T16:23:22</dct:modified>
            <dct:format>
              <dct:IMT>
                <rdfs:label>pdf</rdfs:label>
                <rdf:value>application/pdf</rdf:value>
              </dct:IMT>
            </dct:format>
            <dcat:accessURL>http://url/</dcat:accessURL>
          </dcat:Download>
        </dcat:distribution>
        <dcat:keyword xml:lang="ca">Keyword 2</dcat:keyword>
        <dcat:keyword xml:lang="ca">Keyword</dcat:keyword>
        <dct:creator>Creator</dct:creator>
        <dct:modified rdf:datatype="http://www.w3.org/2001/XMLSchema#date"
        >2013-04-16T12:27:14</dct:modified>
        <dct:issued rdf:datatype="http://www.w3.org/2001/XMLSchema#date"
        >2011-03-02T10:28:58</dct:issued>
      </dcat:Dataset>
    </dcat:dataset>
    <dct:license rdf:resource="http://creativecommons.org/licenses/by/3.0/"/>
    <dct:title xml:lang="es">Example</dct:title>
    <dct:title xml:lang="ca">Example</dct:title>
  </dcat:Catalog>
</rdf:RDF>

我基本上想获得每个dcat:dataset资源和相应的语句.但是我无法弄清楚如何从一个特定的名称空间和本地名称(在本例中为dcat:dataset)遍历所有资源.我猜只是有可能通过包含属性来查找资源.但是,Jena似乎不支持名称空间dcat.我在词汇表中找不到它.

I basically want to get every dcat:dataset resource and the corresponding statements. But I can't figure out how to iterate over all resources form a specific namespace and localname (in this case, dcat:dataset). I guess it is just possible to find resources by containing properties. However the namespace dcat seems not to be supported by Jena. I can't find it in the vocabulary.

推荐答案

在大多数情况下,本地名称和前缀仅在序列化中起作用.尽管RDF/XML文件包含

For the most part, localnames and prefixes only matter in serializations. Although the RDF/XML file contains

<dcat:Catalog rdf:about="http:/uri/>
  <dcat:dataset>
    <dcat:Dataset rdf:about="http://url/bop2262008322pdf/">
    …

您的RDF图实际上包含三元组:

your RDF graph actually contains the triple:

<http:/uri/> <http://www.w3.org/ns/dcat#dataset> <http://url/bop2262008322pdf/>

这是一个重要的区别,因为序列化的图可以使用不同的前缀并产生不同的外观输出.例如,您的RDF/XML文档可能另外具有前缀dcatdata:

This is an important distinction, because a serialized graph could use different prefixes and produce different looking output. For instance, your RDF/XML document could additionally have the prefix dcatdata:

<rdf:RDF
  xmlns:dcatdata="http://www.w3.org/ns/dcat#data"
  >

之后,您的RDF/XML文档可能如下所示:

after which your RDF/XML document could look like:

<dcat:Catalog rdf:about="http:/uri/>
  <dcatdata:set>
    <dcat:Dataset rdf:about="http://url/bop2262008322pdf/">
    …

因此,您不应依赖于特定的前缀,而应通过其IRI访问资源.在这种情况下,听起来您想使用rdf:type dcat:Dataset和以这些资源为主题的语句来检索资源.使用Jena模型和资源API做到这一点很容易.这是一个示例:

So, you should not depend on a particular prefix, but rather access resources by their IRIs. In this case, it sounds like you want to retrieve resources with rdf:type dcat:Dataset and statements that have those resources as subjects. That is easy enough to do using the Jena Model and Resource APIs. Here's an example:

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.RDF;

public class DCATExample {
  public static void main(String[] args) {
    final String dcat = "http://www.w3.org/ns/dcat#";
    Model model = ModelFactory.createDefaultModel();
    model.read( "data.rdf" );
    Resource datasetType = model.getResource( dcat + "Dataset" );
    ResIterator datasets = model.listSubjectsWithProperty( RDF.type, datasetType );
    while ( datasets.hasNext() ) {
      Resource dataset = datasets.next();
      StmtIterator stmts  = dataset.listProperties();
      System.out.println( "* "+dataset );
      while ( stmts.hasNext() ) {
        System.out.println( "** "+stmts.next() );
      }
    }
  }
}

这将产生以下输出:

* http://url/bop2262008322pdf/
** [http://url/bop2262008322pdf/, http://purl.org/dc/terms/publisher, -7ec508e8:13f14cb9040:-7ffd]
** [http://url/bop2262008322pdf/, http://www.w3.org/ns/dcat#distribution, -7ec508e8:13f14cb9040:-7fff]
** [http://url/bop2262008322pdf/, http://www.w3.org/ns/dcat#keyword, "Keyword 2"@ca]
** [http://url/bop2262008322pdf/, http://www.w3.org/ns/dcat#keyword, "Keyword"@ca]
** [http://url/bop2262008322pdf/, http://purl.org/dc/terms/creator, "Creator"]
** [http://url/bop2262008322pdf/, http://purl.org/dc/terms/modified, "2013-04-16T12:27:14"^^http://www.w3.org/2001/XMLSchema#date]
** [http://url/bop2262008322pdf/, http://purl.org/dc/terms/issued, "2011-03-02T10:28:58"^^http://www.w3.org/2001/XMLSchema#date]
** [http://url/bop2262008322pdf/, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/ns/dcat#Dataset]

这篇关于使用Jena遍历RDF文件中的特定资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 20:06