【问题标题】:Using owl:Class prefix with rdflib and xml serialization使用 owl:Class 前缀与 rdflib 和 xml 序列化
【发布时间】:2014-07-23 21:59:00
【问题描述】:

我想在我的 RDF 本体的 XML 序列化中使用 owl: 前缀(使用 rdflib 版本 4.1.1);不幸的是,我仍然将序列化为rdf:Description 标签。我已经在RDFLib: Namespace prefixes in XML serialization 查看了关于将命名空间绑定到图形的答案,但这似乎只在使用ns 格式而不是xml 格式进行序列化时才有效。

让我们更具体一些。我正在尝试在 XML 中获取以下本体(取自 Introducing RDFS and OWL),如下所示:

<!-- OWL Class Definition - Plant Type -->
<owl:Class rdf:about="http://www.linkeddatatools.com/plants#planttype">

    <rdfs:label>The plant type</rdfs:label>
    <rdfs:comment>The class of all plant types.</rdfs:comment>

</owl:Class>

这是构造这样一个东西的python代码,使用rdflib

from rdflib.namespace import OWL, RDF, RDFS
from rdflib import Graph, Literal, Namespace, URIRef

# Construct the linked data tools namespace
LDT   = Namespace("http://www.linkeddatatools.com/plants#")

# Create the graph
graph = Graph()

# Create the node to add to the Graph
Plant = URIRef(LDT["planttype"])

# Add the OWL data to the graph
graph.add((Plant, RDF.type, OWL.Class))
graph.add((Plant, RDFS.subClassOf, OWL.Thing))
graph.add((Plant, RDFS.label, Literal("The plant type")))
graph.add((Plant, RDFS.comment, Literal("The class of all plant types")))

# Bind the OWL and LDT name spaces
graph.bind("owl", OWL)
graph.bind("ldt", LDT)

print graph.serialize(format='xml')

遗憾的是,即使使用这些绑定语句,仍会打印以下 XML:

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
>
  <rdf:Description rdf:about="http://www.linkeddatatools.com/plants#planttype">
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
    <rdfs:label>The plant type</rdfs:label>
    <rdfs:comment>The class of all plant types</rdfs:comment>
    <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
  </rdf:Description>
</rdf:RDF>

当然,这仍然是一个本体,并且可用 - 但由于我们有各种编辑器,使用 owl 前缀的更紧凑和可读的第一个版本将是更可取的。是否可以在rdflib 中做到这一点而不覆盖序列化方法?

更新

作为对 cme​​ts 的回应,我将重新表述我的“额外问题”,作为对整个问题的简单澄清。

不是一个额外的问题这里的主题涉及 OWL 命名空间格式化本体的构造,它是更详细的 RDF/XML 规范的简写。这里的问题比简单地为类或属性的简写声明命名空间前缀要大,有许多简写符号必须在代码中处理;例如 owl:Ontology 描述应该作为良好的形式添加到这个符号中。我希望 rdflib 支持符号的完整规范——而不是我自己的序列化。

【问题讨论】:

  • 我认为您的“额外问题!相关的第二个问题是如何将 owl:Ontology 标头添加到此 RDF 文件中?”最好将其作为一个单独的问题发布。它清晰、简洁,能够回答其中一个问题的人可能无法回答另一个问题。它们应该是单独的问题。但是一个本体头部只是多了几个三元组,所以就 RDF 表示而言,添加起来并不难。
  • 是的,这就是我的想法——但这与使用 owl 命名空间的问题有关,因为我希望该标头也不是 rdf:Description 命名空间的一部分。跨度>
  • 不过,这不是同一个问题。您正在查看的是 RDF 图的 RDF/XML 序列化,它是 OWL 本体的翻译。 rdf:Description 在序列化中使用,因为没有使用 RDF/XML 允许的“类型作为元素名称”快捷方式。 These 是同一图的所有其他序列化(顺便提一下,由 Jena 的 rdfcat 生成)。不过,它们都是相同的 RDF 图。 owl: 是否被声明为 XML 命名空间与 是否 您是否有本体标头是正交的。只是外观不同而已。
  • 我的意思是,没有什么是“rdf:Description 命名空间的成员”。 rdf:Description 元素仅表示您正在编写一些主题为http://www.linkeddatatools.com/plants#planttyp 的三元组。子元素的名称是属性,它们的内容是对象。在 RDF/XML 中,您还可以使用 rdf:type 属性的值作为元素名称。所以&lt;owl:Class rdf:about="http://www.linkeddatatools.com/plants#planttype"&gt;…&lt;/owl:Class&gt; 只是的简写
  • &lt;rdf:Description rdf:about="http://www.linkeddatatools.com/plants#planttype"&gt;&lt;rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/&gt; &lt;/rdf:Description&gt;.

标签: python rdf xml-namespaces owl rdflib


【解决方案1】:

您需要使用pretty-xml 格式,而不是使用xml 格式。它列在文档Plugin serializers 中。这将为您提供您正在寻找的输出类型。也就是说,为了使用PrettyXMLSerializer,您需要使用如下行:

print graph.serialize(format='pretty-xml')

要解决“额外问题”,您可以添加如下一行来创建本体标头,然后使用pretty-xml 序列化将为您提供以下输出。

graph.add((URIRef('https://stackoverflow.com/q/24017320/1281433/ontology.owl'), RDF.type, OWL.Ontology ))
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF
  xmlns:owl="http://www.w3.org/2002/07/owl#"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
>
  <owl:Ontology rdf:about="https://stackoverflow.com/q/24017320/1281433/ontology.owl"/>
  <owl:Class rdf:about="http://www.linkeddatatools.com/plants#planttype">
    <rdfs:comment>The class of all plant types</rdfs:comment>
    <rdfs:subClassOf rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
    <rdfs:label>The plant type</rdfs:label>
  </owl:Class>
</rdf:RDF>

添加x rdf:type owl:Ontology 三元组并不是一种以OWL 为中心的方式来声明本体。听起来您正在寻找更像 Jena 的 OntModel 接口(它只是 Jena 的以 RDF 为中心的模型之上的一个便利层)或 OWLAPI,但用于 RDFLib 的东西。我不知道这样的东西是否存在(我不是 RDFlib 用户),但你可以看看:

  • RDFLib/OWL-RL:看起来像推理机,但可能有一些你需要的方法。
  • Inspecting an ontology with RDFLib:一篇博客文章,其中包含指向可能执行您想要的某些操作的源的链接。
  • Is there a Python library to handle OWL?:堆栈溢出问题(现在离题,因为库/工具请求离题,但这是一个老问题),其中接受的答案指出 rdflib 以 RDF 为中心,而不是以 OWL 为中心,但有些其他答案中的一些可能有用,尤其是 this one,尽管其中大多数已经过时,即使在 2011 年也是如此。

【讨论】:

  • 太棒了——答案也很简单;很高兴你找到它!我担心我不得不对序列化程序进行处理。
  • 不,它已经被弄脏了。请注意,更漂亮的 RDF 序列化可能更昂贵,因为它们必须检查可能的缩写。不过,除非它成为问题,否则我不会担心。
猜你喜欢
  • 1970-01-01
  • 2011-01-21
  • 2013-05-02
  • 1970-01-01
  • 2015-03-31
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
相关资源
最近更新 更多