Tuesday, October 19, 2010

How-to get rid of auto-generated prefixes in Jena.

During my first experiments with Jena to read and produce custom ontologies, I suddenly faced the auto-generated namespace glitch that jena produces when rendering ontologies as RDF (XML view). After a bit o search around the subject, I got to the solution (by narrowing my search after a while).

According to this post, whose blog now I follow, one solution would be to use N3. Now if you can´t get away with and must render it to RDF, the fix is pretty simple.

The key to this is the PrefixMapping interface, which is a super-interface of Model. The method setNsPrefix lets us assign a more meaningful (to human readers!) namespace:

public static void main( String[] args ) {
Model m = ModelFactory.createDefaultModel();
String NS = "";
m.setNsPrefix( "eg", NS );
Property p = m.createProperty( NS + "p" );
Resource r = m.createResource( NS + "r" );
r.addProperty( p, 42 );
m.write( System.out, "RDF/XML" );


xmlns:rdf="" >
<rdf:description about="">

As I said, pretty simple.

No comments:

Post a Comment