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.

Template parameters[Edit template data]

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
extraprefixextraprefix

Extra prefixes to be added

Stringoptional
statestate

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
labellabel

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

Default
Try it!
Contentoptional
shortURLshortURL

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
Highlighthighlight

Line(s) in the source code to be highlighted. See [[mediawiki:Extension:SyntaxHighlight#highlight]] for the supported syntax

Example
3-7
Lineoptional

Examples[edit]

Wikicode Rendered result
{{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}}
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
Try it! (https://w.wiki/37j)
{{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
    } 
  }
} |highlight=1, 8-14 }}
#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
    } 
  }
}
Try 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