[英]Using owl:Class prefix with rdflib and xml serialization
我想在我的RDF本体的XML序列化中使用owl:
前缀(使用rdflib版本4.1.1); 不幸的是,我仍然将序列化作为rdf:Description
标签。 我已经查看了关于将命名空间绑定到RDFLib上的图形的答案:XML序列化中的命名空间前缀,但这似乎仅在使用ns
格式而不是xml
格式进行序列化时才有效。
让我们更具体一点。 我试图在XML中获得以下本体(如引入RDFS和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
执行此rdflib
而不覆盖序列化方法?
更新
在回应评论时,我会将我的“红利问题”改为简单澄清我的问题。
不是奖金问题这里的主题涉及构建OWL命名空间格式化本体,它是更详细的RDF / XML规范的简写。 这里的问题比仅为类或属性的简写的名称空间前缀的简单声明更大,有许多简写符号必须在代码中处理; 例如owl:Ontology
描述应该作为这种表示法的良好形式添加。 我希望rdflib支持表示法的完整规范 - 而不是必须滚动我自己的序列化。
而不是使用的xml
格式,你需要使用pretty-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
triple不是一种非常以OWL为中心的声明本体的方法。 听起来你正在寻找更像Jena的OntModel接口(这只是Jena以RDF为中心的模型的便利层)或OWLAPI,但对于RDFLib。 我不知道是否存在这样的事情(我不是RDFlib用户),但您可能会看一下:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.