我想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 支持符号的完整规范——而不是必须滚动我自己的序列化。