Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPARQL Update example for updating more than one triple in a single query

Tags:

sparql

Can anyone point me to a valid UPDATE statement in SPARQL in any documentation (be it W3C, virtuoso, semantic web page, or your own custom code, etc?

It has to be complete with WHERE specifications and more than one triple being updated in a single query.

Thanks.

EDIT / Example:

Here is my current code, which has the goal of replacing all current values of dc:creator, dc:title and dc:description with the ones specified in the INSERT clause. Is this query correct, both in logic and syntax?

WITH GRAPH  <http://127.0.0.1:3000/dendro_graph>  
DELETE 
{ 
  :teste  dc:creator      ?o0 .
  :teste  dc:title        ?o1 .
  :teste  dc:description  ?o2 .
}
INSERT 
{ 
  :teste  dc:creator      "Creator%201" .
  :teste  dc:creator      "Creator%202" .
  :teste  dc:title        "Title%201" .
  :teste  dc:description  "Description%201" .
} 
WHERE 
{ 
  :teste  dc:creator      ?o0 .
  :teste  dc:title        ?o1 .
  :teste  dc:description  ?o2 .
} 
like image 400
João Rocha da Silva Avatar asked Oct 21 '13 18:10

João Rocha da Silva


People also ask

Which graph update operations can be performed with Sparql?

SPARQL 1.1 Update supports two categories of update operations on a Graph Store: Graph Update - addition and removal of triples from some graphs within the Graph Store.

What types of queries does Sparql support?

SPARQL contains capabilities for querying required and optional graph patterns along with their conjunctions and disjunctions. SPARQL also supports aggregation, subqueries, negation, creating values by expressions, extensible value testing, and constraining queries by source RDF graph.

What is construct query in Sparql?

The CONSTRUCT query form returns an RDF graph. The graph is built based on a template which is used to generate RDF triples based on the results of matching the graph pattern of the query.


Video Answer


2 Answers

It's still not entirely clear to me what you're trying to achieve and why the example update that you give is not doing what you want, but if the aim is to have an update that replaces triples for a given subject, you could do something like this:

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
@prefix ex: <http://example.org/> 

DELETE {?s ?p ?o}
INSERT {?s ex:title "foo" ;
           ex:description "bar" ;
           rdf:type ex:FooBar .  
       }
WHERE  { ?s ?p ?o . 
         FILTER (?s = ex:subject1) 
}

The above operation will delete all existing triples with ex:subject1 as the subject, and insert a new title, description, and type.

Or if you don't like filters, you can also formulate the above like this:

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
@prefix ex: <http://example.org/> 

DELETE {ex:subject1 ?p ?o}
INSERT {ex:subject1 ex:title "foo" ;
                    ex:description "bar" ;
                    rdf:type ex:FooBar .  
}
WHERE  { 
        ex:subject1 ?p ?o . 
}

If you only want to delete specific properties for a given subject (rather than all triples with that subject), you can modify the operation like so:

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
@prefix ex: <http://example.org/> 

DELETE {ex:subject1 ex:title ?t ;
                    ex:description ?d ; 
                    rdf:type ?c . 
       }
INSERT {ex:subject1 ex:title "foo" ;
                    ex:description "bar" ;
                    rdf:type ex:FooBar .  
}
WHERE  { 
        ex:subject1 ex:title ?t ;
                    ex:description ?d ;
                    rdf:type ?c . 
}
like image 122
Jeen Broekstra Avatar answered Oct 23 '22 08:10

Jeen Broekstra


In SPARQL updates are represented as a DELETE followed by an INSERT:

http://www.w3.org/TR/2010/WD-sparql11-update-20100126/#t413

PREFIX foaf:  <http://xmlns.com/foaf/0.1/>

WITH <http://example/addresses>
DELETE { ?person foaf:firstName 'Bill' }
INSERT { ?person foaf:firstName 'William' }
WHERE
  { ?person a foaf:Person .
    ?person foaf:firstName 'Bill'
  }
like image 44
axblount Avatar answered Oct 23 '22 08:10

axblount