javascript - 显示 sparql 查询结果的问题

标签 javascript html web sparql

我正在尝试在 html 中进行 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';

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

最佳答案

您需要:

使用该端点和查询构建查询的完整 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 控制台窗口中查看整个结果,并确定您想要显示的内容。

关于javascript - 显示 sparql 查询结果的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32540051/

相关文章:

javascript - 如何显示多个切换复选框的值

javascript - loadasync 函数导入的外部脚本什么时候执行?

javascript - 隐藏类中的第二个元素

javascript,拆分字符串并显示一个单词粗体

python - BeautifulSoup4 无法从此表中抓取数据

javascript - 使用 IE 导航时处理网站弹出窗口

javascript - 为什么ajax无法将数据返回到表td中的元素?

css - 在 Safari 10(?) 中隐藏自动添加的输入(用户)按钮

python - 使用 Python 将基于 pdf 的网页下载为 pdf

javascript - 如何创建直接指向动态图像的 URL 链接,特别是对于本示例?