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>
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.
Query I tried
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).
}
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.
If a variable is not bound then str(...) will cause an error on evaluation and the whole BIND fails.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With