The aim of this gist is to show how to handle the execution of simple SPARQL query over the DBpedia SPARQL endpoint using Javascript, JQuery and AJAX.
Last active
August 29, 2015 14:02
-
-
Save fabiovalse/4429e68ab79ee82777d4 to your computer and use it in GitHub Desktop.
LOD CNR MI TUTORIAL - Example 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="Seminario CNR MI - Example 1" /> | |
<meta charset="utf-8"> | |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> | |
<script type="text/javascript" src="index.js"></script> | |
<title>Seminario Linked Data</title> | |
</head> | |
<body> | |
<h1>EXAMPLE N° 1</h1> | |
<div><b>SPARQL QUERY:</b></div> | |
<div id="query" contentEditable="true"> | |
<pre> | |
<code> | |
PREFIX : <http://dbpedia.org/resource/> | |
PREFIX dbp: <http://dbpedia.org/ontology/> | |
SELECT ?x | |
WHERE { | |
:Pisa dbp:country ?x. | |
} | |
</code> | |
</pre> | |
</div> | |
<div> | |
<input type="button" onclick="retrieveData()" value="Execute Query"> | |
</div> | |
<br> | |
<div><b>RESULTS:</b></div><br> | |
<div id="output_div"> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function retrieveData() { | |
var url = 'http://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=' + encodeURIComponent($('#query').text()) + '&output=json'; | |
$.ajax({ | |
url: url, | |
dataType: "json", | |
success: function (data) { | |
handle_json(data); | |
}, | |
error: function(e) { | |
console.log("error"); | |
} | |
}); | |
} | |
function handle_json(json) { | |
$('#output_div').text(json['results']['bindings'][0]['x']['value']); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PREFIX : <http://dbpedia.org/resource/> | |
PREFIX dbp: <http://dbpedia.org/ontology/> | |
SELECT ?x | |
WHERE { | |
:Pisa dbp:country ?x. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment