mardi 3 juin 2014

Using owl:Class prefix with rdflib and xml serialization


Vote count:

0




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.


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://ift.tt/1osmGDC">

<rdfs:label>The plant type</rdfs:label>
<rdfs:comment>The class of all plant types.</rdfs:comment>

</owl:Class>


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://ift.tt/1x0Jbp3")

# 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')


Sadly, even with those bind statements, the following XML is printed:



<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
xmlns:rdf="http://ift.tt/oZyhLL"
xmlns:rdfs="http://ift.tt/177k8UL"
>
<rdf:Description rdf:about="http://ift.tt/1osmGDC">
<rdfs:subClassOf rdf:resource="http://ift.tt/1jVq8F0"/>
<rdfs:label>The plant type</rdfs:label>
<rdfs:comment>The class of all plant types</rdfs:comment>
<rdf:type rdf:resource="http://ift.tt/1x0Jbp7"/>
</rdf:Description>
</rdf:RDF>


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?


Bonus Question! A secondary question that is related is how can I add owl:Ontology headers to this RDF file?



asked 59 secs ago






Aucun commentaire:

Enregistrer un commentaire