rdf prefix is not emitted when saving to turtle when only rdf:type is used · Issue #1649 · RDFLib/rdflib (original) (raw)

whenusing rdflib 6.1.1 to convert the following jsonld to turtle:

{ "slots": [ { "name": "type", "slot_uri": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" } ], "@context": [ { "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "@vocab": "https://w3id.org/linkml/", "slot_uri": { "@type": "@id" } } ] }

the turtle emitted is:

@prefix : https://w3id.org/linkml/ .

[] :slots [ :name "type" ; :slot_uri rdf:type ] .

which is invalid, as it lacks a prefix declaration for rdf

The situation is resolved by either

This reproduces the error:

JSONLD = """ { "slots": [ { "name": "type", "slot_uri": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" } ], "@context": [ { "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "@vocab": "https://w3id.org/linkml/", "slot_uri": { "@type": "@id" } } ] }"""

from rdflib import Graph

graph = Graph() graph.parse(data=JSONLD, format="json-ld", prefix=False) ttl_str = graph.serialize(format='turtle') print(ttl_str) graph.parse(data=ttl_str, format="turtle")

This is in contrast to other libs, e.g. Jena, which will produce valid turtle:

@prefix rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns# .

_:b0 https://w3id.org/linkml/slots _:b1 .

_:b1 https://w3id.org/linkml/name "type" ; https://w3id.org/linkml/slot_uri rdf:type .