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

别来无恙 提交于 2019-11-26 17:57:51
Joshua Taylor

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!