1

Context

I am loading a rdf file in rdflib, and am trying to export it in json-ld.

The original rdf looks like:

<cim:Substation rdf:ID="_1234">
    <cim:IdentifiedObject.name>A substation</cim:IdentifiedObject.name>
</cim:Substation>

The exported json-lib looks like:

{
  "@id": "file:///path/to/original/xml#_418866779",
  "@type": "cim:Substation",
  "cim:IdentifiedObject.name": "A substation"
},

I cannot seem to override the file:///path/to/original/xml component in json.

Code

This is what I am doing:

from rdflib import Graph

g = Graph(
    # Giving an identifier or base does not seem to change anything
)
g.parse('path to rdf')
js = g.serialize(
    format="json-ld",
    encoding="utf-8",
    destination='path to json',
    sort_keys=True,  # repeatable output is Good.
    context={ some relevant entries (rdf, cim)  },
    auto_compact=True,
    # giving base does not seem to change anything
)

Question How can I set/override the first component of the ID in the exported json-ld?

Guillaume
  • 2,325
  • 2
  • 22
  • 40
  • Based on the [RDFLib code for Graph.serialize](https://rdflib.readthedocs.io/en/stable/_modules/rdflib/graph.html#Graph.serialize), could you play with the `destination` parameter? Take a look at the part starting with `location = cast(str, destination)`. – Jarno Lamberg May 11 '23 at 08:09
  • Destination is unfortunately only where to write the result. I give there a PurePath. – Guillaume May 11 '23 at 11:56
  • Can you modify the RDF/XML and add `xml:base` to the root element? – IS4 May 12 '23 at 23:58

1 Answers1

1

The solution was in the parse method, with the parameter publicID which is decribed in the source.

the logical URI to use as the document base. If None specified the document location is used (at least in the case where there is a document location).

So my example in the question becomes

g.parse('path to rdf', publicID="#")

and the rest stays as is.

Guillaume
  • 2,325
  • 2
  • 22
  • 40