如何在rdflib中设置JSON元素的ID

huangapple go评论95阅读模式
英文:

How to set up the ID of a json element in rdflib

问题

  1. {
  2. "@id": "_1234",
  3. "@type": "cim:Substation",
  4. "cim:IdentifiedObject.name": "A substation"
  5. }
英文:

Context

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

The original rdf looks like:

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

The exported json-lib looks like:

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

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

Code

This is what I am doing:

  1. from rdflib import Graph
  2. g = Graph(
  3. # Giving an identifier or base does not seem to change anything
  4. )
  5. g.parse('path to rdf')
  6. js = g.serialize(
  7. format="json-ld",
  8. encoding="utf-8",
  9. destination='path to json',
  10. sort_keys=True, # repeatable output is Good.
  11. context={ some relevant entries (rdf, cim) },
  12. auto_compact=True,
  13. # giving base does not seem to change anything
  14. )

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

答案1

得分: 1

解决方案在parse方法中,使用参数publicID,该参数在源代码中有描述。

> 用作文档基础的逻辑URI。如果未指定,则使用文档位置(至少在存在文档位置的情况下)。

因此,我在问题中的示例变为

  1. g.parse('rdf路径', publicID="#")

其余部分保持不变。

英文:

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

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

and the rest stays as is.

huangapple
  • 本文由 发表于 2023年5月11日 15:27:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定