SPARQL/Templates

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Subqueries SPARQL
Templates
WIKIDATA Qualifiers, References and Ranks

Within Wikidata Query Service Query Helper the comment #TEMPLATE can build a simple template where the user can choose one ore more variables to change a query without needing to know the SPARQL query lanaguage.

See below an example to select pesidents and there spouses from any country:

#TEMPLATE={"template":"Presidents of ?country and their spouses","variables":{"?country":{"query":" SELECT ?id WHERE { ?id wdt:P31 wd:Q6256 . }"} } }

SELECT ?p ?pLabel ?ppicture ?w ?wLabel ?wpicture WHERE {
  BIND(wd:Q30 AS ?country)        # United States of America
  ?country (p:P6/ps:P6) ?p.       # Head of government
  ?p wdt:P26 ?w.                  # Spouse
  OPTIONAL {
    ?p wdt:P18 ?ppicture.
    ?w wdt:P18 ?wpicture.
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Try it!

The "template" contains the texts and one ore more variables.
The "variables" list the variables and optionally a "query" to select possible values, in this case ?id is instance of country.

If no query is needed the syntax is "?var1":{} In that case that might be
  #TEMPLATE={"template":"Presidents of ?country and their spouses","variables":{"?country":{} } }

Mind: BIND(wd:Q30 AS ?country) is used as a default for the variable ?country.

References[edit | edit source]


Subqueries SPARQL
Templates
WIKIDATA Qualifiers, References and Ranks