1

I need to query the WikiData to get for an specific country code (that I've, for the example is Q414 (ARG), the flag and the emoji flag respective. The emoji, by I know, is on zxx lang, but anyway any of my solutions worked. Any help will be preciated.

SELECT ?label ?nombre_corto ?imagen_de_la_bandera WHERE {
  wd:Q414 rdfs:label ?label.
  OPTIONAL { ?label wdt:P1813 ?nombre_corto. }
  FILTER(LANGMATCHES(LANG(?label), "es"))
    FILTER(LANGMATCHES(LANG(?nombre_corto), "zxx"))
  OPTIONAL { ?label wdt:P41 ?imagen_de_la_bandera. }
}
LIMIT 100
3

Assuming that countries are entities with ISO 3166-1 alpha-3 codes, your query should be:

#defaultView:ImageGrid
SELECT ?country ?countryLabel ?code ?emoji ?flag {
  # VALUES ?country {wd:Q414}
  ?country wdt:P298 ?code . FILTER NOT EXISTS { ?country wdt:P31 wd:Q3024240 }
  OPTIONAL { ?country wdt:P1813 ?emoji. FILTER(lang(?emoji)= "zxx") }
  OPTIONAL { ?country wdt:P41 ?flag. }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "es" . }
} ORDER BY ?code

Try it! (257 results)

The main problem of the original query is probably a mistype: you use ?label instead of wd:Q414 in two places. By the way, values of ?label are literals, which cannot be subjects of RDF triples.

The SPARQL 1.1 VALUES keyword allows to not repeat oneself and to avoid such mistypes.


There are minor remaining problems, which manifest themselves as duplicated country codes.

  1. The first problem is that not only emojies can be used as short names with no linguistic content, consider wd:Q159 as an example.
    In order to filter out that "РОФ", your query should address qualifiers on P1813-statements:

    SELECT ?country ?countryLabel ?code ?emoji ?flag {
      # VALUES ?country {wd:Q414}
      ?country wdt:P298 ?code . FILTER NOT EXISTS { ?country wdt:P31 wd:Q3024240 }
      OPTIONAL { ?country p:P1813 [ ps:P1813 ?emoji ; pq:P31 wd:Q28840786 ] }
      OPTIONAL { ?country wdt:P41 ?flag. }
      SERVICE wikibase:label { bd:serviceParam wikibase:language "es" . }
    } ORDER BY ?code
    

    Try it! (256 results)

    Other problems are related to Wikidata data quality.

  2. The second problem is exact duplicates, e. g. wd:Q9648 and wd:Q1249802.
    The problem could be fixed in the same way as the next one.

  3. The third problem is that ISO codes are applied both to a part and to a whole: compare wd:Q35 and wd:Q756617; wd:Q55 and wd:Q29999.
    One can get rid of such duplicates using GROUP and SAMPLE:

    SELECT ?code
      (SAMPLE(?label) AS ?label) 
      (MAX(?emoji) AS ?emoji)
      (SAMPLE(?flag) AS ?flag)  {
      # VALUES ?country {wd:Q414}
      ?country wdt:P298 ?code . FILTER NOT EXISTS { ?country wdt:P31 wd:Q3024240 }
      ?country rdfs:label ?label . FILTER (lang(?label) = 'es') 
      OPTIONAL { ?country p:P1813 [ ps:P1813 ?emoji ; pq:P31 wd:Q28840786 ] }
      OPTIONAL { ?country wdt:P41 ?flag. }
    } GROUP BY ?code ORDER BY ?code
    

    Try it! (250 results)

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.