Using Raptor RDF Parser Toolkit to generate a FOAF rdfxml file

Vertexwahn Source

I want to write a C/C++ program using the Raptor RDF Parser Toolkit to generate the following output (checked with 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:[email protected]"/>
     <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>

The triples of the Data Model look like this:

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:[email protected]
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

Just for the records, I am using Visual Studio 2017 x64. I came up with the following code:

#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:[email protected]", 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);
}

The generated file looks like this:

<?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:[email protected]</foaf:mbox>
  </rdf:Description>
</rdf:RDF>

What am I doing wrong here? I want to produce the same result as shown above. Instead of a <foaf:Person> tag a <rdf:Description> tag is created.

The Turtle output (raptor_new_serializer(world, "turtle")) looks like this:

@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:[email protected]" ;
    foaf:name "Jimmy Wales" .
c++rdflinked-datafoafredland

Answers

answered 4 months ago Stanislav Kralin #1

There are three problems in your code.

  1. You're mixing up different kinds of RDF terms:

    • URIs and literals (lines 20, 42),
    • URIs and blank nodes (lines 18, 29, 40).
  2. In order to output typed nodes, you should use the rdfxml-abbrev serialization (line 7).

  3. You're confusing which things are connected, and in which way (lines 29, 40):

    • You need to construct this structure:

      as to be

    • Instead, you're constructing something like this:

      as is

This code works well:

#include <stdio.h>
#include <raptor/raptor2.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
  raptor_world *world = NULL;
  raptor_serializer* rdf_serializer = NULL;
  unsigned char *uri_string;
  raptor_uri *base_uri;
  raptor_statement* triple;

  world = raptor_new_world();

  uri_string = raptor_uri_filename_to_uri_string(argv[1]);
  base_uri = raptor_new_uri(world, uri_string);

  rdf_serializer = raptor_new_serializer(world, "rdfxml-abbrev");
  raptor_serializer_start_to_file_handle(rdf_serializer, base_uri, stdout);

  const unsigned char* foaf_prefix = (const unsigned char*)"foaf";
  raptor_uri* foaf_uri = raptor_new_uri(world, (const unsigned char*)"http://xmlns.com/foaf/0.1/");
  raptor_serializer_set_namespace(rdf_serializer, foaf_uri, foaf_prefix);

  const unsigned char* rdf_prefix = (const unsigned char*)"rdf";
  raptor_uri* rdf_uri = raptor_new_uri(world, (const unsigned char*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#");
  raptor_serializer_set_namespace(rdf_serializer, rdf_uri, rdf_prefix);

  {
    raptor_statement* triple = NULL; triple = raptor_new_statement(world);

    triple->subject = raptor_new_term_from_blank(world, (const unsigned char*)"b1");
    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", NULL, NULL);

    raptor_serializer_serialize_statement(rdf_serializer, triple); raptor_free_statement(triple);
  }

  {
    raptor_statement* triple = NULL; triple = raptor_new_statement(world);

    triple->subject = raptor_new_term_from_blank(world, (const unsigned char*)"b1");  
    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_uri_string(world, (unsigned char*)"mailto:[email protected]");

    raptor_serializer_serialize_statement(rdf_serializer, triple); raptor_free_statement(triple);
  }

  {
    raptor_statement* triple = NULL; triple = raptor_new_statement(world);

    triple->subject = raptor_new_term_from_blank (world, (const unsigned char*)"b1");  
    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_uri_string(world, (unsigned char*)"http://xmlns.com/foaf/0.1/Person");

    raptor_serializer_serialize_statement(rdf_serializer, triple); raptor_free_statement(triple);
  }

  raptor_serializer_serialize_end(rdf_serializer);
  raptor_free_serializer(rdf_serializer);
  raptor_free_uri(base_uri);
  raptor_free_memory(uri_string);
  raptor_free_world(world);
  return 0;
}

The output is:

<?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#">
  <foaf:Person>
    <foaf:mbox rdf:resource="mailto:[email protected]"/>
    <foaf:name>Jimmy Wales</foaf:name>
  </foaf:Person>
</rdf:RDF>

comments powered by Disqus