Search code examples
sparqlrdfsemantic-webdbpedia

SPARQL select literals with a given language tag When Applicable


I am looking for a query that selects literals written in English if applicable. By applicable I mean that if the requested literal is a string and not a number.

function (String p){
...
query= "select distinct ?o where {

    <http://dbpedia.org/resource/Elizabeth:_The_Golden_Age> ?p ?t. 
    FILTER(langMatches(lang(?t), "EN"))

} 
LIMIT 100";
}

the problem is if p is dbo:runtime there will be no results returned because the number has no language tag attached to it. Is there a way to say if there is language tags, chose the one in English.


Solution

  • You'd can use a filter like

    filter (lang(?var) = "" || langMatches(lang(?var), "en"))
    

    If the literal has no language type, you get back "", but if it does, then it had to match "en". You can add more checks on the datatype if you want by using the datatype function to get the datatype of the literal.