JavaScript implementation associated data (linked) query and attention to detail _javascript skills

Source: Internet
Author: User
Preface
The free encyclopedia should not only be free to write, but should be freely available.
DBpedia the Wikipedia data into linked, making it possible for machines to read and freely access the data.
The main purpose of this article is to use JavaScript to get the data we want from DBpedia.
For linked data, please refer to: Getting Started--RDF.

SPARQL
Trying to use the semantic Web without SPARQL are like trying to use a relational database without SQL.
--tim Berners-lee
SPARQL is the SQL of the semantic Web (semantic network), the language used for data querying.

SPARQL Endpoint
SPARQL Query terminal, is an HTTP binding protocol for SPARQL queries through HTTP, and returns the corresponding data.
DBpedia's SPARQL endpoint address is: HTTP://DBPEDIA.ORG/SPARQL
We can open this page through the browser, for SPARQL inquiries (preferably over the wall, did not turn the wall query often failed, do not understand why = =).
But this query eventually returns the result is an HTML page, not what we want, and we can specify the return data type by setting the Accept property of the request header.
For example, if specified as: Text/xml, then the returned RDF format data.
So how do we enter SPARQL query code?
Simply pass the code through a Get or POST method with parameter query. For example:
If you want to query: SELECT DISTINCT? Concept where {[] a? Concept} LIMIT 100
You can use the link to get the data:
Http://dbpedia.org/sparql?query=select%20distinct%20?concept%20where%20{[]%20a%20? concept}%20limit%20100
Where the spaces are turned into%20.

Implementation Details
• Cross-domain
We can do this with Ajax, but Ajax is not cross-domain in some browsers, but it's clear that the linked data we want is almost always cross-domain.
In fact, in some older versions of the browser, we do not have to change its data form in the front-end dynamic Cross-domain asynchronous read.
However, we can solve cross-domain problems through the server proxy approach.
Get or POST
How do I post with Get?
This may be considered in many ways, but given that get may be cached, we use post to prevent data from being cached.
• Return data in what form
We talked about using Text/xml to return RDF data, but RDF doesn't work well in JavaScript, so we return with JSON, which means we need to set accept to Application/sparql-results+json.

Implement
Interface Reference Python's SPARQL wrapper
Copy Code code as follows:

(function (root, factory) {
if (typeof define = = "function") {
Define ("Sparqlwrapper", factory); AMD | | Cmd
}else{
Root. Sparqlwrapper = Factory (); <script>
}
} (this, function () {
' Use strict '
function Sparqlwrapper (endpoint) {
This.endpoint = endpoint;
This.querypart = "";
This.type = "JSON";
}
Sparqlwrapper.prototype = {
Constructor:sparqlwrapper,
Setquery:function (query) {
This.querypart = "query=" + encodeuri (query);
},
Settype:function (type) {
This.type = Type.tolowercase ();
},
Query:function (Type, callback) {
Callback = callback = = undefined? Type:this.setType (type) | | Callback
var xhr = new XMLHttpRequest ();
Xhr.open (' POST ', this.endpoint, true);
Xhr.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded ');
Switch (this.type) {
Case "JSON":
Type = "Application/sparql-results+json";
Break
Case "XML":
Type = "Text/xml";
Break
Case "HTML":
Type = "Text/html";
Break
Default
Type = "Application/sparql-results+json";
Break
}
Xhr.setrequestheader ("Accept", type);
Xhr.onreadystatechange = function () {
if (xhr.readystate = = 4) {
var sta = xhr.status;
if (sta = = | | sta = = 304) {
Callback (Xhr.responsetext);
}else{
Console && console.error ("SPARQL Query Error:" + Xhr.status + "" + Xhr.responsetext);
}
Window.settimeout (function () {
Xhr.onreadystatechange= new Function ();
XHR = null;
},0);
}
}
Xhr.send (This.querypart);
}
}
return sparqlwrapper;
}));

Use the method, for example, to query:
SELECT DISTINCT? Concept where {[] a? Concept} LIMIT 100
Then the page is:
Copy Code code as follows:

<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<script src= "Sparqlwrapper.js" type= "Text/javascript" ></script>
<body>
<script>
var sparql = new Sparqlwrapper ("HTTP://DBPEDIA.ORG/SPARQL");
Sparql.setquery (' SELECT distinct? Concept where {[] a? Concept} LIMIT 100 ');
Sparql.query (function (JSON) {
Console.log (eval (' + JSON + '));
});
</script>
</body>

Small example: Download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.