linkeddata / rdflib.js

Linked Data API for JavaScript
http://linkeddata.github.io/rdflib.js/doc/
Other
566 stars 146 forks source link

RDF/XML syntax error No namespace for rdf:about #47

Open nicola opened 10 years ago

nicola commented 10 years ago

I have been trying with 5 different files and this code:

request('http://schema.rdfs.org/all.rdf', function(err, response, body) {
    var kb = new $rdf.IndexedFormula();
    $rdf.parse(body, kb, 'http://schema.rdfs.org/all.rdf', 'application/rdf+xml');
    console.log(kb)
  });

but I get:

Error trying to parse <http://schema.rdfs.org/all.rdf> as application/rdf+xml:
Error: RDF/XML syntax error: No namespace for rdf:about in http://schema.rdfs.org/all.rdf:
Error: RDF/XML syntax error: No namespace for rdf:about in http://schema.rdfs.org/all.rdf
    at null.<anonymous> (/Users/mozilla/Proj/schemas/node_modules/rdflib/dist/rdflib.js:1698:23)
    at parseDOM (/Users/mozilla/Proj/schemas/node_modules/rdflib/dist/rdflib.js:1828:38)
    at parse (/Users/mozilla/Proj/schemas/node_modules/rdflib/dist/rdflib.js:1688:14)
    at Object.parse (/Users/mozilla/Proj/schemas/node_modules/rdflib/dist/rdflib.js:8221:20)
    at Request._callback (/Users/mozilla/Proj/schemas/index.js:16:10)
    at Request.self.callback (/Users/mozilla/Proj/schemas/node_modules/request/request.js:372:22)
    at Request.emit (events.js:98:17)
    at Request.<anonymous> (/Users/mozilla/Proj/schemas/node_modules/request/request.js:1317:14)
    at Request.emit (events.js:117:20)
    at IncomingMessage.<anonymous> (/Users/mozilla/Proj/schemas/node_modules/request/request.js:1265:12)

Am I parsing the right way?

melvincarvalho commented 10 years ago

I dont think schema.org serves application/rdf+xml right now, so the XML parser cant get the RDF from that serialization

nicola commented 10 years ago

I am so sorry I meant: http://schema.rdfs.org/all.rdf (I just updated the example for clarity!).

I think they do, but it might be not the right format!

melvincarvalho commented 10 years ago

Thanks for the clarification.

It's been a while since I used RDF/XML. However unless I'm mistaken schema.rdfs.org ought to contain the namespace at the top, something like:

<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"

However it validates both in rapper and the w3c validator

W3C validator:

http://www.w3.org/RDF/Validator/rdfval?URI=http%3A%2F%2Fschema.rdfs.org%2Fall.rdf+&PARSE=Parse+URI%3A+&TRIPLES_AND_GRAPH=PRINT_TRIPLES&FORMAT=PNG_EMBED

Rapper:

rapper -g http://schema.rdfs.org/all.rdf

So maybe somehow it knows that rdf links to "http://www.w3.org/1999/02/22-rdf-syntax-ns# ... I asked on #swig and the response was that the namespace should be included.

ckristo commented 9 years ago

Hi, same problem here when trying to load a local RDF/XML file -- rapper parses it without complaints; @nicola did you solve the problem somehow?

var fetch = $rdf.fetcher(kb);
fetch.nowOrWhenFetched('file:foaf.rdf', undefined, function(ok, body, xhr) {
    console.log(ok);
    console.log(body);
    console.log(xhr);
});
ckristo commented 9 years ago

Ok, seems to be a problem with the use of jsdom, replaced jsdom with xmldom in util.js and the parser seems to work fine:

$rdf.Util.parseXML = function(str) {
    var dparser;
    if ((typeof tabulator != 'undefined' && tabulator.isExtension)) {
        dparser = Components.classes["@mozilla.org/xmlextras/domparser;1"].getService(
                    Components.interfaces.nsIDOMParser);
    } else if (typeof module != 'undefined' && module && module.exports){ // Node.js
        //var libxmljs = require('libxmljs'); // Was jsdom before 2012-01 then libxmljs but that nonstandard
        //return libxmljs.parseXmlString(str);
        //var jsdom = require('jsdom');
        //var dom = jsdom.jsdom(str, undefined, {} );// html, level, options
        //return dom
        var DOMParser = require('xmldom').DOMParser;
        var dom = new DOMParser().parseFromString(str,'text/xml')
        return dom
    } else {
        dparser = new DOMParser();
    }
    return dparser.parseFromString(str, 'application/xml');
};

This shouldn't be a problem for RDF/XML, but as far as I saw, the function parseXML was also used to parse RDFa in the fetcher. Due to the fact that RDFa can be used with HTML (that must not be valid XML), I think this needs further work. Maybe just use jsdom again for parsing when the fetcher detects RDFa?

ckristo commented 9 years ago

somehow also related: #67

ckristo commented 9 years ago

A patched version that makes use of xmldom can be found here: https://github.com/ckristo/rdflib.js/tree/xmldom

ckristo commented 9 years ago

Tried once more to use jsdom for parsing, but with no luck. jsdom does assign wrong namespaces (e.g. for the rdf:RDF element it assigns some HTML namespace instead of the RDF one).

Anyhow, jsdom is a library for parsing HTML, so maybe its not a bad choice to introduce xmldom for parsing RDF/XML data.

jamsden commented 9 years ago

I tried the patched version above, but still get the error. Perhaps I don't understand what the base URI parameter should be. Here's what I'm doing: rskb = new rdflib.IndexedFormula(); rdflib.parse(rdfSource, rskb, 'https://oslclnx2.rtp.raleigh.ibm.com:9443/ccm/rootservices', 'application/rdf+xml'); ... attempting to read an RTC rootservices document. Here's the first few lines of that document: <?xml version="1.0"?>

<rdf:Description xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/terms/" xmlns:jfs="http://jazz.net/xmlns/prod/jazz/jfs/1.0/" xmlns:jd="http://jazz.net/xmlns/prod/jazz/discovery/1.0/" xmlns:jdb="http://jazz.net/xmlns/prod/jazz/dashboard/1.0/" xmlns:jp06="http://jazz.net/xmlns/prod/jazz/process/0.6/" xmlns:jp="http://jazz.net/xmlns/prod/jazz/process/1.0/" xmlns:jtp="http://jazz.net/xmlns/prod/jazz/jtp/0.6/" xmlns:ju="http://jazz.net/ns/ui#" xmlns:oslc="http://open-services.net/ns/core#" xmlns:trs="http://jazz.net/ns/trs#" xmlns:trs2="http://open-services.net/ns/core/trs#" rdf:about="https://oslclnx2.rtp.raleigh.ibm.com:9443/ccm/rootservices">

As you can see, rdf:about does have a namespace. Am I using the correct base parameter?

jamsden commented 9 years ago

OK, I have it parsing OK now.

ckristo commented 9 years ago

@jamsden Any problems with my patch I should fix?

ckristo commented 9 years ago

btw: rdf-ext uses a fork of rdflib.js' RDF/XML Parser that also uses xmldom with node.js, see https://github.com/zazukoians/rdf-ext/blob/master/lib/rdfxml-parser.js and https://github.com/zazukoians/rdf-ext/blob/master/lib/utils-node.js