how to query Dbpedia in Javascript

风流意气都作罢 提交于 2019-12-05 18:38:15

I use a different approach for multiline strings, and i use it directly for SPARQL query writing against DBPedia.

var query = [
 "PREFIX dbpedia2: <http://dbpedia.org/resource/>",
 "PREFIX Abs: <http://dbpedia.org/ontology/>",
 "SELECT ?abstract",
 "WHERE {",
    "?s dbpedia2:Civil_engineeringe\"@en;",
    "Abs:abstract ?abstract",
 "}"
].join(" ");

I do it this way because that allows me to adjust the line separator if encoding issues arise, and also it allows me to easily comment lines out if necessary.

Now, once i need to run the query, i encode the query itself and append that to the URL.

Be careful with how you are wrapping the entire query string, because it might be encoding the keys, values, and equals sign as escaped characters.

I do it this way:

var queryUrl = url+"?query="+ encodeURIComponent(query) +"&format=json";
Spork

The encoding seems okay, but your original SPARQL / JavaScript does not look Okay to me.

var query = "\
 PREFIX dbpedia2: <http://dbpedia.org/resource/>\
 PREFIX Abs: <http://dbpedia.org/ontology/>\
 SELECT ?abstract\
 WHERE {\
         ?s dbpedia2:Civil_engineeringe\"@en;\ Abs:abstract ?abstract\ 
 }";

Does not result in a valid JavaScript string as there is a space after '?abstract\', meaning you're escaping a space character. Check out this question relating to multiline JavaScript strings: Creating multiline strings in JavaScript.

Moreover, the SPARQL query is plain wrong at the moment. Try to build it out and test it first here and take a look at the spec.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!