You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Deepak Konidena <de...@cornell.edu> on 2011/05/02 22:28:32 UTC
fn:concat and afn:strJoin not appending returning empty results.
Hi,
When I try to use fn:concat and afn:strJoin in the following fashion:
PREFIX fn: <http://www.w3.org/2005/xpath-functions#>
PREFIX afn: <http://jena.hpl.hp.com/ARQ/function#>
select ?ALLTEXT ?d ?e ?h ?j ?k ?l where{
?a rdf:type foaf:Person ; ?b ?c .
?c rdf:type core:Position .
optional { ?c core:hrJobTitle ?d . } .
optional { ?c core:involvedOrganizationName ?e . } .
optional { ?c core:positionForPerson ?f . ?f rdfs:label ?h .}
optional { ?c core:positionInOrganization ?i . ?i rdfs:label ?j .}
optional { ?c core:titleOrRole ?k .}
optional { ?c rdfs:label ?l . }
LET (?ALLTEXT := fn:concat(?k, " ", ?l )) .
LET (?ALLTEXT := afn:strJoin(?k, " ", ?l ))
}
The variable ALLTEXT gets populated only when both ?k and ?l are not empty. When one
Of them is empty, ALLTEXT is showing an empty result.
How would I modify this behavior? Ideally, you would expect fn:concat to concatenate even empty results.
Am I doing anything wrong here?
Thanks,
Deepak Konidena.
Re: fn:concat and afn:strJoin not appending returning empty results.
Posted by Andy Seaborne <an...@epimorphics.com>.
On 02/05/11 21:28, Deepak Konidena wrote:
> Hi,
>
> When I try to use fn:concat and afn:strJoin in the following fashion:
>
> PREFIX fn:<http://www.w3.org/2005/xpath-functions#>
> PREFIX afn:<http://jena.hpl.hp.com/ARQ/function#>
>
> select ?ALLTEXT ?d ?e ?h ?j ?k ?l where{
>
> ?a rdf:type foaf:Person ; ?b ?c .
> ?c rdf:type core:Position .
>
> optional { ?c core:hrJobTitle ?d . } .
> optional { ?c core:involvedOrganizationName ?e . } .
> optional { ?c core:positionForPerson ?f . ?f rdfs:label ?h .}
> optional { ?c core:positionInOrganization ?i . ?i rdfs:label ?j .}
> optional { ?c core:titleOrRole ?k .}
> optional { ?c rdfs:label ?l . }
>
> LET (?ALLTEXT := fn:concat(?k, " ", ?l )) .
> LET (?ALLTEXT := afn:strJoin(?k, " ", ?l ))
> }
>
>
> The variable ALLTEXT gets populated only when both ?k and ?l are not empty. When one
> Of them is empty, ALLTEXT is showing an empty result.
By not empty, I take it you mean unbound.
> How would I modify this behavior? Ideally, you would expect fn:concat to concatenate even empty results.
>
> Am I doing anything wrong here?
The behaviour is correct and nothing to do with fn:concat.
For any strict function:
function(unbound) -> error
because the arguments are evaluated before the function is called, and
eval(?x) -> error for unbound ?x
An error in LET will leave it unbound.
Try:
SELECT * { LET ( ?A := 1/0 ) }
You may wish to use COALESCE:
BIND (COALESCE(?k, "") as ?k2)
to give a default value to ?k as ?k2. Only new variables are legal in
BIND but it is SPARQL 1.1.
>
> Thanks,
> Deepak Konidena.
>
Andy
Re: fn:concat and afn:strJoin not appending returning empty results.
Posted by Damian Steer <d....@bristol.ac.uk>.
On 2 May 2011, at 21:28, Deepak Konidena wrote:
> Hi,
Hi Deepak,
> LET (?ALLTEXT := fn:concat(?k, " ", ?l )) .
> LET (?ALLTEXT := afn:strJoin(?k, " ", ?l ))
> }
>
>
> The variable ALLTEXT gets populated only when both ?k and ?l are not empty. When one
> Of them is empty, ALLTEXT is showing an empty result.
COALESCE [1] seems like you best bet:
LET (?ALLTEXT := COALESCE(fn:concat(?k, " ", ?l ), ?k, ?l, ""))
but no doubt there are cunning alternatives I'm missing.
Damian
[1] <http://www.w3.org/TR/sparql11-query/#func-coalesce>