我想在我的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>
以下是使用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
中执行此操作而不覆盖序列化方法?
更新
在回应评论时,我将重新提出我的问题&#34;简单澄清我的问题。
不是奖金问题这里的主题涉及构建OWL命名空间格式化本体,这是更详细的RDF / XML规范的简写。这里的问题比仅为类或属性的简写的名称空间前缀的简单声明更大,有许多简写符号必须在代码中处理;例如,owl:Ontology
描述应添加为此表示法的良好形式。我希望rdflib支持表示法的完整规范 - 而不是必须滚动我自己的序列化。
答案 0 :(得分:6)
您需要使用xml
格式,而不是使用pretty-xml
格式。它列在文档Plugin serializers中。这将为您提供您正在寻找的输出类型。也就是说,您可以使用如下所示的行来使用PrettyXMLSerializer:
print graph.serialize(format='pretty-xml')
要解决&#34;奖励问题&#34;,您可以添加如下所示的行来创建本体标题,然后使用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用户),但您可能会看一下: