SPARQL query to get all class label with namespace prefix defined

You want to add a URI prefix to the front of a label string?

I think you might be confused about what URI prefixes do. They’re just shorthand for full URIs, and aren’t part of the URI, and they don’t have any bearing on strings.

You can do something like what you want with

SELECT ?class (CONCAT("ex:", ?classLabel) AS ?label
WHERE {
   ?class rdf:type rdfs:Class.
   ?class rdfs:label ?classLabel.
}

But the prefix won’t depend on the prefix of the URI.

You could have a chain of IF()s that tests the starting characters of STR(?class), but it will get ugly quickly:

BIND(IF(STRSTARTS(STR(?class), "http://example.com/"), "ex:", "other:") as ?prefix)

then

SELECT ... (CONCAT(?prefix, ?classLabel) AS ?label

There’s almost certainly an easier way to get what you want than doing it in SPARQL though 🙂

Leave a Comment