Populate 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 W3 SPARQL query language may be run.

For example, JavaScript may be written to target the service query endpoint and run a query that obtains the URI and label for each of the entities in the current version of the register: https://reference.metoffice.gov.uk/EnvironmentalDataCategorisation/_data-readiness

let endpoint = "/system/query";
let query = "prefix rdfs:  \
        prefix reg:  \
        prefix version:  \
        select ?regdef ?label where { \
        ?item reg:register  ; \
              version:currentVersion/reg:definition/reg:entity ?regdef ; \
              version:currentVersion ?itemVer. \
        ?regdef rdfs:label ?label . }";

A javascript function can be provided to run the query on the end point and return results that may be processed and presented, used in forms, and applied to other aspects of user interaction.

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.

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 column headers
    let tableHeader = document.createElement("tr");

    for (let dataColumn of jsonObj.head.vars) {
        let th = document.createElement("th");
        th.appendChild(document.createTextNode(dataColumn));
        tableHeader.appendChild(th);
    }

    table.appendChild(tableHeader);

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

        table.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. 'browse source' on this page to see this working example code set out.

It may take a few moments for the info to be displayed here...