在rdflib和XML序列化中使用owl:Class前缀

6 投票
1 回答
4510 浏览
提问于 2025-04-18 08:25

我想在我的RDF本体的XML序列化中使用owl:这个前缀(我用的是rdflib版本4.1.1);可惜的是,我得到的序列化结果还是rdf:Description标签。我看过关于如何将命名空间绑定到图形的回答,地址在RDFLib: XML序列化中的命名空间前缀,但这似乎只在使用ns格式序列化时有效,而不是xml格式。

让我们具体一点。我想把以下本体(来自介绍RDFS和OWL)以XML格式表示,如下所示:

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

这里是用rdflib构建这样的东西的Python代码:

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能支持完整的表示规范,而不是让我自己去实现序列化。

1 个回答

9

你需要使用 pretty-xml 格式,而不是 xml 格式。这个信息在文档中有提到,具体可以查看 插件序列化器。使用这个格式,你就能得到你想要的输出。也就是说,你可以用下面这样的代码来使用 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 这个三元组并不是声明本体的最标准方式。听起来你可能想要的是类似 Jena 的 OntModel 接口(这只是 Jena 的 RDF 模型上的一个便利层),或者是 OWLAPI,但用于 RDFLib。我不确定是否有这样的东西(我不是 RDFlib 的用户),但你可以看看:

  • RDFLib/OWL-RL: 看起来像是一个推理器,但可能有一些你需要的方法。
  • 用 RDFLib 检查本体: 一篇博客文章,里面有一些链接,可能能满足你的需求。
  • 有没有 Python 库可以处理 OWL?: 这是一个 Stack Overflow 的问题(现在已经不太相关,因为库/工具请求不再是主题,但这是个老问题),接受的答案指出 rdflib 是以 RDF 为中心的,而不是以 OWL 为中心的,但其他一些答案可能会有用,特别是 这个答案,尽管大部分内容在 2011 年时就已经过时了。

撰写回答