Search code examples
sparqlwikidata

How to use a variable as a property ID in a Wikidata SPARQL query


This query can return both the ID (?ID) for the property and the item ID, but I can't seem to get it to use the property ID (?ID) in a triplet.

SPARQL is a dark art to me. Thank you for your help.


SELECT ?item ?itemLabel ?superpower ?property ?label ?ID ?superpowerLabel
WHERE
{
  
  ?property a wikibase:Property ;
        schema:description ?label . 
  
  filter contains(?label,"super") 
  filter contains(?label,"abilities") 
  
  ?item rdfs:label "Wolverine"@en .
  # Get the ID
  BIND(REPLACE(STR(?property), "http://www.wikidata.org/entity/", "wdt:") AS ?ID)
  ?item ?ID ?superpower   . # This part works with "wdt:P2563" instead of ?ID.
  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

LIMIT 10

Solution

  • UPDATE: Solution by Stanislav Kralin in the comments

    w.wiki/4yY5 оr w.wiki/4yY7, see m.mediawiki.org/wiki/Wikibase/Indexing/RDF_Dump_Format – Stanislav Kralin

    WHERE
    {
      
      ?property a wikibase:Property ;
            schema:description ?label . 
      
      filter contains(?label,"super") 
      filter contains(?label,"abilities") 
      
      ?item rdfs:label "Wolverine"@en .
      # Get the ID
      BIND(URI(REPLACE(STR(?property), STR(wd:), STR(wdt:))) AS ?ID)
      ?item ?ID ?superpower   . # This part works with "wdt:P2563" instead of ?ID
      hint:Prior hint:runLast true .
      
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }
    
    LIMIT 10