Why does my SPARQL query return the URI of a resource instead of its name?

Non-anonymous resources in RDF and OWL are identified by IRIs. Your ontology clearly says that http://www.w3.org/2002/07/owl#aqua is class. If you ask for the class, that’s what you should get. It might be that Protege strips off the http://www.w3.org/2002/07/owl# part when it displays the result, but the result is still actually the IRI.

Note: you really should not be defining new classes whose IRIs begin with the standard OWL namespace. You should be defining your own prefix, typically related to the ontology IRI.

If you just want to get the string “aqua” as the result, you have two options. The first (and preferred) approach is to retrieve the rdfs:label of the class, if it has one, which should be the string name of the class. If for some reason that doesn’t work, you can take the string value of the URI and strip off the string value of the prefix. Here are examples of both approaches on the DBpedia SPARQL endpoint:

select ?class ?label where {
  ?class a owl:Class ; rdfs:label ?label
  filter langMatches(lang(?label),'en')
}
limit 10

SPARQL results (with rdfs:label)

select ?class ?name where {
  ?class a owl:Class
  bind(strafter(str(?class),str(dbpedia-owl:)) as ?name)
}
limit 10

SPARQL results (by stripping the prefix)

Stripping off the prefix of a URI for display purposes is, in general, not a recommended practice, as it assumes that the URI has a human-readable form. In the case of DBPedia that happens to work, but plenty of datasets have URIs with internal codes rather than human-readable names. So if the rdfs:label (which is explicitly defined to be the human-readable representation of the resource) is available, you should try and always use that.

Leave a Comment