Skip to main content

Creating SPARQL query templates

Steps include: Create a query, declare a variable, use the variable in the query, add comments to give instructions on using the template, and save the template.

To create a template for SPARQL query:

  1. Create a SPARQL query. Check out this documentation to learn about how to write SPARQL queries.

    query-template-08.png
  2. Declare a variable in the query. The SPARQL query contains atleast one variable by default. To turn that into a parameter that can be used in a query template, change the character in front of the variable name from to $. Make sure to perform this change for every occurrence of the variable within the query. Alternatively, declare a new variable by starting its name with with  the character.

    In our example, we will create a new variable $year_of_interest and replacing the value 2018 in the FILTER function.

    query-template-09.png
  3. To add a default value for a variable, add a conditional BIND statement on the first line of the WHERE clause.

    The general format of that statement is:

    BIND(IF(BOUND($variable), $variable, DEFAULT_VALUE) AS $variable)

    In our example, add a new line on line 7:

    BIND(IF(BOUND($year_of_interest), $year_of_interest, 2018) AS $year_of_interest)

    query-template-10.png

    Although this sets the default value, that value is not shown in the textbox in the query template form. Even though the value is not visible, the query will now use 2018 as the default year until another value is entered.

    Note

    Since the query template does not show any default values, it is recommended you add this information in the comment for the query so that users using the template are aware of it.

  4. Add comments to the query by using the # character. The query template shows all comments before the first SPARQL statement (typically a PREFIX statement).

    query-template-11.png
  5. Click the Preview query template link above to create the template.

    Note

    Note that the Preview query template link appears only when the SPARQL query is created properly with the variables required to create a template.

    query-template-12.png
  6. Click Save to save the query template in a project.

  7. Any time you want to make changes to the query or the template, click the Edit query link.