问题
I'm using jena 2.6.4, and I notice a strange behavior with namespaces. I'm using the following code:
public static void main(String[] args) {
String myUri = "http://www.example.com/1.0/myUri#";
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
model.setNsPrefix("myuri", myUri);
OntClass c616 = model.createClass(myUri + "616");
OntClass c123 = model.createClass(myUri + "123");
Individual a = c616.createIndividual(myUri + "a");
a.addOntClass(c123);
model.write(System.out);
}
The output is:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:j.0="http://www.example.com/1.0/myUri#616"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:j.1="http://www.example.com/1.0/myUri#123"
xmlns:myuri="http://www.example.com/1.0/myUri#"
xmlns:owl="http://www.w3.org/2002/07/owl#" >
<rdf:Description rdf:about="http://www.example.com/1.0/myUri#123">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
</rdf:Description>
<rdf:Description rdf:about="http://www.example.com/1.0/myUri#a">
<rdf:type rdf:resource="http://www.example.com/1.0/myUri#123"/>
<rdf:type rdf:resource="http://www.example.com/1.0/myUri#616"/>
</rdf:Description>
<rdf:Description rdf:about="http://www.example.com/1.0/myUri#616">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
</rdf:Description>
</rdf:RDF>
I don't understand why the following namespaces are declared:
xmlns:j.0="http://www.example.com/1.0/myUri#616"
xmlns:j.1="http://www.example.com/1.0/myUri#123"
Strangely, If I change the serialization to TURTLE (model.write(System.out, "TURTLE");
), then I get the following output:
@prefix myuri: <http://www.example.com/1.0/myUri#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
<http://www.example.com/1.0/myUri#123>
a owl:Class .
myuri:a
a <http://www.example.com/1.0/myUri#123> , <http://www.example.com/1.0/myUri#616> .
<http://www.example.com/1.0/myUri#616>
a owl:Class .
Where namespaces declarations are as I expected.
I've also noticed I different behavior when changing OntClass
names from 616
to c616
and 123
to c123
:
public static void main(String[] args) {
String myUri = "http://www.example.com/1.0/myUri#";
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
model.setNsPrefix("myuri", myUri);
OntClass c616 = model.createClass(myUri + "c616");
OntClass c123 = model.createClass(myUri + "c123");
Individual a = c616.createIndividual(myUri + "a");
a.addOntClass(c123);
model.write(System.out);
}
outputs:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:myuri="http://www.example.com/1.0/myUri#"
xmlns:owl="http://www.w3.org/2002/07/owl#" >
<rdf:Description rdf:about="http://www.example.com/1.0/myUri#c123">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
</rdf:Description>
<rdf:Description rdf:about="http://www.example.com/1.0/myUri#a">
<rdf:type rdf:resource="http://www.example.com/1.0/myUri#c123"/>
<rdf:type rdf:resource="http://www.example.com/1.0/myUri#c616"/>
</rdf:Description>
<rdf:Description rdf:about="http://www.example.com/1.0/myUri#c616">
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Class"/>
</rdf:Description>
</rdf:RDF>
and in TURTLE format:
@prefix myuri: <http://www.example.com/1.0/myUri#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
myuri:c123
a owl:Class .
myuri:a
a myuri:c123 , myuri:c616 .
myuri:c616
a owl:Class .
Am I doing something wrong? Can someone explain this strange behavior? Thanks!
回答1:
The two prefixes are not used and do not affect the 'nature' of your RDF graph. The so called 'localnames' cannot start with a number, see:
- http://www.w3.org/TR/REC-xml/#NT-Name
- http://www.w3.org/TR/REC-xml/#NT-NameStartChar
The two prefixes in your first example (j.0 and j.1) are not actually used, why they concern you so much?
来源:https://stackoverflow.com/questions/9735008/strange-namespaces-declarations