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.
Related
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.
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).
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.
In ANSI SQL, you can write something like:
SELECT * FROM DBTable WHERE Description LIKE 'MEET'
or also:
SELECT * FROM DBTable WHERE Description LIKE '%MEET%'
What I would like help with is writing the SPARQL equivalent of the above please.
Use a regex filter. You can find a short tutorial here
Here's what it looks like:
PREFIX ns: <http://example.com/namespace>
SELECT ?x
WHERE
{ ?x ns:SomePredicate ?y .
FILTER regex(?y, "YOUR_REGEX", "i") }
YOUR_REGEX must be an expression of the XQuery regular expression language
i is an optional flag. It means that the match is case insensitive.
If you have a fixed string to match you can use that directly in your graph pattern e.g.
PREFIX ns: <http://example.com/namespace>
SELECT ?x
WHERE
{ ?x ns:SomePredicate "YourString" }
Note this does not always work because pattern matching is based on RDF term equality which means "YourString" is not considered the same term as say "YourString"#en so if that may be an issue use the REGEX approach Tom suggests
Also some SPARQL engines provide a full text search extension which allows you to integrate Lucene style queries into your SPARQL queries which may better fit your use case and almost certainly be more efficient for the generic search which would otherwise require a REGEX
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.