xmlxml-parsingrdfredlandn-triples

Why does this raptor code parse NTriples but not RDFXML?


I've been trying for a while to parse some RDF/XML files with the raptor2 C library. This example code will read an NTriples file and print it, but not RDF/XML.

From the raptor tutorial:

#include <stdio.h>
#include <raptor2.h>

/* rdfcat.c: parse any RDF syntax and serialize to RDF/XML-Abbrev */

static raptor_serializer* rdf_serializer;

static void
serialize_triple(void* user_data, raptor_statement* triple) 
{
  raptor_serializer_serialize_statement(rdf_serializer, triple);
}

static void
declare_namespace(void* user_data, raptor_namespace *nspace)
{
  raptor_serializer_set_namespace_from_namespace(rdf_serializer, nspace);
}

int
main(int argc, char *argv[])
{
  raptor_world *world = NULL;
  raptor_parser* rdf_parser = NULL;
  unsigned char *uri_string;
  raptor_uri *uri, *base_uri;

  world = raptor_new_world();

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

  /* Ask raptor to work out which parser to use */
  rdf_parser = raptor_new_parser(world, "guess");
  raptor_parser_set_statement_handler(rdf_parser, NULL, serialize_triple);
  raptor_parser_set_namespace_handler(rdf_parser, NULL, declare_namespace);

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

  raptor_serializer_start_to_file_handle(rdf_serializer, base_uri, stdout);
  raptor_parser_parse_file(rdf_parser, uri, base_uri);
  raptor_serializer_serialize_end(rdf_serializer);

  raptor_free_serializer(rdf_serializer);
  raptor_free_parser(rdf_parser);

  raptor_free_uri(base_uri);
  raptor_free_uri(uri);
  raptor_free_memory(uri_string);

  raptor_free_world(world);

  return 0;
}

Here is an example from Wikipedia of the same RDF written in both formats. The first version prints (with a couple errors, but I don't think that's important), but the second one doesn't.

NTriples:

<http://www.w3.org/2001/sw/RDFCore/ntriples/> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Document> .
<http://www.w3.org/2001/sw/RDFCore/ntriples/> <http://purl.org/dc/terms/title> "N-Triples"@en-US .
<http://www.w3.org/2001/sw/RDFCore/ntriples/> <http://xmlns.com/foaf/0.1/maker> _:art .
<http://www.w3.org/2001/sw/RDFCore/ntriples/> <http://xmlns.com/foaf/0.1/maker> _:dave .

_:art <http://www.w3.org/1999/02/22-rdf-syntax-ns#> <http://xmlns.com/foaf/0.1/Person> .
_:art <http://xmlns.com/foaf/0.1/name> "Art Barstow".

_:dave <http://www.w3.org/1999/02/22-rdf-syntax-ns#> <http://xmlns.com/foaf/0.1/Person> .
_:dave <http://xmlns.com/foaf/0.1/name> "Dave Beckett".

RDFXML:

<rdf:RDF 
    xmlns="http://xmlns.com/foaf/0.1/"
    xmlns:dc="http://purl.org/dc/terms/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

    <Document rdf:about="http://www.w3.org/2001/sw/RDFCore/ntriples/">
        <dc:title xml:lang="en-US">N-Triples</dc:title>
        <maker>
            <Person rdf:nodeID="art">
                <name>Art Barstow</name>
            </Person>
        </maker>
        <maker>
            <Person rdf:nodeID="dave">
                <name>Dave Beckett</name>
            </Person>
        </maker>
    </Document>
</rdf:RDF>

Any ideas why? Thanks!

EDIT: The RDFXML one should be valid because it passes the W3C RDF Validator.

EDIT: Explicitly setting the parser to "rdfxml" doesn't help. I actually just found out about the guess option from this example and was excited because before I had been manually checking the extension and calling it with "ntriples" or "rdfxml".


Solution

  • It's still just guessing what the input format is - the parser is "guess":

      /* Ask raptor to work out which parser to use */
    

    Guesses can be wrong. Set the parser explicitly to "ntriples" or "rdfxml" and it won't guess, it'll parse exactly what you give it.