SPARQL/Templates
< SPARQL
Within Wikidata Query Service Query Helper the comment #TEMPLATE can build a simple template where the user can choose one or more variables to change a query without needing to know the SPARQL query language.
See below an example to select presidents and their 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". }
}
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