问题描述
我想在我的RDF本体的XML序列化中使用owl:
前缀(使用rdflib版本4.1.1);不幸的是,我仍在以rdf:Description
标签的形式进行序列化.我在 RDFLib:XML中的命名空间前缀中查看了有关将名称空间绑定到图形的答案序列化,但这似乎仅在使用ns
格式而不是xml
格式进行序列化时有效.
I would like to use the owl:
prefix in the XML serialization of my RDF ontology (using rdflib version 4.1.1); unfortunately I'm still getting the serialization as rdf:Description
tags. I have looked at the answer about binding the namespace to the graph at RDFLib: Namespace prefixes in XML serialization but this seems to only work when serializing using the ns
format rather than xml
format.
让我们更具体一些.我正在尝试获取以下本体(摘自引入RDFS和OWL )在XML中如下所示:
Let's be more concrete. I'm attempting to get the following ontology (as taken from Introducing RDFS and OWL) in XML as follows:
<!-- 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代码:
Here is the python code for constructing such a thing, using 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')
可悲的是,即使使用这些bind语句,也会打印以下XML:
Sadly, even with those bind statements, the following XML is printed:
<?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
中执行此操作而不覆盖序列化方法?
Granted, this is still an Ontology, and usable - but since we have various editors, the much more compact and readable first version using the owl
prefix would be far preferable. Is it possible to do this in rdflib
without overriding the serialization method?
更新
针对这些评论,我将我的奖励问题"改写为对我的问题的简单澄清.
In response to the comments, I'll rephrase my "bonus question" as simple clarification to my question at large.
不是奖励问题这里的主题涉及OWL名称空间格式的本体的构造,这是更冗长的RDF/XML规范的简写.尽管这里的问题比简单的仅为类或属性的快捷方式声明名称空间前缀的声明要大,但在代码中必须处理许多速记符号.例如,应将owl:Ontology
说明作为良好形式添加到该符号中.我希望rdflib支持该符号的完整规范-而不是必须进行我自己的序列化.
Not a Bonus Question The topic here involves the construction of the OWL namespace formatted ontology which is a shorthand for the more verbose RDF/XML specification. The issue here is larger though than the simple declaration of a namespace prefix for shorthand for only Classes or Properties, there are many shorthand notations that have to be dealt with in code; for example owl:Ontology
descriptions should be added as good form to this notation. I am hoping that rdflib has support for the complete specification of the notation- rather than have to roll my own serialization.
推荐答案
您需要使用pretty-xml
格式,而不是使用xml
格式.它在文档插件序列化器中列出.这将为您提供所需的输出类型.也就是说,您将使用以下类似的行来使用 PrettyXMLSerializer :
Instead of using the xml
format, you need to use the pretty-xml
format. It's listed in the documentation, Plugin serializers. That will give you the type of output that you're looking for. That is, you'd use a line like the following in order to use the PrettyXMLSerializer:
print graph.serialize(format='pretty-xml')
要解决奖励问题",您可以添加如下所示的行来创建本体头,然后使用pretty-xml
进行序列化将为您提供以下输出.
To address the "bonus question", you can add a line like the following to create the ontology header, and then serializing with pretty-xml
will give you the following output.
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用户),但是您可以看看:
Adding the x rdf:type owl:Ontology
triple isn't a very OWL-centric way of declaring the ontology though. It sounds like you're looking for something more like Jena's OntModel interface (which is just a convenience layer over Jena's RDF-centric Model), or the OWLAPI, but for RDFLib. I don't know whether such a thing exists (I'm not an RDFlib user), but you might have a look at:
- RDFLib/OWL-RL :看起来像一个推理机,但可能有一些原因您需要的方法.
- 使用RDFLib检查本体:一篇博客文章,其中包含指向源的链接,这些链接可能会满足您的某些要求.
- 是否有Python库可以处理OWL?:堆栈溢出问题(由于库/工具请求是题外话,但这是一个古老的问题),其中公认的答案指出rdflib以RDF为中心,而不是以OWL为中心,但是其他一些答案可能有用,特别是,尽管其中大多数已经过时了,甚至在2011年.
- RDFLib/OWL-RL: It looks like a reasoner, but it might have some of the methods that you need.
- Inspecting an ontology with RDFLib: a blog article with links to source that might do some of what you want.
- Is there a Python library to handle OWL?: A Stack Overflow question (now off-topic, because library/tool requests are off-topic, but it's an old question) where the accepted answer points out that rdflib is RDF-centric, not OWL-centric, but some of the other answers might be useful, particular this one, although most of those were outdated, even in 2011.
这篇关于在rdflib和xml序列化中使用owl:Class前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!