首页 > 其他 > 详细

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

时间:2014-12-08 17:34:38      阅读:170      评论:0      收藏:0      [点我收藏+]

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:

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

原文:http://www.cnblogs.com/RuiYan/p/4151181.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!