如何编写带有空白节点的正确SPARQL更新语句?

问题描述 投票:0回答:1
@prefix  : <http://example.org/-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

:Person a rdf:Class ;
    :hasAttribute :name,:weight.

:Bob a :Person ;
    :hasAttribute [rdf:type :name;
            :hasValue 'Bob'],
      [rdf:type :height;
            :hasValue '1.67'^^xsd:decimal],
       [rdf:type :weight;
            :hasValue '70.2'^^xsd:decimal]. 

:John a :Person ;
    :hasAttribute [rdf:type :name;
            :hasValue 'John'],
      [rdf:type :height;
            :hasValue '1.71'^^xsd:decimal],
       [rdf:type :weight;
            :hasValue '70.2'^^xsd:decimal].

我想将上述RDF文件中Bob的权重值更新为71.8,我编写了以下SPARQL语句:

DELETE {
  ?person  :hasAttribute [ rdf:type  :weight;  :hasValue ?oldWeight ] .
}

INSERT {
  ?person  :hasAttribute [ rdf:type  :weight;  :hasValue "71.8"^^xsd:decimal ] .
}

WHERE {
  ?person rdf:type  :Person ;
           :hasAttribute [ rdf:type  :name;  :hasValue "Bob" ] .
  ?person  :hasAttribute [ rdf:type  :weight;  :hasValue ?oldWeight ] .
}

但是我发现它不能正常工作,因为旧的权重值并没有被删除,而是添加了新的权重值。

应该如何修改才能正确更新 Bob 的体重值?

sparql rdf blank-nodes
1个回答
2
投票

DELETE

不允许
存在空白节点,因为

DELETE
模板中使用新的空白节点将导致任何内容被删除,因为新的空白节点无法与图形存储中的任何内容匹配。

您可以使用 SPARQL 变量(例如

?name
?weight
)来引用现有的空白节点:

DELETE {

  ?person :hasAttribute ?weight .
  ?weight :hasValue ?oldWeightValue .

}

INSERT {

  ?person :hasAttribute [ 
    rdf:type :weight ;  
    :hasValue "71.8"^^xsd:decimal 
  ] .

}

WHERE {

  ?person a :Person ;
          :hasAttribute ?name , ?weight .

  ?name a :name ;
        :hasValue "Bob" .

  ?weight a :weight ;
        :hasValue ?oldWeightValue .

}

这应该为

WHERE
中找到的每个解决方案插入一个新的空白节点。因此,如果有两个名为 Bob 的人,则会添加两个空白节点(每个节点的权重为 71.8)。

© www.soinside.com 2019 - 2024. All rights reserved.