SPARQL query to get all class label with namespace prefix defined

前端 未结 2 1120
小蘑菇
小蘑菇 2020-12-01 23:13

I want to get all class that stored in sesame repository.

This is my query

SELECT ?class ?classLabel
WHERE {
?class rdf:type rdfs:Class.
?class rdfs         


        
2条回答
  •  青春惊慌失措
    2020-12-01 23:32

    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 :)

提交回复
热议问题