Search code examples
sparqldbpedia

SPARQL query: How to have a new "null" element for each result


i'm making a project using .csv files that the DBpedia Sparql endpoint creates. For the normal query there's no problem, i use this one for have the results:

    SELECT ?museum, ?artwork, ?abstract, ?thumbnail WHERE {
?museum <http://dbpedia.org/ontology/address> ?address.
?artwork <http://dbpedia.org/ontology/location> ?museum.
?artwork <http://dbpedia.org/ontology/abstract> ?abstract.
?artwork <http://dbpedia.org/ontology/thumbnail> ?thumbnail.
 FILTER contains(?address, "Roma")
}

and it generates a table (museum - artwork - abstract .thumblink) ...

I would like to know if it's possible to have the same table with a element added like

table (museum - artwork - abstract - thumblink - personal_element_code)

all the new element are good if they are null...

Thanks in advance

UPDATE

i've made a poit using this "kind" of query

SELECT ?museum ?artwork ?abstract ('null' as ?beacon_code) WHERE {

?museum <http://dbpedia.org/ontology/address> ?address.
?artwork <http://dbpedia.org/ontology/location> ?museum.
?artwork <http://dbpedia.org/ontology/abstract> ?abstract.

 FILTER contains(?address, "Firenze")
}

note: do not flag: "Strict checking of void variables" It's kind barbaric but it works


Solution

  • As written, your query isn't actually legal SPARQL. There should not be commas between your projection variables. DBpedia's endpoint, Virtuoso, is notorious for allowing some non-standard syntax. It's particularly frustrating because if your client has to parse your query first, then something you try in the DBpedia webservice directly might not work with your client library. You can check queries at sparql.org's query validator.

    In SPARQL, you can select variables that don't appear in your query, and they should have a sort of null value. That is, you should simply be able to do:

    SELECT ?museum ?artwork ?abstract ?thumbnail ?personal_element_code WHERE {
    

    However, that doesn't actually work on the DBpedia endpoint (again, a Virtuoso oddity). E.g., if you try the following query, you get the following error:

    select ?person ?x where {
      ?person a dbpedia-owl:Person
    }
    limit 10
    

    Virtuoso 37000 Error SP031: SPARQL compiler: Variable 'x' is used in the query result set but not assigned

    However, you can work around this without too much trouble. Just use values ?x { undef } in the body of the query. E.g.:

    select ?person ?x where {
      values ?x { undef }
      ?person a dbpedia-owl:Person
    }
    limit 10
    

    SPARQL results

    If you request CSV for that, you'll get back the following, which has the empty column that you're looking for:

    "person","x"
    "http://dbpedia.org/resource/%C3%81ngel_Gim%C3%A9nez",
    "http://dbpedia.org/resource/Aaron_Lines",
    "http://dbpedia.org/resource/Abel_Lafleur",
    "http://dbpedia.org/resource/Ada_Maimon",
    "http://dbpedia.org/resource/Adam_Krikorian",
    "http://dbpedia.org/resource/Albert_Constable",
    "http://dbpedia.org/resource/Alex_Reid_(actress)",
    "http://dbpedia.org/resource/Alex_Reid_(art_dealer)",
    "http://dbpedia.org/resource/Alex_Reid_(fighter)",
    "http://dbpedia.org/resource/Alex_Reid_(footballer)",
    

    If you actually just want the string null, this is all much easier. You can just ('null' as ?x). E.g.:

    select ?person ('null' as ?x) where {
      values ?x { undef }
      ?person a dbpedia-owl:Person
    }
    limit 10
    

    SPARQL results

    "person","x"
    "http://dbpedia.org/resource/%C3%81ngel_Gim%C3%A9nez","null"
    "http://dbpedia.org/resource/Aaron_Lines","null"
    "http://dbpedia.org/resource/Abel_Lafleur","null"
    "http://dbpedia.org/resource/Ada_Maimon","null"
    "http://dbpedia.org/resource/Adam_Krikorian","null"
    "http://dbpedia.org/resource/Albert_Constable","null"
    "http://dbpedia.org/resource/Alex_Reid_(actress)","null"
    "http://dbpedia.org/resource/Alex_Reid_(art_dealer)","null"
    "http://dbpedia.org/resource/Alex_Reid_(fighter)","null"
    "http://dbpedia.org/resource/Alex_Reid_(footballer)","null"