Preventing graph union in rdflib

158 views Asked by At

Is there a way to prevent rdflib merging graphs ? I am creating two graphs in a single response which have a node in common, so rdf creates (because of the union):

<ore:aggregation rdf:about="http://example.org/obj/123">
  <edm:aggregatedCHO>
  <edm:ProvidedCHO rdf:about="#OBJ123">
    ...
  </edm:ProvidedCHO>
  </edm:aggregatedCHO>
</ore:aggregation>

which I would prefer instead to return as two top level graphs

<ore:aggregation rdf:about="http://example.org/obj/123">
  <edm:aggregatedCHO rdf:resource="#OBJ123/>
</ore:aggregation>

<edm:ProvidedCHO rdf:about="#OBJ123>
  ...
</edm:ProvidedCHO>

Is this possible ? I understand I can create them as part of a dataset which would prevent the union, but there doesn't seem to be a way to serialise the dataset out.

1

There are 1 answers

0
Richard On

Solved by passing max_depth to serialisation call:

g.serialize(format='pretty-xml', max_depth=1)

which avoids recursing through the whole graph in one and instead writes each top level subject node out individually in the document.