Iterate over specific resource in RDF file with Jena

巧了我就是萌 提交于 2020-01-04 06:08:20

问题


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>

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.


回答1:


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/">
    …

your RDF graph actually contains the triple:

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

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

after which your RDF/XML document could look like:

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

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

This produces this output:

* 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]


来源:https://stackoverflow.com/questions/16939159/iterate-over-specific-resource-in-rdf-file-with-jena

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!