【发布时间】:2014-07-23 21:59:00
【问题描述】:
我想在我的 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>
这是构造这样一个东西的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 中做到这一点而不覆盖序列化方法?
更新
作为对 cmets 的回应,我将重新表述我的“额外问题”,作为对整个问题的简单澄清。
不是一个额外的问题这里的主题涉及 OWL 命名空间格式化本体的构造,它是更详细的 RDF/XML 规范的简写。这里的问题比简单地为类或属性的简写声明命名空间前缀要大,有许多简写符号必须在代码中处理;例如 owl:Ontology 描述应该作为良好的形式添加到这个符号中。我希望 rdflib 支持符号的完整规范——而不是我自己的序列化。
【问题讨论】:
-
我认为您的“额外问题!相关的第二个问题是如何将 owl:Ontology 标头添加到此 RDF 文件中?”最好将其作为一个单独的问题发布。它清晰、简洁,能够回答其中一个问题的人可能无法回答另一个问题。它们应该是单独的问题。但是一个本体头部只是多了几个三元组,所以就 RDF 表示而言,添加起来并不难。
-
是的,这就是我的想法——但这与使用 owl 命名空间的问题有关,因为我希望该标头也不是 rdf:Description 命名空间的一部分。跨度>
-
不过,这不是同一个问题。您正在查看的是 RDF 图的 RDF/XML 序列化,它是 OWL 本体的翻译。
rdf:Description在序列化中使用,因为没有使用 RDF/XML 允许的“类型作为元素名称”快捷方式。 These 是同一图的所有其他序列化(顺便提一下,由 Jena 的 rdfcat 生成)。不过,它们都是相同的 RDF 图。owl:是否被声明为 XML 命名空间与 是否 您是否有本体标头是正交的。只是外观不同而已。 -
我的意思是,没有什么是“rdf:Description 命名空间的成员”。
rdf:Description元素仅表示您正在编写一些主题为http://www.linkeddatatools.com/plants#planttyp的三元组。子元素的名称是属性,它们的内容是对象。在 RDF/XML 中,您还可以使用 rdf:type 属性的值作为元素名称。所以<owl:Class rdf:about="http://www.linkeddatatools.com/plants#planttype">…</owl:Class>只是的简写 -
<rdf:Description rdf:about="http://www.linkeddatatools.com/plants#planttype"><rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/> </rdf:Description>.
标签: python rdf xml-namespaces owl rdflib