簡體   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