奇怪的命名空间声明

时间:2012-03-16 09:50:14

标签: xml-namespaces jena

我正在使用jena 2.6.4,我注意到名称空间的奇怪行为。我正在使用以下代码:

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>

我不明白为什么声明以下命名空间:

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");),那么我得到以下输出:

@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 .

名称空间声明符合我的预期。

我还注意到,在将OntClass名称从616更改为c616123c123时,我的行为有所不同:

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 .

我做错了吗?有人能解释这种奇怪的行为吗? 谢谢!

1 个答案:

答案 0 :(得分:2)

这两个前缀未使用,不会影响RDF图的“性质”。 所谓的'localnames'不能以数字开头,请参阅:

你的第一个例子中的两个前缀(j.0和j.1)实际上并没有使用,为什么他们如此关注你?