【问题标题】:RDF/XML using Jena使用耶拿的 RDF/XML
【发布时间】:2011-07-14 13:37:53
【问题描述】:

如何使用 Jena 生成以下 RDF/XML?

<rdfs:Class rdf:about="http://example.com/A#B">
    <rdfs:subClassOf>
            <rdfs:Class rdf:about="http://example.com/A" />
     </rdfs:subClassOf>
        <rdf:Property rdf:about="http://example.com/C">
            <rdfs:range rdf:resource="http://example.com/A" />
        </rdf:Property>
</rdfs:Class>   

【问题讨论】:

  • example.com/A#B"> example.com/A" /> example.com/C"> example.com/A" />
  • 这是无效的 RDF/XML,因为您不能只在没有中间属性的 rdfs:Class 定义中嵌套 rdf:Property 定义。通常,在处理 RDF 时,请忘记 XML,并根据 XML 中编码的 三元组 来考虑。这是理解它的唯一方法。请记住,这是一组三元组。
  • 好吧,忘了有效性,我们可以删除属性
  • 最好在问题本身中发布与问题直接相关的信息,而不是作为评论。 StackOverflow 允许您在发布问题后对其进行编辑。您可以通过以四个空格开始每行来嵌入源代码(请参阅下面的答案)。

标签: rdf semantic-web jena rdfs


【解决方案1】:

网络上有许多 Jena 教程。但是,您的要求非常简单。这是一个解决方案:

package example;

import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.ModelFactory;

class RdfXmlExample {
    public static void main( String[] args ) {
        new RdfXmlExample().run();
    }

    public void run() {
        OntModel m = ModelFactory.createOntologyModel( OntModelSpec.RDFS_MEM );
        String NS = "http://example.com/test#";

        OntClass a = m.createClass( NS + "A" );
        OntClass b = m.createClass( NS + "B" );

        a.addSubClass( b );

        OntProperty c = m.createOntProperty( NS + "c" );
        c.addRange( a );

        m.write( System.out, "RDF/XML-ABBREV" );
    }
}

产生:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:owl="http://www.w3.org/2002/07/owl#"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <rdfs:Class rdf:about="http://example.com/test#B">
    <rdfs:subClassOf>
      <rdfs:Class rdf:about="http://example.com/test#A"/>
    </rdfs:subClassOf>
  </rdfs:Class>
  <rdf:Property rdf:about="http://example.com/test#c">
    <rdfs:range rdf:resource="http://example.com/test#A"/>
  </rdf:Property>
</rdf:RDF>

【讨论】:

  • 非常感谢你,你只是为了救援而发帖:)
【解决方案2】:

@Ian Dickinson 的回答很到位。 如果您希望将此输出写入文件,则可以改用此行

 m.write( new FileWriter("some-file.owl"), "RDF/XML-ABBREV" );

然后您可以通过 protege 或 WebVowl 查看此文件。

【讨论】:

    【解决方案3】:

    代码+解释

    • 耶拿 RDF API 教程
    • SPARQL 教程
    • 本体 API 概述

    http://incubator.apache.org/jena/getting_started/index.html

    从耶拿开始的好地方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      相关资源
      最近更新 更多