Doro
Doro

Reputation: 355

Jena - How to link a resource to a property?

I am working at a framework for implementing Europeana Data Model (EDM) for a particular data structure and I have an issue when I want to add a property. For example, considering the following simple example (using Apache Jena 3.9):

public static void main(String[] args) {
    String NS = "http://my.com/";
    Model model = ModelFactory.createDefaultModel();
    model.setNsPrefix("edm", EDM_TEST.getURI());
    model.setNsPrefix("skos", SKOS.getURI());

    Resource repository = model.createResource(NS + "testing_agent");
    repository.addProperty(RDF.type, EDM_TEST.Agent);
    repository.addProperty(SKOS.altLabel, model.createLiteral("Vasile Alecsandri Museum"));
    repository.addProperty(SKOS.prefLabel, model.createLiteral("Vasile Alecsandri National Museum"));

    Resource providedCHO = model.createResource(NS + "testing_cho");
    providedCHO.addProperty(RDF.type, EDM_TEST.ProvidedCHO);
    providedCHO.addProperty(EDM_TEST.currentLocation, repository);

    StringWriter out = new StringWriter();
    model.write(out, "RDF/XML");
    String result = out.toString();
    System.out.println(result);
}

The result it seems to be ok:

<edm:ProvidedCHO rdf:about="http://my.com/testing_cho">
    <edm:currentLocation>
      <edm:Agent rdf:about="http://my.com/testing_agent">
        <skos:prefLabel>Vasile Alecsandri National Museum</skos:prefLabel>
        <skos:altLabel>Vasile Alecsandri Museum</skos:altLabel>
      </edm:Agent>
    </edm:currentLocation>
</edm:ProvidedCHO>

but it isn't ok, because EDM does not allow an inner object for currentLocation property. So, I need to generate the following output for currentLocation property:

<edm:ProvidedCHO rdf:about="http://my.com/testing_cho">
    <edm:currentLocation rdf:resource="http://my.com/testing_agent"/>
</edm:ProvidedCHO>
<edm:Agent rdf:about="http://my.com/testing_agent">
    <skos:prefLabel>Vasile Alecsandri National Museum</skos:prefLabel>
    <skos:altLabel>Vasile Alecsandri Museum</skos:altLabel>
</edm:Agent>

How can I separately create the repository resource (the Agent) and link to currentLocation property from providedCHO resource as I explained above?

Upvotes: 1

Views: 589

Answers (1)

AndyS
AndyS

Reputation: 16680

The difference between Jena 3.0.1 and 3.9.0 is that the default choice of RDF/XMl writer changed from plain to pretty.

More control of the choice of format details is available with

RDFDataMgr.write(..,..,RDFFormat.RDFXML_ABBREV)

RDFDataMgr.write(..,..,RDFFormat.RDFXML_PLAIN)

Even more control is available with:

http://jena.staging.apache.org/documentation/io/rdfxml_howto.html#advanced-rdfxml-output

To create a very particular XML schema, you may need to get the data out and then run XSLT.

Upvotes: 1

Related Questions