Sparql-如果未绑定任何一个变量,则串联失败 [英] Sparql - Concatenation fails if any one variable is not bound
问题描述
您好,我正在使用AllegroGraph和Sparql查询来检索结果.这是一个示例数据,重现了我的问题. 考虑下面的数据,其中一个人的名字,中间名和姓氏.
Hi am using AllegroGraph and Sparql query to retrieve the results. This is a sample data that reproduces my issue. Consider below data where a person has first, middle and last names.
<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个姓名来计算人员姓名.名称是可选的,一个人可能没有名字,中间名和姓氏.
Now I need to compute the persons name by combining the all 3 names. The names are optional and a person may not have any of the first, middle and last names.
我尝试过查询
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),因为名字不存在.如果未绑定名字,请让我知道.
But the result set does not contain the name for person6 (Mannan Sathish) since the first name is not present. Please let me know if I can ignore the firstName if it's not bound.
推荐答案
如果未绑定变量,则str(...)将导致求值错误,并且整个BIND都会失败.
If a variable is not bound then str(...) will cause an error on evaluation and the whole BIND fails.
COALESCE
可用于为表达式提供默认值.
COALESCE
can be used to give default values to expressions.
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
这篇关于Sparql-如果未绑定任何一个变量,则串联失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!