SPARQL - What does a single colon do in a prefix? - sparql

I've often seen a SPARQL query starting with this prefix:
PREFIX : <http://dbpedia.org/resource/>
But what exactly does it mean to use only a colon ":" in a prefix? I usually know it as putting another abbreviation in front of it. Like for example here:
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
Is there a reason to write it this way, and if so, what is the function of the prefix?
I would imagine that this would cover any other abbreviations that were not assigned. But unfortunately I've not found anything specific about this on the Internet

There’s no special functionality involved. It’s a regular prefix label, which happens to be empty.
SPARQL: Prefixed Names (bold emphasis mine):
The PREFIX keyword associates a prefix label with an IRI. A prefixed name is a prefix label and a local part, separated by a colon ":". A prefixed name is mapped to an IRI by concatenating the IRI associated with the prefix and the local part. The prefix label or the local part may be empty.
So these three snippets are equivalent:
SELECT * WHERE {
?person a <http://xmlns.com/foaf/0.1/Person> .
}
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT * WHERE {
?person a foaf:Person .
}
PREFIX : <http://xmlns.com/foaf/0.1/>
SELECT * WHERE {
?person a :Person .
}
Using an empty prefix label in SPARQL queries might make sense if all, or almost all, IRIs come from the same ontology, because the query might become more readable then.

Related

Are foaf:Person and schema.org/Person compatible with each other? or better stick with one of them?

schema.org is better for SEO
From https://lov.linkeddata.es/dataset/lov/ it seems FOAF has more adoption?
I have used the ontology:
#prefix dcterms: <http://purl.org/dc/terms/> .
#prefix foaf: <http://xmlns.com/foaf/0.1/> .
#prefix gr: <http://purl.org/goodrelations/v1#> .
GoodRelations is worry free as it is mostly compatible with schema.org, but what about the other two? i.e.
dcterms:creator vs https://schema.org/creator
foaf:Person vs https://schema.org/Person
You can use multiple vocabularies/ontologies to describe the same entity.
If a consumer (like a search engine) only recognizes terms from a specific vocabulary (like Schema.org), it will simply ignore the terms from the other vocabularies (like FOAF).
So, for an entity that represents a person, you could use the types schema:Person, foaf:Person and/or dcterms:Agent. And for a book this person wrote, you could use the properties schema:creator, foaf:maker, and/or dcterms:creator.
If your primary motivation is SEO (instead of Linked Data), I would recommend to use the vocabulary Schema.org as far as possible, and other vocabularies in addition (at least where Schema.org is lacking, or, if you prefer, wherever it’s possible).

SPARQL individuals queries for Pizza ontology

I wrote this query but it does't work. Anyone knows what is the problem.
PREFIX : <http://www.semanticweb.org/ontologies/2009/pizza.owl#>
SELECT ?X ?Y
WHERE {?X :hasCountryOfOrigin "Italy".
?Y :hasCalorificValue "400"}
According to the Pizza ontology tutorial here, there are two main issues with your query:
hasCountryOfOrigin is an object property, thus, the values can't be literals. Italy is an individual, thus, you have to use the correct URI, probably http://www.semanticweb.org/ontologies/2009/pizza.owl#Italy
The data property hasCalorificValue has values of type integer, i.e. literals should be used like "400"^^xsd:integer (or maybe xsd:int, depends on what you've chosen in Protege)
Both triple patterns in your query are not connected, i.e. no shared variable. I don't see the goal of your query.
PREFIX : <http://www.semanticweb.org/ontologies/2009/pizza.owl#>
SELECT ?X ?Y
WHERE {?X :hasCountryOfOrigin :Italy.
?Y :hasCalorificValue "400"^^xsd:integer}

sparql-query and sparql-update both are not the same?

I find that these are two different query languages:
SPARQL-QUERY and SPARQL-UPDATE.
What other types I could see in SPRARQL?
And I am looking for a syntax where I can replace a particular element property with a new value.
But, using insert query, I can only see that the new value is being added as additional value of the property instead of replacing the whole values of the property.
So, is there any other language for this purpose, like sparql-update something?
Also, I can see that delete option is there. But I don't want to specify a particular value, but to delete the whole pattern. Of course, we can specify the pattern I guess. But I just wonder, if there is a specific language for this purpose.
EDIT:
And in the following query, I don't find the purpose of using where clause at all. It always inserts specified value as a new value, but is not replacing it. We need to use the delete clause specifically. Then what's the purpose of where clause here?
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX indexing: <http://fedora.info/definitions/v4/indexing#>
PREFIX custom: <http://namespaces.info/custom#>
DELETE {
}
INSERT {
<> indexing:hasIndexingTransformation "default";
rdf:type indexing:Indexable;
dc:title "title3";
custom:ownerId "owner6";
dc:identifier "test:10";
}
WHERE {
<>
custom:ownerId "owner1";
}
The SPARQL recommendation is separated into separate documents, see SPARQL 1.1 Overview from W3C.
The WHERE clause can be empty, but also look into INSERT DATA, which takes a set of triple specifications (not patterns - no variables) and inserts them. No WHERE clause id needed int that case. Same for deleting triple specifications with DELETE DATA.

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?
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.
"#PREFIX" doesn't do anything in SPARQL, except possibly generate a syntax error.
"PREFIX", however (without the "#"), is the SPARQL instruction for a declaration of a namespace prefix. It allows you to write prefixed names in queries instead of having to use full URIs everywhere. So it's a syntax convenience mechanism for shorter, easier to read (and write) queries.

Querying with Spaces Sparql

Say if I want to return the result "having fun" of the triple. I don't know how to account for the space in between the words. Below is a query I tried but it didn't work. Let me know if anyone can spot what I doing wrong
<rdf:Description rdf:about="http://website.com/urls/playing games">
<owl:sameAs rdf:resource="http://website.com/urls/having fun"/>
</rdf:Description>
"PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT * WHERE { ?y owl:sameAs+ <http://website.com/urls/playing fun> }";
Short answer: You can't, at least not directly.
Slightly longer answer: RDF itself uses RDF URI references. The SPARQL query language, on the other hand, uses IRIs (the reason is that RDF predates IRIs and the notion of RDF URI references was developed in anticipation of what IRIs were expected to eventually look like. They almost got it right :)).
Unfortunately, there is a discrepancy between the definitions of RDF URI references and IRIs, and you have just hit one of the cases: while RDF URI references allow whitespaces, IRIs do not. The SPARQL syntax can not cope with URI references such as the one in your example. Have a look at this discussion for more details.
Your best bet? Avoid using spaces in URI refs. Replace them with underscores or just remove them.
All that being said, there is a workaround to make your query work:
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT ?y
WHERE {
?y owl:sameAs+ ?x
FILTER (str(?x) = "http://website.com/urls/playing fun")
}
It's owl:sameAs not owl:sameas. Note capital A.
You can't have spaces in IRIs.