Search code examples
javascriptc#asp.netsparqldbpedia

how to run sparql query in javascript for dbpedia in vs 2010 to get abstract?


I want to get abstract from dbpedia using sparql. I want to run below query in ASP .NET in Visual Studio C#. Here is what i have done.

<script>
var query= "\
PREFIX dbpedia: <http://dbpedia.org/resource/>\
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>\
SELECT ?abstract\
 WHERE {\
 ?s dbpedia:Junagadh 
 dbpedia-owl:abstract ?abstract\
 }";


var queryUrl = url + "?query=" + encodeURIComponent(query) + "&format=json";
  $.ajax({
      dataType: "jsonp",
      url: queryUrl,
      success: function (_data) {
          var results = _data.results.bindings;

          for (var i in results) {
              var src = results[i].abstract.value;
              $('body').append(src);


          }
      }
  });

I want to get abstract to the entered search term in the browser. Here i hav entered "India". When i run this code i am not getting the output in browser.

So my question is how to execute this query from javascript and get the result in browser?

Please help me, Thank you.

edit #1:

i tried as you suggest link of example to run sparql query. i tried code given in example but gets error as below in Line 17.

 Version number '1.1' is invalid. Line 1, position 16.
 Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.Xml.XmlException: Version number '1.1' is invalid. Line 1, position 16.
source Error: 

Line 16:         //Make a SELECT query against the Endpoint
Line 17:         SparqlResultSet results = endpoint.QueryWithResultSet("SELECT DISTINCT ?Concept WHERE {[] a ?Concept}");
Line 18:         foreach (SparqlResult result in results)
Line 19:         {


Stack Trace: 
[XmlException: Version number '1.1' is invalid. Line 1, position 16.]
System.Xml.XmlTextReaderImpl.Throw(Exception e) +76
System.Xml.XmlTextReaderImpl.Throw(String res, String arg) +126
System.Xml.XmlTextReaderImpl.ParseXmlDeclaration(Boolean isTextDecl)      +3983351
System.Xml.XmlTextReaderImpl.Read() +239
VDS.RDF.Parsing.SparqlXmlParser.Parse(SparqlXmlParserContext context) in e:\mercurial\dotnetrdf\Libraries\core\net40\Parsing\SPARQLXMLParser.cs:376
VDS.RDF.Parsing.SparqlXmlParser.Load(ISparqlResultsHandler handler, TextReader input) in e:\mercurial\dotnetrdf\Libraries\core\net40\Parsing\SPARQLXMLParser.cs:101
VDS.RDF.Parsing.SparqlXmlParser.Load(ISparqlResultsHandler handler, StreamReader input) in e:\mercurial\dotnetrdf\Libraries\core\net40\Parsing\SPARQLXMLParser.cs:124
VDS.RDF.Query.SparqlRemoteEndpoint.QueryWithResultSet(ISparqlResultsHandler handler, String sparqlQuery) in e:\mercurial\dotnetrdf\Libraries\core\net40\Query\SPARQLRemoteEndpoint.cs:309
VDS.RDF.Query.SparqlRemoteEndpoint.QueryWithResultSet(String sparqlQuery) in e:\mercurial\dotnetrdf\Libraries\core\net40\Query\SPARQLRemoteEndpoint.cs:284
test.Page_Load(Object sender, EventArgs e) in c:\Users\MKS\Documents\Visual Studio 2010\WebSites\WebSite3\test.aspx.cs:17
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

Please help me, Thank you.


Solution

  • Your query is totally wrong. When you write SPARQL queries you need to ask about a triple ?s ?p ?o. If you look at your query, you have 4 items there. So if you need abstracts in all the languages, you need to ask for:

    SELECT ?abstract
    WHERE {
        dbpedia:Junagadh dbpedia-owl:abstract ?abstract.
    }
    

    If you need a specific language for the abstract, you need to filter it accordingly. For example the English abstract would be:

    SELECT ?abstract
    WHERE {
        dbpedia:Junagadh dbpedia-owl:abstract ?abstract.
        filter(lang(?abstract)='en')
    }
    

    If you need to know how to write SPARQL in a .net environment, please read these examples.