2

I am trying to make a sparql query in html. I have this code:

    <script type="text/javascript">
        document.write("<strong>Hola!</strong>");
        var SPARQL_ENDPOINT = 'http://datos.zaragoza.es/sparql';
        var query = 'PREFIX pproc: <http://contsem.unizar.es/def/sector-publico/pproc#>\
        PREFIX dcterms: <http://purl.org/dc/terms/>\
        SELECT DISTINCT ?uri ?titulo ?servicioGestor WHERE {\
        ?uri a <http://contsem.unizar.es/def/sector-publico/pproc#Contract>;\
        dcterms:title ?titulo;\
        pproc:managingDepartment ?managingDepartment.\
        ?managingDepartment dcterms:title ?servicioGestor} ORDER BY ?titulo';

And now I have to complete it so I can see the results of the query when I open the file in a the browser.

luis.galdo
  • 543
  • 5
  • 20

1 Answers1

2

You need to:

Build a full URL for the query using that endpoint and query. You may want to request the results in JSON format as that will make the results easier to handle. The query and format need to be URL encoded e.g.:

var url = SPARQL_ENDPOINT
  + '?query=' + encodeURIComponent(query)
  + '&format=' + encodeURIComponent('application/sparql-results+json');

Make a GET request to that URL - plenty of code for this available online.

You can then parse and display the JSON result you get back e.g. as a table:

function printTable(response){
    var results = JSON.parse(response);
    var table = '<table>';
    results.results.bindings.forEach(function (result){
        table += '<tr><td>' + result.uri.value + '</td><td>' + result.titulo.value
          + '</td><td>' + result.servicioGestor.value + '</td></tr>';
    })
    table += '</table>';
    document.write(table);
}

Add in a console.log(results); to view the entire results in the browser Javascript console window and figure out what exactly you want to display.

sheilak
  • 5,833
  • 7
  • 34
  • 43