Using Owl:class Prefix With Rdflib And Xml Serialization
Solution 1:
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')
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:RDFxmlns: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:Ontologyrdf:about="https://stackoverflow.com/q/24017320/1281433/ontology.owl"/><owl:Classrdf:about="http://www.linkeddatatools.com/plants#planttype"><rdfs:comment>The class of all plant types</rdfs:comment><rdfs:subClassOfrdf:resource="http://www.w3.org/2002/07/owl#Thing"/><rdfs:label>The plant type</rdfs:label></owl:Class></rdf:RDF>
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: 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.
Post a Comment for "Using Owl:class Prefix With Rdflib And Xml Serialization"