我想使用Raptor RDF Parser Toolkit编写一个 C/C++ 程序来生成以下输出(使用RDF Validator检查):
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:foaf="http://xmlns.com/foaf/0.1/">
<foaf:Person xml:lang="en">
<foaf:name>Jimmy Wales</foaf:name>
<foaf:mbox rdf:resource="mailto:jwales@bomis.com"/>
<foaf:nick>Jimbo</foaf:nick>
<!-- photo -->
<foaf:depiction
rdf:resource="http://upload.wikimedia.org/wikipedia/commons/1/19/Jimbo_Wales_in_France_cropped.jpg" />
</foaf:Person>
</rdf:RDF>
数据模型的三元组如下所示:
Number Subject Predicate Object
1 genid:A4486 http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://xmlns.com/foaf/0.1/Person
2 genid:A4486 http://xmlns.com/foaf/0.1/name "Jimmy Wales"@en
3 genid:A4486 http://xmlns.com/foaf/0.1/mbox mailto:jwales@bomis.com
4 genid:A4486 http://xmlns.com/foaf/0.1/nick "Jimbo"@en
5 genid:A4486 http://xmlns.com/foaf/0.1/depiction http://upload.wikimedia.org/wikipedia/commons/1/19/Jimbo_Wales_in_France_cropped.jpg
仅作记录,我使用的是Visual Studio 2017 x64。我想出了以下代码:
#include "raptor2/raptor2.h"
int main(int argc, char* argv[]) {
FILE* outfile = fopen("myTestfile.rdf", "w");
raptor_world* world = raptor_new_world();
rdf_serializer = raptor_new_serializer(world, "rdfxml" /* "turtle" */);
raptor_serializer_start_to_file_handle(rdf_serializer, nullptr, outfile);
const unsigned char* prefix = (const unsigned char*)"foaf";
raptor_uri* uri = raptor_new_uri(world, (const unsigned char*)"http://xmlns.com/foaf/0.1/");
raptor_serializer_set_namespace(rdf_serializer, uri, prefix);
{
raptor_statement* triple = nullptr;
triple = raptor_new_statement(world);
triple->subject = raptor_new_term_from_uri_string(world, (const unsigned char*)"genid:A4486");
triple->predicate = raptor_new_term_from_uri_string(world, (const unsigned char*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
triple->object = raptor_new_term_from_literal(world, (unsigned char*)"http://xmlns.com/foaf/0.1/Person", nullptr, nullptr);
raptor_serializer_serialize_statement(rdf_serializer, triple);
raptor_free_statement(triple);
}
{
raptor_statement* triple = nullptr;
triple = raptor_new_statement(world);
triple->subject = raptor_new_term_from_uri_string(world, (const unsigned char*)"http://xmlns.com/foaf/0.1/Person");
triple->predicate = raptor_new_term_from_uri_string(world, (const unsigned char*)"http://xmlns.com/foaf/0.1/name");
triple->object = raptor_new_term_from_literal(world, (unsigned char*)"Jimmy Wales", nullptr, nullptr);
raptor_serializer_serialize_statement(rdf_serializer, triple);
raptor_free_statement(triple);
}
{
raptor_statement* triple = nullptr;
triple = raptor_new_statement(world);
triple->subject = raptor_new_term_from_uri_string(world, (const unsigned char*)"http://xmlns.com/foaf/0.1/Person");
triple->predicate = raptor_new_term_from_uri_string(world, (const unsigned char*)"http://xmlns.com/foaf/0.1/mbox");
triple->object = raptor_new_term_from_literal(world, (unsigned char*)"mailto:jwales@bomis.com", nullptr, nullptr);
raptor_serializer_serialize_statement(rdf_serializer, triple);
raptor_free_statement(triple);
}
raptor_serializer_serialize_end(rdf_serializer);
raptor_free_serializer(rdf_serializer);
raptor_free_world(world);
fclose(outfile);
}
生成的文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="genid:A4486">
<rdf:type>http://xmlns.com/foaf/0.1/Person</rdf:type>
</rdf:Description>
<rdf:Description rdf:about="http://xmlns.com/foaf/0.1/Person">
<foaf:name>Jimmy Wales</foaf:name>
</rdf:Description>
<rdf:Description rdf:about="http://xmlns.com/foaf/0.1/Person">
<foaf:mbox>mailto:jwales@bomis.com</foaf:mbox>
</rdf:Description>
</rdf:RDF>
我在这里做错了什么?我想产生与上图相同的结果。创建标签而不是<foaf:Person>
标签<rdf:Description>
。
Turtle 输出 ( raptor_new_serializer(world, "turtle")
) 如下所示:
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<genid:A4486>
a "http://xmlns.com/foaf/0.1/Person" .
foaf:Person
foaf:mbox "mailto:jwales@bomis.com" ;
foaf:name "Jimmy Wales" .