Skip to content

rdf prefix is not emitted when saving to turtle when only rdf:type is used #1649

@cmungall

Description

@cmungall

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

  • adding another node to the graph in the rdf namespace, other than rdf:type
  • saving to a format like nt

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 .

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions