Escaping strings in SPARQL queries

July 06, 2011 – tagged SPARQL

Flattr this

Resources in the RDF framework are identified by URIs, i.e. strings like http://dbpedia.org/resource/Mathematics. When they contain special characters, some care needs to be taken, when querying, comparing, escaping etc.

However, the essence of this post is that when you have a urlencoded string (i.e. one with percent signs in it), you cannot use the prefixed form of SPARQL. The following statement will not "compile":

PREFIX : <http://de.dbpedia.org/resource/>   
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>    
SELECT ?lbl_de WHERE {
  :Topologie_%28Mathematik%29 rdfs:label ?lbl_de .
  FILTER (LANG(?lbl_de) = 'de')       
}​

You will need to remove the prefix and use the full URI in angle brackets, as in:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>    
SELECT ?lbl_de WHERE {
  <http://de.dbpedia.org/resource/Topologie_%28Mathematik%29> rdfs:label ?lbl_de .
  FILTER (LANG(?lbl_de) = 'de')       
}​