从文件加载本体时如何覆盖默认前缀?
我使用 RDFS 创建了一个本体,使用相对 URIrefs 来保存击键。现在,我想使用 Jena 加载本体并使用它来构建我的数据。但是,当我使用 m.read("file:flow-schema.rdf");
读取文件并使用 m.write(System.out, "RDF/XML -ABBREV", "http://flowschema/#");
,相对 URIref 的前缀为 file:flow-schema.rdf
,而不是 http://flowschema /#
.如何更改从文件加载的本体的默认前缀?
这是我的本体文件:
<?xml version="1.0" ?>
<!DOCTYPE rdf:RDF [<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#">]>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:base="http://flowschema/#">
<rdfs:Class rdf:ID="Flow" />
<rdf:Property rdf:ID="srcIP">
<rdfs:domain rdf:resource="#Flow" />
<rdfs:range rdf:resource="http://www.w3.org/2000/01/rdf-schema#Literal" />
</rdf:Property>
<rdf:Property rdf:ID="dstIP">
<rdfs:domain rdf:resource="#Flow" />
<rdfs:range rdf:resource="http://www.w3.org/2000/01/rdf-schema#Literal" />
</rdf:Property>
</rdf:RDF>
这是从文件中读取本体的 Java 代码:
OntModel m = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM);
m.read("file:flow-schema.rdf");
m.write(System.out, "RDF/XML-ABBREV", "http://flowschema/#");
I have created an ontology with RDFS, using relative URIrefs to save keystrokes. Now, I want to load the ontology with Jena and use it to structure my data. However, when I read the file in with m.read("file:flow-schema.rdf");
and display it with m.write(System.out, "RDF/XML-ABBREV", "http://flowschema/#");
, the relative URIrefs are prefixed with file:flow-schema.rdf
, not http://flowschema/#
. How can I change the default prefix for an ontology loaded from a file?
Here's my ontology file:
<?xml version="1.0" ?>
<!DOCTYPE rdf:RDF [<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#">]>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:base="http://flowschema/#">
<rdfs:Class rdf:ID="Flow" />
<rdf:Property rdf:ID="srcIP">
<rdfs:domain rdf:resource="#Flow" />
<rdfs:range rdf:resource="http://www.w3.org/2000/01/rdf-schema#Literal" />
</rdf:Property>
<rdf:Property rdf:ID="dstIP">
<rdfs:domain rdf:resource="#Flow" />
<rdfs:range rdf:resource="http://www.w3.org/2000/01/rdf-schema#Literal" />
</rdf:Property>
</rdf:RDF>
Here's the Java code that reads the ontology from a file:
OntModel m = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM);
m.read("file:flow-schema.rdf");
m.write(System.out, "RDF/XML-ABBREV", "http://flowschema/#");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您似乎尝试在 RDF/XML 中设置基本 URI,但使用了
xmlns:base
,这是不正确的用法。要设置 Base URI,您必须使用xml:base
,您所做的是定义一个额外的命名空间base
,而不是定义 Base URI。更改 RDF/XML 的该部分,然后它应该可以正常工作,而不需要其他人建议的解决方法。
您当前的写入代码最终会出现不正确的 URI,因为 Jena 已使用文件 URI 作为基础自动生成绝对 URI。如果就 Jena 而言,RDF 中的 URI 不被认为位于该 Base URI 之下,则设置用于写入的 Base URI 没有任何效果。
注意:
RDF 通常需要绝对 URI,相对 URI 是一种序列化便利,可以帮助压缩语法。如果您使用相对 URI,则应该始终显式指定基本 URI(如果语法允许),如果不允许,则使用绝对 URI。
You appear to have tried to set the Base URI in your RDF/XML but you've used
xmlns:base
which is incorrect usage. To set the Base URI you must usexml:base
, what you have done is define an additional namespacebase
rather than define the Base URI.Change that part of your RDF/XML and then it should work fine without needing the workarounds others have suggested.
Your current write code ends up with incorrect URIs because Jena has already automatically generated absolute URIs using the file URI as a base. Setting a Base URI for writing has no effect if the URIs in the RDF are not perceived to be under that Base URI as far as Jena is concerned.
Note:
RDF generally requires absolute URIs, relative URIs are a serialization convenience that can help compress the syntax. If you use relative URIs you should always specify the Base URI explicitly if the syntax permits this and if not use absolute URIs instead.
您可以使用 read(String url, String base, String lang) 通过指定基本 URI 而不是
read(String url)
? ?you could use read(String url, String base, String lang) by specifying the base URI instead of
read(String url)
? ?Jena 在内部对所有内容都使用完整的 URI。这就是为什么您必须在加载时指定基本 URI。仅在写入时指定它不会达到预期的效果。正如皮埃尔所说,这应该有效:
Jena internally uses full URIs for everything. That's why you have to specify the base URI at load time. Specifying it at write time only doesn't have the desired effect. As Pierre said, this should work: