Thursday, December 3, 2015

SPARQL Guide for the Classic ASP Developer

Author:
Topics: , , , ,

SPARQL Guide for the Classic ASP Developer

Introduction

This is the Classic ASP variation of 's SPARQL guides for PHP, Perl, Python, JavaScript developers. I wanted to share this because quite a number of developers (including me) are still using Classic ASP and It could also be used for Linked Data Application development.

What?

A simple guide usable by any Classic ASP developer seeking to exploit SPARQL without hassles.

Why?

SPARQL is a powerful query language, results serialization format, and an HTTP based data access protocol from the W3C. It provides a mechanism for accessing and integrating data across Deductive Database Systems (colloquially referred to as triple or quad stores in Semantic Web and Linked Data circles) -- database systems (or data spaces) that manage proposition oriented records in 3-tuple (triples) or 4-tuple (quads) form.

How?

SPARQL queries are actually HTTP payloads (typically). Thus, using a RESTful client-server interaction pattern, you can dispatch calls to a SPARQL compliant data server and receive a payload for local processing e.g. local object binding re. ASP.

Steps:
  • Determine which SPARQL endpoint you want to access e.g. DBpedia or a local Virtuoso instance (typically: http://localhost:8890/sparql).
  • If using Virtuoso, and you want to populate its quad store using SPARQL, assign "SPARQL_SPONGE" privileges to user "SPARQL" (this is basic control, more sophisticated WebID based ACLs are available for controlling SPARQL access).
  • Make sure MSXML parser is installed on the server or you can download from here.
  • Download AXE (ASP Xtreme Evolution) JSON parser json2.asp.
  • Create a new ASP page, and paste the following code in the page and save the file to the Web server.
Script:
   <!--#include file="json2.asp"-->
   <%

   dim mysrc
   dim inputsource
   dim parameters
   dim dsn

   ' Setting Data Source Name (DSN)
   dsn="http://dbpedia.org/sparql"

   ' HTTP URL is constructed accordingly with JSON query results format in mind.
   parameters="default-graph-uri=&should-sponge=&format=application%2Fsparql-results%2Bjson&timeout=0&debug=on"

   ' SPARQL Query
   ' List all the landlocked countries in Africa
   mysrc="PREFIX rdfs:  "
   mysrc = mysrc & "PREFIX type:  "
   mysrc = mysrc & "PREFIX prop:  "
   mysrc = mysrc & "SELECT ?country_name ?population ?ind1 "
   mysrc = mysrc & "WHERE { "
   mysrc = mysrc & "    ?country a type:LandlockedCountries, yago:AfricanCountries ; "
   mysrc = mysrc & "             rdfs:label ?country_name ; "
   mysrc = mysrc & "             prop:populationEstimate ?population . "
   mysrc = mysrc & "             FILTER (?population > 15000000) . "
   mysrc = mysrc & "             filter (lang(?country_name) = 'en'). "
   mysrc = mysrc & "             bind( str(?country_name) as ?ind1 ). "
   mysrc = mysrc & "} "
   mysrc = dsn & "?" & "query=" & server.urlencode(mysrc) & "&" & parameters



   Set xmlHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP")
   xmlHTTP.open "GET", mysrc, False
   xmlHTTP.setRequestHeader "Content-Type", "text/json"
   xmlHTTP.setRequestHeader "CharSet", "UTF-8"
   xmlHTTP.Send
   inputsource = xmlHTTP.responseText
   set xmlHTTP = Nothing


   set test = JSON.parse(inputsource)

   Response.write "Retrieved Data 

" dim key : for each key in test.results.bindings.keys() Response.write( test.results.bindings.get(key).country_name.value & "
") next Set test = Nothing %>
Output:
  Retrieved Data 

  Burkina Faso
  Ethiopia
  Malawi
  
Conclusion:

JSON was chosen over XML (re. output format) since this is about a "no-brainer installation and utilization" guide for a Javascript developer that already knows how to use Javascript for HTTP based data access within HTML. SPARQL just provides an added bonus to URL dexterity (delivered via URI abstraction) with regards to constructing Data Source Names or Addresses.

No comments:

Post a Comment