Jena adds path/url to URIs

谁都会走 提交于 2020-01-16 18:41:50

问题


I just started working with RDF and Jena. I got a RDF model which I want to read, modify and write out again. Assume my model file is located at http://xyz/model.ttl and contains an element with URI "someURI". When I do

Model model = ModelFactory.createDefaultModel();
model.read("http://xyz/model.ttl", "", "TURTLE");
model.write(System.out, "TURTLE");

the URI in the output changes from "someURI" to http://xyz/someURI. When I read the model from the local filesystem, the URI changes to file://pathToFile/someURI. Is there a way to avoid this behaviour and keep the URI unchanged?


回答1:


In RDF (like HTML) URLs (/ URIs / IRIs) are resolved relative to a base URL, typically the URL of the source document.

So reading someURI in http://xyz/model.ttl becomes http://xyz/someURI, and from a file you get file://pathToFile/someURI.

You can avoid this by supplying an explicit base, which will make the resulting URLs consistent across sources.

model.read("http://xyz/model.ttl", "http://xyz/model.ttl", "TURTLE");
// or with same result
model.read(fileSource, "http://xyz/model.ttl", "TURTLE");

and also relativise the result:

model.write(System.out, "TURTLE", "http://xyz/model.ttl");

(The documentation states that the base and lang arguments are switched for reading and writing, which seems odd)



来源:https://stackoverflow.com/questions/25136333/jena-adds-path-url-to-uris

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!