Rdflib.js,如何将数据序列化为turtle(.ttl)格式?
Rdflib.js, how to serialize the data into turtle (.ttl) format?
如何使用 rdflib.js 在 turtle 中序列化 RDF?没有太多的文档。我可以使用:
Serializer.statementsToN3(destination);
序列化为 N3 格式,但除此之外没有太多。我已经尝试将上述命令更改为 statementsToTtl/Turtle/TURTLE/TTL 之类的内容,但似乎没有任何效果。
想通了。由 this (secret) Github gist.
提供
$rdf.serialize(undefined, source, undefined,` 'text/turtle', function(err, str){
// do whatever you want, the data is in the str variable.
})
这是上述 Github 要点中的代码。
/**
* rdflib.js with node.js -- basic RDF API example.
* @author ckristo
*/
var fs = require('fs');
var $rdf = require('rdflib');
FOAF = $rdf.Namespace('http://xmlns.com/foaf/0.1/');
XSD = $rdf.Namespace('http://www.w3.org/2001/XMLSchema#');
// - create an empty store
var kb = new $rdf.IndexedFormula();
// - load RDF file
fs.readFile('foaf.rdf', function (err, data) {
if (err) { /* error handling */ }
// NOTE: to get rdflib.js' RDF/XML parser to work with node.js,
// see https://github.com/linkeddata/rdflib.js/issues/47
// - parse RDF/XML file
$rdf.parse(data.toString(), kb, 'foaf.rdf', 'application/rdf+xml', function(err, kb) {
if (err) { /* error handling */ }
var me = kb.sym('http://kindl.io/christoph/foaf.rdf#me');
// - add new properties
kb.add(me, FOAF('mbox'), kb.sym('mailto:e0828633@student.tuwien.ac.at'));
kb.add(me, FOAF('nick'), 'ckristo');
// - alter existing statement
kb.removeMany(me, FOAF('age'));
kb.add(me, FOAF('age'), kb.literal(25, null, XSD('integer')));
// - find some existing statements and iterate over them
var statements = kb.statementsMatching(me, FOAF('mbox'));
statements.forEach(function(statement) {
console.log(statement.object.uri);
});
// - delete some statements
kb.removeMany(me, FOAF('mbox'));
// - print modified RDF document
$rdf.serialize(undefined, kb, undefined, 'application/rdf+xml', function(err, str) {
console.log(str);
});
});
});
如何使用 rdflib.js 在 turtle 中序列化 RDF?没有太多的文档。我可以使用:
Serializer.statementsToN3(destination);
序列化为 N3 格式,但除此之外没有太多。我已经尝试将上述命令更改为 statementsToTtl/Turtle/TURTLE/TTL 之类的内容,但似乎没有任何效果。
想通了。由 this (secret) Github gist.
提供$rdf.serialize(undefined, source, undefined,` 'text/turtle', function(err, str){
// do whatever you want, the data is in the str variable.
})
这是上述 Github 要点中的代码。
/**
* rdflib.js with node.js -- basic RDF API example.
* @author ckristo
*/
var fs = require('fs');
var $rdf = require('rdflib');
FOAF = $rdf.Namespace('http://xmlns.com/foaf/0.1/');
XSD = $rdf.Namespace('http://www.w3.org/2001/XMLSchema#');
// - create an empty store
var kb = new $rdf.IndexedFormula();
// - load RDF file
fs.readFile('foaf.rdf', function (err, data) {
if (err) { /* error handling */ }
// NOTE: to get rdflib.js' RDF/XML parser to work with node.js,
// see https://github.com/linkeddata/rdflib.js/issues/47
// - parse RDF/XML file
$rdf.parse(data.toString(), kb, 'foaf.rdf', 'application/rdf+xml', function(err, kb) {
if (err) { /* error handling */ }
var me = kb.sym('http://kindl.io/christoph/foaf.rdf#me');
// - add new properties
kb.add(me, FOAF('mbox'), kb.sym('mailto:e0828633@student.tuwien.ac.at'));
kb.add(me, FOAF('nick'), 'ckristo');
// - alter existing statement
kb.removeMany(me, FOAF('age'));
kb.add(me, FOAF('age'), kb.literal(25, null, XSD('integer')));
// - find some existing statements and iterate over them
var statements = kb.statementsMatching(me, FOAF('mbox'));
statements.forEach(function(statement) {
console.log(statement.object.uri);
});
// - delete some statements
kb.removeMany(me, FOAF('mbox'));
// - print modified RDF document
$rdf.serialize(undefined, kb, undefined, 'application/rdf+xml', function(err, str) {
console.log(str);
});
});
});