Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using bind concat in construct query

Tags:

sparql

I have the following query

CONSTRUCT{
   ?entity a something;
    a label ?label .
} 
WHERE
{
   ?entity a something;
   a label ?label .
 BIND(CONCAT(STR( ?label ), " | SOME ADDITIONAL TEXT I WOULD LIKE TO APPEND MANUALLY") ) AS ?label ) .
}

I simply want to concatenate some text with ?label, however when running the query I get the following error:

BIND clause alias '?label' was previously used

I only want to return a single instance of ?label hence, I defined it in the construct clause.

like image 666
Imran Azad Avatar asked Mar 12 '23 20:03

Imran Azad


1 Answers

The error message seems to be accurate, but is only the first of many you will get with this query. The usual request to take a look at some SPARQL learning resources to at least understand the basics of triple-based graph pattern matching, along with, a couple of hints one what to look for. CONSTRUCT isn't a bad place to start, and the following should almost do what I think you intend:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
CONSTRUCT{
   ?entity rdfs:label ?label .
} 
WHERE
{
  ?entity a ex:something ;
      rdfs:label ?oldlabel .
  BIND(CONCAT(STR( ?oldlabel ), " | SOME ADDITIONAL TEXT I WOULD LIKE TO APPEND MANUALLY") ) AS ?label ) .
}

There's quite a few things different about that query, so take a look to see if it accurately does what you want. One hint is the syntactic difference between using '.' and ';' to separate the triple patterns. Another is that each clause defines either a URL, using a qname in the example, or a variable, prefixed by a '?'. Neither 'label' or 'something' are valid.

I say "almost" because CONSTRUCT only returns a set of triples. To modify the labels, which I think is the intent, you need to use SPARQL Update, i.e.:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ex: <http://example.org/example#>
DELETE {
   ?entity rdfs:label ?oldlabel .
}
INSERT{
   ?entity rdfs:label ?label .
} 
WHERE
{
  ?entity a ex:something .
  ?entity rdfs:label ?oldlabel .
  BIND(CONCAT(STR( ?oldlabel ), " | SOME ADDITIONAL TEXT I WOULD LIKE TO APPEND MANUALLY")  AS ?label ) .
}

Note how the triple pattern finds matches for ?oldlabel and deletes them, inserting the newly bound ?label instead. This query assumes a default graph is defined that holds both the original data and the target for updates. If not then the graph needs to be specified using WITH or GRAPH. (Also included another hint on the syntactic difference between using '.' and ';' to separate triple patterns.)

like image 90
scotthenninger Avatar answered Mar 28 '23 20:03

scotthenninger