Query Registry Contents Tutorial
This is a developer tutorial, demonstrating how one may use information from the registry within other implementations.
Contents of a registry may be used as structured information. The registry provides a query endpoint where queries using the W3C SPARQL query language may be run.
For example, JavaScript may be written to target the service query (/system/query
) endpoint and
run a query that obtains all the elements within a specific register and the associated labels for each element.
let endpoint = "/system/query"; let query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX reg: <http://purl.org/linked-data/registry#> PREFIX version: <http://purl.org/linked-data/version#> SELECT ?regdef ?label WHERE { ?item reg:register <http://codes.wmo.int/49-2/AerodromeRecentWeather> ; version:currentVersion/reg:definition/reg:entity ?regdef ; version:currentVersion ?itemVer . ?regdef rdfs:label ?label . } LIMIT 10"
The query above obtains the identifying URI and associated label text for each member of the
DataCategories/data-types
register of terms.
There is a SPARQL interface available from the _Advanced_ menu, which you can use to experiment with queries and responses. You can try copying this query and running it through the query web interface page, and also adapting query syntax to meet your needs.
A JavaScript function can be provided to run the query on the endpoint and return
results that may be processed and presented, used in forms, and applied to other aspects of user
interaction. In this example, an HTML div
container is created as the location for the output.
<div id="results"></div>
This example creates an HTTP Request against the endpoint, checks for an OK (HTTP 200) response, and sends the response content to a callback method.
let divResults = document.getElementById("results"); function sparqlQueryJson(queryStr, endpoint, callback) { // Build the request URI let requestUri = endpoint + "?query=" + escape(queryStr) + "&output=json"; // Get our HTTP request object if (window.XMLHttpRequest) { let xhr = new XMLHttpRequest(); xhr.open('GET', requestUri, true); // Set up callback to get the response asynchronously xhr.onreadystatechange = function () { if (xhr.readyState === 4) { if (xhr.status === 200) { // Do something with the results callback(xhr.responseText); } else { // Some kind of error occurred alert("Sparql query error: " + xhr.status + " " + xhr.responseText); } } }; // Send the query to the endpoint xhr.send(); } else { divResults.innerHTML = "Your browser does not support XMLHttpRequest"; } }
Defining a callback function in the script to process results provides easy access to information from the register. The JavaScript below loops through the results of the SPARQL query and outputs them as rows in an HTML table. Actual usage would depend on the context and usage profile, which is likely not a plain html table of elements but the general principle remains.
function myCallback(str) { // Convert result to JSON let jsonObj = eval('(' + str + ')'); // Build up a table of results let table = document.createElement("table"); table.className = "table table-striped table-bordered datatable dataTable"; // Create table head let tableHead = document.createElement("thead"); table.appendChild(tableHead); // Create column headers let tableHeadRow = document.createElement("tr"); for (let dataColumn of jsonObj.head.vars) { let th = document.createElement("th"); th.appendChild(document.createTextNode(dataColumn)); tableHeadRow.appendChild(th); } tableHead.appendChild(tableHeadRow); // Create table body let tableBody = document.createElement("tbody"); table.appendChild(tableBody); // Create result rows for (let dataRow of jsonObj.results.bindings) { let tableRow = document.createElement("tr"); // Create columns in row for (let column of jsonObj.head.vars) { let td = document.createElement("td"); td.appendChild(document.createTextNode(dataRow[column].value)); tableRow.appendChild(td); } tableBody.appendChild(tableRow); } // Append the table to the results HTML container divResults.textContent = ""; divResults.appendChild(table); }
Finally, call the sparqlQueryJson
method to initiate the query.
sparqlQueryJson(query, endpoint, myCallback);
Queries may be structured to deliver the information required for a particular use case, based on knowledge of the targeted register information.
Example Results
Results from this example are presented below. See the full working example.