4

我想owl:在我的 RDF 本体的 XML 序列化中使用前缀(使用 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不覆盖序列化方法的情况下做到这一点?

更新

作为对评论的回应,我将把我的“奖金问题”改写为对我的整个问题的简单澄清。

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

4

1 回答 1

6

xml您需要使用格式,而不是使用pretty-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:一篇博客文章,其中包含指向可能执行您想要的某些操作的源的链接。
  • 是否有 Python 库来处理 OWL?:一个 Stack Overflow 问题(现在离题,因为库/工具请求离题,但这是一个老问题),其中接受的答案指出 rdflib 是以 RDF 为中心,而不是以 OWL 为中心,而是其他一些答案可能有用,尤其是这个答案,尽管其中大多数已经过时,即使在 2011 年也是如此。
于 2014-06-04T20:12:20.480 回答