sparql Comparing language tags


Example

The SPARQL function langMatches can be used to compare the language tags of RDF literals in SPARQL queries. The simple equality operator, =, can be used to compare exact string matches, but will not correctly consider regional variants. For instance, the four possible values of ?str in:

values ?str { "color"@en-US "color"@en "colour"@en "colour"@en-GB }

are all English language strings, but two of these have regional specifications. This means that

select ?str { 
  values ?str { "color"@en-US "color"@en "colour"@en "colour"@en-GB }
  filter (lang(?str) = 'en')
}

will return only two results, since only two of the values of ?str have "en" as a language tag. However,

select ?str { 
  values ?str { "color"@en-US "color"@en "colour"@en "colour"@en-GB }
  filter langMatches(lang(?str), 'en')
}

will return all four values.