-->

Jena adds path/url to URIs

2019-09-01 03:54发布

问题:

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)



标签: rdf jena