3

I am creating an RDF graph by using SPARQL CONSTRUCT statement. Following is my query:

prefix map: <#> 
prefix db: <> 
prefix vocab: <vocab/> 
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
prefix xsd: <http://www.w3.org/2001/XMLSchema#> 
prefix d2rq: <http://www.wiwiss.fu-berlin.de/suhl/bizer/D2RQ/0.1#> 
prefix jdbc: <http://d2rq.org/terms/jdbc/>
prefix fn: <http://www.w3.org/2005/xpath-functions#> 

CONSTRUCT { 
map:database a fn:concat('d2rq:Database', ';').
map:database d2rq:jdbcDriver str(?o1).
map:database d2rq:jdbcDSN ?o2.
map:database d2rq:username ?o3.
map:database d2rq:password ?o4.
}
FROM <http://www.ndssl.vbi.vt.edu/epidl>
WHERE 
{ 
<http://www.ndssl.vbi.vt.edu/epidl#NDSSLDB> <http://www.ndssl.vbi.vt.edu/epidl#driver> ?o1.
<http://www.ndssl.vbi.vt.edu/epidl#NDSSLDB> <http://www.ndssl.vbi.vt.edu/epidl#dsn> ?o2.
<http://www.ndssl.vbi.vt.edu/epidl#NDSSLDB> <http://www.ndssl.vbi.vt.edu/epidl#username> ?o3.
<http://www.ndssl.vbi.vt.edu/epidl#NDSSLDB> <http://www.ndssl.vbi.vt.edu/epidl#password> ?o4.

}

}

I have found that fn:concat() and str() functions are not working with SPARQL CONSTRUCT. Query is giving me an error. However above mentioned functions works properly with separate select statements like followings:

fn:concat()

prefix fn: <http://www.w3.org/2005/xpath-functions#> 
select (fn:concat('d2rq:jdbcDriver', ';') AS ?p) where {?s ?p ?o} LIMIT 1

str()

select str(?o) from <http://www.ndssl.vbi.vt.edu/epidl> 
where {<http://www.ndssl.vbi.vt.edu/epidl#NDSSLDB> <http://www.ndssl.vbi.vt.edu/epidl#dsn> ?o}  

Please let me know how can I use fn:concat() and str() functions with SPARQL CONSTRUCT.

Beautiful Mind
  • 5,828
  • 4
  • 23
  • 42
  • Why are you doing fn:concat? It should just be concat. And the usual way to do this is a construct query with a select subquery. There are some other questions that have useful examples. – Joshua Taylor Feb 17 '16 at 21:29
  • 1
    E.g., see http://stackoverflow.com/questions/33475950/use-aggregates-min-max-avg-in-construct-query – Joshua Taylor Feb 17 '16 at 21:31
  • Construct with select subquery is not working for me. Could you please provide me an example? – Beautiful Mind Feb 17 '16 at 21:45
  • 1
    I think that some of those questions have examples, but it'd be something like `construct { ?s :concatLabel ?labels } where {{ select ?s (group_concat(?label) as ?labels) { ?s rdfs:label ?label } group by ?s }}`. – Joshua Taylor Feb 17 '16 at 21:53
  • 1
    @user2151087 not directly related to this particular question, but I had a quick look at your profile and you've asked many questions here on StackOverflow, but have never marked any of the answers you received as accepted. It's your choice whether to do so or not, of course, but perhaps you're not aware that this is an option. See [what to do if someone answers](http://stackoverflow.com/help/someone-answers) for more info. – Jeen Broekstra Feb 18 '16 at 02:49

1 Answers1

4

The CONSTRUCT clause in a SPARQL query can only contain graph patterns. Filters or functions can not be included.

To include the output of some function in your CONSTRUCT query result, you need to use a BIND operation in your WHERE clause that assigns the function output to a new variable, and then you can use that new variable in your CONSTRUCT clause.

For example, to use the output of the STR() function, you'd do something like this:

CONSTRUCT { ?s ?p ?string }
WHERE {
        ?s ?p ?o .
        BIND(STR(?o) as ?string)
 }
Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73
  • If this answers OP'S question, then I really think this is a duplicate of one of the other questions. – Joshua Taylor Feb 18 '16 at 12:25
  • Possibly, but I didn't spot one where the answer showed a solution with BIND. I you have a duplicate I overlooked, feel free to vote to close. – Jeen Broekstra Feb 18 '16 at 18:29