javascript
  • html
  • web
  • sparql
  • 2015-09-12 206 views 1 likes 
    1

    我试图在html中进行sparql查询。我有这样的代码:显示sparql查询结果的问题

    <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'; 
    

    现在我必须去完成它,所以我可以看到查询的结果时,我在浏览器中打开该文件。

    回答

    1

    您需要:

    使用该端点和查询为查询构建完整的URL。您可能想要以JSON格式请求结果,因为这会使结果更容易处理。查询和格式需要以URL编码。例如:

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

    对该URL发出GET请求 - 大量代码可用于此在线可用。

    然后,您可以解析并显示返回的JSON结果,例如,为表:

    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); 
    } 
    

    添加在console.log(results);以查看浏览器的Javascript控制台窗口中的整个结果,并弄清楚到底要显示的内容。

    相关问题