繁体   English   中英

使用owl:带有rdflib和xml序列化的类前缀

[英]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用户),但您可能会看一下:

  • RDFLib / OWL-RL :它看起来像一个推理器,但它可能有你需要的一些方法。
  • 使用RDFLib检查本体 :一篇博客文章,其中包含指向源代码的链接,可能会执行您想要的某些操作。
  • 是否有一个Python库来处理OWL? :Stack Overflow问题(现在偏离主题,因为库/工具请求是偏离主题的,但这是一个老问题),其中接受的答案指出rdflib是以RDF为中心的,而不是以OWL为中心的,而是其他一些答案可能是有用的,尤其是这一点 ,尽管大多数答案都已过时,即使在2011年也是如此。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM