Template:SPARQL

From Wikidata
Jump to navigation Jump to search
Documentation icon Template documentation[view · edit · history · purge ]

Usage[edit]

This template displays SPARQL query with syntax highlighting, prefixes and link to WDQS site to run the query.

[Edit template data]

Template parameters

This template prefers block formatting of parameters.

ParameterDescriptionTypeStatus
pp

Produce p: prefix if set

Booleanoptional
qq

Produce q: prefix if set

Booleanoptional
vv

Produce v: prefix if set

Booleanoptional
queryquery

SPARQL query

Stringrequired
extraprefix

Extra prefixes to be added

Stringoptional
state

Either "collapsed" or "expanded", both make the query code collapsible, "collapsed" makes it start collapsed. In a list ("*" or "#" at beginning of line) the "Collapse"/"Expand" toggle button is displayed next to the link to the Wikidata Query Service.

Stringoptional
label

Text to be used as the label of the link to the Wikidata Query Service

Default
Try it!
Contentoptional
shortURL

slug of the query's short URL, as generated by the WMF URL shortener - for example, for https://w.wiki/37j enter just 37j

Stringoptional
projectproject

project name: '''wd''' for Wikidata and '''sdc''' for Structured Data on Commons

Stringoptional
Embeddedembedded

Shall the link direct user to the results (yes) or to the code (no, empty)

Stringsuggested

Examples[edit]

  • Wikicode:
{{SPARQL|query=SELECT DISTINCT ?person ?name ?language ?death (URI(CONCAT("https://www.gutenberg.org/ebooks/author/", ?gutenberg)) AS ?gberglink) WHERE {
  ?person wdt:P1938 ?gutenberg.
  ?person wdt:P570 ?death. # Dead people only
  MINUS {
    ?enws schema:about ?person.
    ?enws schema:isPartOf <https://en.wikisource.org/>
  }
  OPTIONAL {?person wdt:P1412 ?lang}.
  FILTER (!BOUND(?lang) {{!}}{{!}} ?lang = wd:Q1860) # Language: English or absent
  BIND(IF(BOUND(?lang),"English","Not specified") AS ?language
)  ?person rdfs:label ?name.
  FILTER((LANG(?name)) = "en")
}
ORDER BY ?death
|shortURL=37j}}
  • Rendered result:
SELECT DISTINCT ?person ?name ?language ?death (URI(CONCAT("https://www.gutenberg.org/ebooks/author/", ?gutenberg)) AS ?gberglink) WHERE {
  ?person wdt:P1938 ?gutenberg.
  ?person wdt:P570 ?death. # Dead people only
  MINUS {
    ?enws schema:about ?person.
    ?enws schema:isPartOf <https://en.wikisource.org/>
  }
  OPTIONAL {?person wdt:P1412 ?lang}.
  FILTER (!BOUND(?lang) || ?lang = wd:Q1860) # Language: English or absent
  BIND(IF(BOUND(?lang),"English","Not specified") AS ?language
)  ?person rdfs:label ?name.
  FILTER((LANG(?name)) = "en")
}
ORDER BY ?death
Run it! (https://w.wiki/37j)
  • Wikicode:
{{SPARQL|embedded=yes|query=
#defaultView:Map
SELECT DISTINCT ?airport ?coor WHERE {
  ?airport (wdt:P31/wdt:P279) wd:Q62447; wdt:P17 wd:Q142.
    ?airport     wdt:P625 ?coor.
  MINUS { ?airport (wdt:P576|wdt:P582|wdt:P3999) ?dispar. }
  MINUS {
    ?airport wdt:P5817 ?interdit.
    VALUES ?interdit {
      wd:Q811683
      wd:Q63065035
      wd:Q12377751
      wd:Q30108381
      wd:Q11639308
    } 
  }
} }}
  • Rendered result:
#defaultView:Map
SELECT DISTINCT ?airport ?coor WHERE {
  ?airport (wdt:P31/wdt:P279) wd:Q62447; wdt:P17 wd:Q142.
    ?airport     wdt:P625 ?coor.
  MINUS { ?airport (wdt:P576|wdt:P582|wdt:P3999) ?dispar. }
  MINUS {
    ?airport wdt:P5817 ?interdit.
    VALUES ?interdit {
      wd:Q811683
      wd:Q63065035
      wd:Q12377751
      wd:Q30108381
      wd:Q11639308
    } 
  }
}
Run it!

See also[edit]

Warning: If you use the pipe character ("|") in your query, you need to replace it with {{!}}

  • {{SPARQLText}} syntax highlighted query text
  • {{SPARQL}} shows query text, links to the non-executed query (this template)
  • {{SPARQL2}} with item/property listing
  • {{SPARQL5}} with item/property listing
  • {{SPARQL6}} with item/property listing, indentable
  • {{SPARQL8}} inline (no query text), links to the non-executed query
  • {{SPARQL9}} same as SPARQL?
  • {{SPARQL Inline}} inline (no query text), links to the non-executed query and or the results page
  • {{SPARQL Box}} inline (no query text) styled as a centered paragraph box, links to the non-executed query
  • {{Wdquery}} inline (no query text), links to the non-executed query, rather than to the results page
  • {{Quickquery}} simple graph patters, allows to set limit and defaultView
  • {{Query page}} query stored on a dedicated wiki page, to be transcluded into other pages