No need to add "optional" in your insert section of your sparql code

I was trying to insert new data while the where clauses have an "optional" clause. I thought I also need to use optional in the insert clause, but it's not true: 

PREFIX foaf:  <http://xmlns.com/foaf/0.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

INSERT 
  { GRAPH <http://example/addresses>
    {
      ?person  foaf:name  ?name .
       optional { ?person  foaf:mbox  ?email } # what I thought, which is wrong . Also you can pay attention to optional syntax : use {} and no punctuation at the end of the triple.
    } }
WHERE
  { GRAPH  <http://example/people>
    {
      ?person  foaf:name  ?name .
      OPTIONAL { ?person  foaf:mbox  ?email }
    } }

the rigth version is : 

PREFIX foaf:  <http://xmlns.com/foaf/0.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

INSERT 
  { GRAPH <http://example/addresses>
    {
      ?person  foaf:name  ?name .
      ?person  foaf:mbox  ?email . # you don't need to add optional here
    } }
WHERE
  { GRAPH  <http://example/people>
    {
      ?person  foaf:name  ?name .
      OPTIONAL { ?person  foaf:mbox  ?email }
    } }

As it puts here: http://www.w3.org/TR/sparql11-update/ (and you search example 9 in the page )

This example copies triples of name and email from one named graph to another. Some individuals may not have an address, but the name is copied regardless:

posted @ 2014-12-08 15:21  Rui Yan  阅读(187)  评论(0编辑  收藏  举报