Meaning of square brackets "[]" when querying RDF with SPARQL?
Asked Answered
L

2

11

I am pretty new to SPARQL and RDF and I was wondering what exactly does the below mean in SPARQL?

[] vc:n ?vcard .

The complete query is

PREFIX vc: <http://www.w3.org/2006/vcard/ns#>

SELECT ?given ?family

WHERE{

    [] vc:n ?vcard .

    OPTIONAL {?vcard vc:given-name ?given .}

    OPTIONAL {?vcard vc:family-name ?family .}

}
Lumbricalis answered 13/8, 2014 at 4:25 Comment(4)
The answer here may be useful: #22312232Bandurria
@MarcBaumbach It's more than useful, it's a duplicate. What are brackets in SPARQL and why is the linked movie database limited to 2500 records?. "Brackets" in the title of that question refers to square brackets, i.e., [ and ].Verret
I've also added the turtle tag, since this syntax is shared (for the most part, if not exactly) with the Turtle serialization of RDF.Verret
Thank You all. This was pretty helpful. I would be even more grateful if any of you could give me a brief idea as to what exactly does that line do that is "[] vc:n ?vcard ." As far as I understand it is of the triple format. What does vc:n do?Lumbricalis
V
8

This is cannibalized from my answer to What are brackets in SPARQL and why is the linked movie database limited to 2500 records?, of which this question may be a duplicate, although it's a bit more broad. (It asks two questions, whereas this asks just one.) The answer is mostly links and citations of the SPARQL specification.

[ … ] is a blank node

The square brackets are described in the SPARQL 1.1 Query Language. In particular, see 4.1.4 Syntax for Blank Nodes

4.1.4 Syntax for Blank Nodes

Blank nodes in graph patterns act as variables, not as references to specific blank nodes in the data being queried.

Blank nodes are indicated by either the label form, such as "\_:abc", or the abbreviated form "[]". A blank node that is used in only one place in the query syntax can be indicated with []. A unique blank node will be used to form the triple pattern. Blank node labels are written as "_:abc" for a blank node with label "abc". The same blank node label cannot be used in two different basic graph patterns in the same query.

The [:p :v] construct can be used in triple patterns. It creates a blank node label which is used as the subject of all contained predicate-object pairs. The created blank node can also be used in further triple patterns in the subject and object positions.

The following two forms

[ :p "v" ] .
[] :p "v" .

allocate a unique blank node label (here "b57") and are equivalent to writing:

_:b57 :p "v" .

This allocated blank node label can be used as the subject or object of further triple patterns. For example, as a subject:

[ :p "v" ] :q "w" .

which is equivalent to the two triples:

_:b57 :p "v" .
_:b57 :q "w" .

and as an object:

:x :q [ :p "v" ] .

which is equivalent to the two triples:

:x  :q _:b57 .
_:b57 :p "v" .
Verret answered 13/8, 2014 at 14:21 Comment(0)
M
7

[] is a blank node in a query. It acts like a named variable except you can't use it in a SELECT project or FILTER or anywhere where you need to name the variable. You can replace [] with a named variable using a name not used anywhere in the query. SELECT * would add it but otherwise it is much the same query.

Metametabel answered 13/8, 2014 at 8:7 Comment(1)
I think this answer is relatively easier to catch for someone who is pretty new to SPARQL.Pavonine

© 2022 - 2024 — McMap. All rights reserved.