简体   繁体   English

将RDF / XML文件转换或转换为JSON-LD格式

[英]Convert or translate a RDF/XML file to JSON-LD format

I am trying to parse a RDF/XML formatted document into JSON-LD in order to frame it. 我正在尝试将RDF / XML格式的文档解析为JSON-LD,以便对其进行构架。 All using Node.js and not utilizing any web service APIs (a not too uncommon solution). 全部使用Node.js而不使用任何Web服务API(一种不太常见的解决方案)。

I feel that I am almost there, but my current approach feels clumsy to say the least. 我觉得自己快到了,但是至少可以说,我目前的做法显得笨拙。 Putting the graph into a rdflib store and then querying it back up again gives me a strange response with some headers and no real context within the graph. 将图放入rdflib存储,然后再次查询它,这给了我一个奇怪的响应,其中包含一些标头,并且图内没有实际上下文。 Hence the doc[5]['@graph'] stuff in the middle. 因此doc[5]['@graph']放在中间。

var fs = require('fs')
var $rdf = require('rdflib')
var jsonld = require('jsonld')

var path = 'path_to_rdf_file'

const frame = {}

fs.readFile(path, 'utf8', function (err, data) {
    var uri = 'https://some.other.uri'
    var store = $rdf.graph()
    $rdf.parse(data, store, uri, 'application/rdf+xml')
    var a = $rdf.serialize(null, store, uri, 'application/n-quads')
    jsonld.fromRDF(a, { format: 'application/n-quads' }, (err, doc) => {
        jsonld.flatten(doc[5]['@graph'], (err, flattened) => {
            console.log(flattened)
            jsonld.frame(flattened, frame, (err, framed) => {
                resolve(framed)
            })
        })
    })
})

With all the RDF and linked data packages floating around npm, I reckon there must be a simpler solution out there that could get me from A to B. 由于所有RDF和链接数据包都在npm左右浮动,因此我认为必须有一个更简单的解决方案可以使我从A升级到B。

How could I parse my RDF/XML document into a JSON-LD document without using rdflib this way? 在不使用rdflib的情况下,如何将RDF / XML文档解析为JSON-LD文档?

You can use rdflib to serialize to application/ld+json directly (rdflib uses the jsonld module internally). 您可以使用rdflib直接序列化到application/ld+json (rdflib在内部使用jsonld模块)。

var fs = require('fs')
var $rdf = require('rdflib')
var jsonld = require('jsonld')

var path = 'path_to_rdf_file'

const frame = {}

const toJSONLD = (data, uri, mimeType) => {
    return new Promise((resolve, reject) => {
        var store = $rdf.graph()
        $rdf.parse(data, store, uri, mimeType)
        $rdf.serialize(null, store, uri, 'application/ld+json', (err, jsonldData) => {
            if (err) return reject(err);
            resolve(JSON.parse(jsonldData))
        })
    })
}

fs.readFile(path, 'utf8', function (err, data) {
    var uri = 'https://some.other.uri'
    toJSONLD(data, uri, 'application/rdf+xml')
        .then((doc) => {
            jsonld.flatten(doc[5]['@graph'], (err, flattened) => {
                console.log(flattened)
                jsonld.frame(flattened, frame, (err, framed) => {
                    resolve(framed)
                })
            })
        })
})

Another way would be to equip jsonld with custom parsers for your data type using jsonld.regiserRDFParser ( https://www.npmjs.com/package/jsonld#custom-rdf-parser ). 另一种方法是使用jsonld.regiserRDFParserhttps://www.npmjs.com/package/jsonld#custom-rdf-parser )为jsonld配备用于您的数据类型的自定义解析器。 Allthough you would likely use rdflib for this task as well. 尽管您可能也将rdflib用于此任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM