奇怪的名称空间声明 [英] strange namespaces declarations
问题描述
我使用的是jena 2.6.4,我注意到名称空间的异常行为.我正在使用以下代码:
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);
}
输出为:
<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"
奇怪的是,如果我将序列化更改为TURTLE(model.write(System.out, "TURTLE");
),则会得到以下输出:
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.
在将OntClass
名称从616
更改为c616
并将123
更改为c123
时,我也注意到了不同的行为:
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);
}
输出:
<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>
和TURTLE格式:
@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!
推荐答案
这两个前缀未使用,不会影响RDF图的性质". 所谓的本地名称"不能以数字开头,请参见:
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
第一个示例中的两个前缀(j.0和j.1)没有实际使用,为什么它们如此引起您的关注?
The two prefixes in your first example (j.0 and j.1) are not actually used, why they concern you so much?
这篇关于奇怪的名称空间声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!