mmmattos.net

Saturday, October 23, 2010

Adding ENTITIES to the generated RDF

If you have to embed typed resources as literals within an RDF, such in this example:

• <dcterms:identifier rdf:resource="&xxx;type#TheDataValue"/>

The xxx entity abbreviation must be declared outside the RDF core, in the XML DOCTYPE main node. Something like:

<!DOCTYPE rdf:RDF [
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#">
<!ENTITY xxx "http://someurl.org/path/path">
]>


So, the question is: how to implement this in Jena, considering Jena RDF write just outputs the RDF tree...?
As shown in this Jena file i/o how-to, Jena provides some advanced output configuration options that can control the generation of those items.
Indeed, there are two options that can be useful in generating the extra ENTITY references.

Those are:
  • showXmlDeclaration:
"If true, an XML Declaration is included in the output, if false no XML declaration is included. The default behaviour only gives an XML Declaration when asked to write to an OutputStreamWriter that uses some encoding other than UTF-8 or UTF-16. In this case the encoding is shown in the XML declaration. To ensure that the encoding attribute is shown in the XML declaration either:
Set this option to true and use the write(Model,Writer,String) variant with an appropriate OutputStreamWriter.
Or set this option to false, and write the declaration to an OutputStream before calling write(Model,OutputStream,String)."
  • showDoctypeDeclaration:
"If true, an XML Doctype declaration is included in the output. This declaration includes a !ENTITY declaration for each prefix mapping in the model, and any attribute value that starts with the URI of that mapping is written as starting with the corresponding entity invocation."


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 = "http://example.com/foo#";
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" );
}


Producing:

<rdf:RDF
xmlns:eg="http://example.com/foo#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
<rdf:description about="http://example.com/foo#r">
42
</rdf:Description>
</rdf:RDF>



As I said, pretty simple.