paradise
paradise

Reputation: 491

Jena nested properties

How could I generate by code nested properties please? Like that:

<geo:Departement rdf:about="DEP_05">
    <geo:code_departement>05</geo:code_departement>
    <geo:subdivision>
        <geo:Arrondissement rdf:about="ARR_051">
            <geo:code_arrondissement>051</geo:code_arrondissement>
            <geo:nom xml:lang="fr">Briançon</geo:nom>
        </geo:Arrondissement>
    </geo:subdivision>
    <geo:subdivision>
        <geo:Arrondissement rdf:about="ARR_052">
            <geo:code_arrondissement>052</geo:code_arrondissement>
            <geo:nom xml:lang="fr">Gap</geo:nom>
        </geo:Arrondissement>
    </geo:subdivision>
</geo:Departement>

All my properties (created using "createProperty") are at the same level.

Upvotes: 1

Views: 518

Answers (1)

Joshua Taylor
Joshua Taylor

Reputation: 85843

It's not clear exactly what you mean by subproperties. It's important to realize that RDF is a graph-based data representation based on labeled, directed, edges called triples of the form:

subject predicate object

Your data for instance, includes the triples

DEP_05 rdf:type                 geo:Department
DEP_05 geo:code_department      "05"
DEP_05 subdivision              ARR_O51
ARR_051 rdf:type                geo:Arrondissement 
ARR_051 geo:code_arrondissement "051"

RDF/XML provides lots of different ways to write the same graph. For instance, one way of writing your data (I've added the appropriate prefixes) is:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:geo="https://stackoverflow.com/q/21383685/1281433/">
  <geo:Departement rdf:about="https://stackoverflow.com/q/21383685/1281433/DEP_05">
    <geo:code_departement>05</geo:code_departement>
    <geo:subdivision>
      <geo:Arrondissement rdf:about="https://stackoverflow.com/q/21383685/1281433/ARR_051">
        <geo:code_arrondissement>051</geo:code_arrondissement>
        <geo:nom xml:lang="fr">Briançon</geo:nom>
      </geo:Arrondissement>
    </geo:subdivision>
    <geo:subdivision>
      <geo:Arrondissement rdf:about="https://stackoverflow.com/q/21383685/1281433/ARR_052">
        <geo:code_arrondissement>052</geo:code_arrondissement>
        <geo:nom xml:lang="fr">Gap</geo:nom>
      </geo:Arrondissement>
    </geo:subdivision>
  </geo:Departement>
</rdf:RDF>

Another way, which happens to use fewer of the "shortcuts" that RDF/XML allows, is:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:geo="https://stackoverflow.com/q/21383685/1281433/" > 
  <rdf:Description rdf:about="https://stackoverflow.com/q/21383685/1281433/ARR_052">
    <rdf:type rdf:resource="https://stackoverflow.com/q/21383685/1281433/Arrondissement"/>
    <geo:code_arrondissement>052</geo:code_arrondissement>
    <geo:nom xml:lang="fr">Gap</geo:nom>
  </rdf:Description>
  <rdf:Description rdf:about="https://stackoverflow.com/q/21383685/1281433/ARR_051">
    <rdf:type rdf:resource="https://stackoverflow.com/q/21383685/1281433/Arrondissement"/>
    <geo:code_arrondissement>051</geo:code_arrondissement>
    <geo:nom xml:lang="fr">Briançon</geo:nom>
  </rdf:Description>
  <rdf:Description rdf:about="https://stackoverflow.com/q/21383685/1281433/DEP_05">
    <rdf:type rdf:resource="https://stackoverflow.com/q/21383685/1281433/Departement"/>
    <geo:code_departement>05</geo:code_departement>
    <geo:subdivision rdf:resource="https://stackoverflow.com/q/21383685/1281433/ARR_051"/>
    <geo:subdivision rdf:resource="https://stackoverflow.com/q/21383685/1281433/ARR_052"/>
  </rdf:Description>
</rdf:RDF>

Even though these are different XML documents, they are serializations of the same RDF graph. There are non-XML serializations as well. In the Turtle serialization, your data is:

@prefix geo:   <https://stackoverflow.com/q/21383685/1281433/> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

geo:ARR_051  a                   geo:Arrondissement ;
        geo:code_arrondissement  "051" ;
        geo:nom                  "Briançon"@fr .

geo:DEP_05  a                 geo:Departement ;
        geo:code_departement  "05" ;
        geo:subdivision       geo:ARR_051 , geo:ARR_052 .

geo:ARR_052  a                   geo:Arrondissement ;
        geo:code_arrondissement  "052" ;
        geo:nom                  "Gap"@fr .

It's very important to realize that these are all the same graph. An RDF processing tool doesn't care which one you use. That said, it can be nice to use a format that's more human-readable, so I tend to prefer Turtle. If you still need to use RDF/XML, then you may way to serialize your model using the "RDF/XML-ABBREV" language. To do this, specify "RDF/XML-ABBREV" as the lang argument to Model.write(OutputStream out, String lang). From the Javadoc:

write

Model write(OutputStream out, String lang)

Write a serialized represention of this model in a specified language.

The language in which to write the model is specified by the lang argument. Predefined values are "RDF/XML", "RDF/XML-ABBREV", "N-TRIPLE", "TURTLE", (and "TTL") and "N3". The default value, represented by null, is "RDF/XML".

Parameters:

  • out - The output stream to which the RDF is written
  • lang - The output language

Returns:

  • This model

Related Questions

Some related questions have arisen here before, and you might find some of them helpful. The ones that I quickly found are:

Upvotes: 3

Related Questions