Skip to content

Instantly share code, notes, and snippets.

@baskaufs
Created June 4, 2015 02:51
Show Gist options
  • Select an option

  • Save baskaufs/438101ac575e25210733 to your computer and use it in GitHub Desktop.

Select an option

Save baskaufs/438101ac575e25210733 to your computer and use it in GitHub Desktop.
Hack of jQuery SPARQL test using a Callimachus "page" using Sean's query
<!DOCTYPE html>
<html>
<head>
<title>jQuery SPARQL test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
// function to turn XML object into a string
function getXmlString(xml) {
if (window.ActiveXObject) { return xml.xml; }
return new XMLSerializer().serializeToString(xml);
}
$(document).ready(function(){
// creates a function that's executed when the button is clicked
$("button").click(function(){
//pulls data from the input boxes
input1=document.getElementById('box1').value;
input2=document.getElementById('box2').value;
input3=document.getElementById('box3').value;
input4=document.getElementById('box4').value;
// creates a string that contains the query with the data from the box
// inserted into the right place
var query = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>"+
"PREFIX ac: <http://rs.tdwg.org/ac/terms/>"+
"PREFIX dwc: <http://rs.tdwg.org/dwc/terms/>"+
"PREFIX dsw: <http://purl.org/dsw/>"+
"PREFIX Iptc4xmpExt: <http://iptc.org/std/Iptc4xmpExt/2008-02-29/>"+
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
"SELECT DISTINCT ?image WHERE {" +
"?identification dwc:genus " + input1 + "." +
"?identification dwc:specificEpithet " + input2 + "." +
"?organism dsw:hasIdentification ?identification." +
"?organism foaf:depiction ?image." +
"?organism dsw:hasOccurrence ?occurrence." +
"?occurrence dsw:atEvent ?event." +
"?event dsw:locatedAt ?location." +
"?location dwc:stateProvince " + input3 + "." +
"?image Iptc4xmpExt:CVterm ?view." +
"?view rdfs:subClassOf ?featureCategory." +
"?featureCategory rdfs:label " + input4 + "." +
"} " +
"LIMIT 10";
// URL-encodes the query so that it can be appended as a query value
var encoded = encodeURIComponent(query)
// does the AJAX to send the HTTP GET to the Callimachus SPARQL endpoint
// then puts the result in the "data" variable
$.ajax({
type: 'GET',
url: '/sparql?query=' + encoded,
headers: {
Accept: 'application/sparql-results+xml'
},
success: function(data) {
// performs the string-conversion function on the returned XML object
var stringXml=getXmlString(data) ;
alert(stringXml);
document.getElementById('div1').innerHTML=stringXml ;
}
});
});
});
</script>
</head>
<body>
<h1>jQuery SPARQL test</h1>
<div id="div1">Query results get dumped here in an ugly way.</div><br/>
<input id="box1" type="text" size="50" value="?genus"/><br/>
<input id="box2" type="text" size="50" value="?species"/><br/>
<input id="box3" type="text" size="50" value="?state"/><br/>
<input id="box4" type="text" size="50" value="?category"/><br/>
<button>Insert the URI from the text box into the query.</button>
</body>
</html>
@baskaufs
Copy link
Copy Markdown
Author

baskaufs commented Jun 4, 2015

I took the example query from yesterday and inserted the query that Sean and I came up with for our Friday test. Instead of the one text box of yesterday, there are four for the four things we want the user to be able to select. The default values are set for variables. To restrict the results enter a string in quotes, e.g. "Tennessee" for "?state"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment