0

I'm trying to build a sparql query based on a list of properties but I get an error saying that the query is bad formed. The problem is I don't know what to do to fix it.

This is the function:

def create_query(dbpedia_uri, props):
    #props are something like this ('dbpedia-owl', 'birthdate')
    filters = ''

    for prop in QUERIES_DICT[ename]:
        filters += ' OPTIONAL { ?x %s:%s ?%s. } \n' % (corresp_dict[prop[0]], prop[1], prop[1])

    query = u"""
        SELECT * WHERE {
.          <%s>.
          ?x dbpedia-owl:abstract ?abstract.
          %s
          FILTER (LANG(?abstract) = 'en')
        }
    """ % (dbpedia_uri, filters)

    return query

This is the query I get:

u"\n        SELECT * WHERE { <http://dbpedia.org/resource/Tim_Cook>\n          ?x dbpedia-owl:abstract ?abstract.\n          OPTIONAL { ?x dbpedia-owl:birthDate ?birthDate. }\n OPTIONAL { ?x dbpedia-owl:birthPlace ?birthPlace. }\n OPTIONAL { ?x dbpprop:name ?name. }\n OPTIONAL { ?x dbpedia-owl:profession ?profession. }\n OPTIONAL { ?x dbpprop:residence ?residence. }\n OPTIONAL { ?x dbpprop:website ?website. }\n \n          FILTER (LANG(?abstract) = 'en')\n        }\n    "

Or printed:

        SELECT * WHERE { <http://dbpedia.org/resource/Tim_Cook>
          ?x dbpedia-owl:abstract ?abstract.
          OPTIONAL { ?x dbpedia-owl:birthDate ?birthDate. }
 OPTIONAL { ?x dbpedia-owl:birthPlace ?birthPlace. }
 OPTIONAL { ?x dbpprop:name ?name. }
 OPTIONAL { ?x dbpedia-owl:profession ?profession. }
 OPTIONAL { ?x dbpprop:residence ?residence. }
 OPTIONAL { ?x dbpprop:website ?website. }

          FILTER (LANG(?abstract) = 'en')
        }
Eric
  • 95,302
  • 53
  • 242
  • 374
Rod0n
  • 1,019
  • 2
  • 14
  • 33
  • [AndyS's answer](http://stackoverflow.com/a/18279160/1281433) is spot on; the problem is that the prefixes aren't defined. Can you include the text of the "error saying that the query is bad formed"? I expect that it includes a similar message. – Joshua Taylor Aug 16 '13 at 20:22

1 Answers1

5

You need to define all the prefixes used e.g. dbpprop ,dbpedia-owl

Try: http://www.sparql.org/query-validator.html

AndyS
  • 16,345
  • 17
  • 21
  • The prefixes are defined, look below on the generated query and you'll see them added. – Rod0n Aug 16 '13 at 18:17
  • 1
    @Rod0n The generated query as shown does not include any lines such as `PREFIX dbpedia: `. It doesn't define the prefixes. The SPARQL validator that @AndyS linked to makes this clear, saying "Line 2, column 14: Unresolved prefixed name: dbpedia-owl:abstract". Without defining the prefix, `dbpedia-owl:abstract` can't be expanded into a full IRI. – Joshua Taylor Aug 16 '13 at 20:21