4

您好,我正在使用 AllegroGraph 和 Sparql 查询来检索结果。这是重现我的问题的示例数据。考虑以下数据,其中一个人有名字、中间名和姓氏。

<http://mydomain.com/person1> <http://mydomain.com/firstName> "John"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>
<http://mydomain.com/person1> <http://mydomain.com/middleName> "Paul"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>
<http://mydomain.com/person1> <http://mydomain.com/lastName> "Jai"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>
<http://mydomain.com/person1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://mydomain.com/person>

<http://mydomain.com/person6> <http://mydomain.com/middleName> "Mannan"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>
<http://mydomain.com/person6> <http://mydomain.com/lastName> "Sathish"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>
<http://mydomain.com/person6> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://mydomain.com/person>

现在我需要通过结合所有 3 个名字来计算人名。姓名是可选的,一个人可能没有名字、中间名和姓氏中的任何一个。

我试过的查询

select ?person ?name ?firstName ?middleName ?lastName where 
{
  ?person rdf:type  <http://mydomain.com/person>.
    optional {?person <http://mydomain.com/firstName> ?firstName}.
    optional {?person <http://mydomain.com/middleName> ?middleName}.
    optional {?person <http://mydomain.com/lastName> ?lastName}.    
    bind (concat(str(?firstName),str(?middleName),str(?lastName)) as ?name).
} 

但是结果集不包含 person6 (Mannan Sathish) 的名字,因为名字不存在。如果未绑定,请告诉我是否可以忽略 firstName。

结果集

4

1 回答 1

9

如果变量未绑定,则 str(...) 将导致评估错误并且整个 BIND 失败。

COALESCE可用于为表达式提供默认值。

bind ( COALESCE(?firstName, "") As ?firstName1)
bind ( COALESCE(?middleName, "") As ?middleName1)
bind ( COALESCE(?lastName, "") As ?lastName1)
bind (concat(str(?firstName1),str(?middleName1),str(?lastName1)) as ?name
于 2013-04-18T10:05:44.803 回答