英文:
How to set up the ID of a json element in rdflib
问题
{
"@id": "_1234",
"@type": "cim:Substation",
"cim:IdentifiedObject.name": "A substation"
}
英文:
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?
答案1
得分: 1
解决方案在parse
方法中,使用参数publicID
,该参数在源代码中有描述。
> 用作文档基础的逻辑URI。如果未指定,则使用文档位置(至少在存在文档位置的情况下)。
因此,我在问题中的示例变为
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
g.parse('path to rdf', publicID="#")
and the rest stays as is.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论