Search code examples
sparqlowlprotege

what does the @prefix do in sparql?


I have a question : In sparql what does the @prefix do:

@prefix : http://example.org/animals/

and why write like this?


Solution

  • Jeen Broekstra's answer is correct; @prefix <...> is not legal SPARQL syntax, and will only produce syntax errors for you. However, there is a similar construction in the Turtle and N3 serializations of RDF. In those syntaxes, the code

    @prefix animals: <http://example.org/animals/>.
    @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
    
    animals:Moose rdfs:subClasof animals:Animal
    

    is the graph with the single triple:

    <http://example.org/animals/Moose> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://example.org/animals/Animal>
    

    The @prefix lines define prefixes that make it easier to reference IRIs in the serialization of the graph. SPARQL also has prefix declarations, but the syntax is a bit different. In a SPARQL query, there is no @ at the beginning of the line, and no . at the end of the line. So, continuing the example above, you might query for all things of subclasses of animal:Animal with the following query:

    prefix an: <http://example.org/animals/>
    prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    select ?subclass where { 
      ?subclass rdfs:subClassOf an:Animal
    }
    

    Note that in the first serialization, we used animals: but in the SPARQL query, we used an: with the same prefix. The particular prefix doesn't matter, so long as the actual (portion of a) URI that it expands to is the same. Strictly speaking, prefixes are for human convenience; they aren't strictly necessary.