0

I am using the following SPARQL query from PHP where $title is the same for all five query triples:

SELECT * WHERE {
    { <http://dbpedia.org/resource/$title> rdfs:label ?pageTitle . }
    UNION
    { <http://dbpedia.org/resource/$title> owl:sameAs ?sameAs . }
    UNION
    { <http://dbpedia.org/resource/$title> dbpedia-owl:abstract ?abstract . }
    UNION
    { <http://dbpedia.org/resource/$title> dbpedia-owl:thumbnail ?thumbnail . }
    UNION
    { <http://dbpedia.org/resource/$title> dbpedia-owl:wikiPageID ?wikiID . }
}

Is there a way to simplify this so that $title is only specified once in the query? I am thinking an anonymous node might be involved, somewhere? perhaps? :-)

Nicholas Shanks
  • 10,623
  • 4
  • 56
  • 80
  • Is there a reason you want a union here, specifically? It seems like your results will be a table with five columns (one for each variable), but in each row, exactly one of those columns will have a value. Would you prefer a solution with two columns: one indicating the property, and one indicating the value? – Joshua Taylor Sep 26 '14 at 13:56
  • @JoshuaTaylor You're right, in that with the HTML output from http://dbpedia.org/sparql (using a web browser) you get what you describe, but the JSON output gives me something less useless :o) Is there a way to get the output as you describe without doing those union things (e.g. the answer from jimkont - which I am just about to try) – Nicholas Shanks Sep 26 '14 at 17:04

2 Answers2

3
SELECT * WHERE {
<http://dbpedia.org/resource/$title> ?p ?o . 
FILTER (?p IN (rdfs:label, owl:sameAs, dbpedia-owl:abstract, dbpedia-owl:thumbnail, dbpedia-owl:wikiPageID))  

Instead of FILTER (?x IN ()) you can also use VALUES (?x) {<...>, <...> } as mentioned in another reply

jimkont
  • 913
  • 1
  • 11
  • 18
1

Try to rewrite the query as follows:

SELECT * WHERE {
  { ?id rdfs:label ?pageTitle . }
  UNION
  { ?id owl:sameAs ?sameAs . }
  UNION
  { ?id dbpedia-owl:abstract ?abstract . }
  UNION
  { ?id dbpedia-owl:thumbnail ?thumbnail . }
  UNION
  { ?id dbpedia-owl:wikiPageID ?wikiID . }
} values (?id) { (<http://dbpedia.org/resource/$title>) }

This is a very nice SPARQL 1.1 feature.

enridaga
  • 631
  • 4
  • 9