我正在尝试在 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/