How to convert XSD Simple Type to rdfs:Datatype using Jena
题
Suppose I have an XSD file having below lines of code;
<xsd:simpleType name="test">
<xsd:restriction base="xsd:string">
<xsd:maxLength value="50" />
</xsd:restriction>
</xsd:simpleType>
What I am trying to do is to convert this xsd simple type into an instance of rdfs:Datatype in an ontology. Below is what I want.
<rdfs:Datatype rdf:about="http://www.example.org/example1/#testDatatype">
<rdfs:subClassOf rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
<owl:equivalentClass>
<rdfs:Datatype>
<owl:onDatatype rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
<owl:withRestrictions rdf:parseType="Collection">
<rdf:Description>
<xsd:maxLength rdf:datatype="http://www.w3.org/2001/XMLSchema#string"
>50</xsd:maxLength>
</rdf:Description>
</owl:withRestrictions>
</rdfs:Datatype>
</owl:equivalentClass>
</rdfs:Datatype>
How do I do this using Jena?
解决方案
Turns out I can do this with the below code. I parse XSD with XSOM. Assume that I stored "test" simpletype in a variable called "simple" (XSSimpleType).
String URI = simple.getTargetNamespace() + "#" + simple.getName() + "Datatype";
OntModel ontology = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
OntClass datatype = ontology.createOntResource(OntClass.class, RDFS.Datatype, URI);
Resource onDatatype = XSD.xstring;
OntClass equivClass = ontology.createOntResource(OntClass.class, RDFS.Datatype, null);
equivClass.addProperty(OWL2.onDatatype, onDatatype);
datatype.addEquivalentClass(equivClass);
It is not a complete code and it does not include the code segment related with parsing XSD but I hope it will give the idea.
不隶属于 StackOverflow