owlcs / owlapi

OWL API main repository
831 stars 315 forks source link

How to force OWL-API to use shortened IRIs in RDF/XML? #1107

Open trojczak opened 1 year ago

trojczak commented 1 year ago

Hi!

I have the following simple code:

OWLOntologyManager ontologyManager = OWLManager.createOWLOntologyManager();
OWLDataFactory dataFactory = ontologyManager.getOWLDataFactory();
OWLOntology ontology = ontologyManager.createOntology(IRI.create(PREFIX));

OWLClass owlClass1 = dataFactory.getOWLClass(IRI.create(PREFIX + "C1"));
OWLClass owlClass2 = dataFactory.getOWLClass(IRI.create(PREFIX2 + "C2"));

ontology.addAxiom(dataFactory.getOWLDeclarationAxiom(owlClass1));
ontology.addAxiom(dataFactory.getOWLDeclarationAxiom(owlClass2));

RDFXMLDocumentFormat format = new RDFXMLDocumentFormat();
format.setPrefix("o2:", PREFIX2);
ontologyManager.saveOntology(ontology, format, IRI.create("file:/some/path"));

The saved ontology looks like this (I've removed comments):

<?xml version="1.0"?>
<rdf:RDF xmlns="http://example.com/ontology1/"
     xml:base="http://example.com/ontology1/"
     xmlns:o2="http://example.com/ontology2/"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
    <owl:Ontology rdf:about="http://example.com/ontology1/"/>

    <owl:Class rdf:about="http://example.com/ontology1/C1"/>

    <owl:Class rdf:about="http://example.com/ontology2/C2"/>
</rdf:RDF>

My goal here is to force OWL-API to use shortened IRIs inside the rdf:about property. I.e. to have something like this:

<owl:Class rdf:about="&o2;C2"/>
ignazio1977 commented 1 year ago

format.setPrefix("o2:", PREFIX2);

The semicolon shouldn't be there in the prefix name, try

format.setPrefix("o2", PREFIX2);