tags:

views:

73

answers:

1

In the following DBpedia query, is there a way to consolidate the UNIONs into a single pattern?

PREFIX prop: <http://resedia.org/ontology/&gt;
PREFIX res: <http://resedia.org/resource/&gt;
SELECT DISTINCT ?language ?label 
WHERE { 
  {res:Spain prop:language ?language} 
    UNION
  {res:France prop:language ?language}
    UNION
  {res:Italy prop:language ?language}
  ?language rdfs:label ?label .
  FILTER langMatches(lang(?label), "en") 
}

The SPARQL spec mentions something about RDF collections but I don't really understand what it's describing. It seemed like the following syntax should work, but it didn't.

PREFIX prop: <http://resedia.org/ontology/&gt;
PREFIX res: <http://resedia.org/resource/&gt;
SELECT DISTINCT ?language ?label 
WHERE { 
  (res:Spain res:France res:Italy) prop:language ?language
  ?language rdfs:label ?label .
  FILTER langMatches(lang(?label), "en") 
}

Is there a way to define a list (or "multiset", or "bag") of URIs like this inside a SELECT query?

+1  A: 

Simple answer: no.

(res:Spain res:France res:Italy) prop:language ?language

means 'match where a list containing Spain, France and Italy has a language', i.e. the list itself has a language.

You could do:

?country prop:language ?language . ?language rdfs:label ?label . 
FILTER ( ?country == res:Spain || ?country == res:France || ?country == res:Italy )

which is shorter, but may be slower.

(I had a feeling SPARQL 1.1 had an 'IN' feature, but I don't see it in the drafts)

That's unfortunate, maybe something like this will be added in later versions. Thanks for the clarification and suggestion. On a related note, is there a way to assign a single URI to a query variable and use that variable elsewhere in the query?
wynz
In standard SPARQL 1.0, no, although you can prebind variables in some systems.In SPARQL 1.1 and some systems, yes. See e.g. http://www.semanticoverflow.com/questions/925/927#927. SELECT with a sub SELECT will definitely work in SPARQL 1.1. LET might get in.
Oh, and of course { ... FILTER ( ?var = <uri> ) } has same effect.
Good to know, thanks again!
wynz