Meaning of square brackets “[]” when querying RDF with SPARQL?

て烟熏妆下的殇ゞ 提交于 2019-12-04 07:11:14
Joshua Taylor

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" .

[] 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!