Vimal
Vimal

Reputation: 416

Comma error while searching on SPARQL, DBpedia

While performing the query giving error, syntax error at ',' before '_New_South_Wales'.

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX grs: <http://www.georss.org/georss/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>

SELECT ?label ?country ?isPartOf ?_type ?capital ?lcity ?geopoint
WHERE {
        { dbr:Eraring,_New_South_Wales rdfs:label ?label  .  FILTER(LANGMATCHES(LANG(?label), "en"))}
          UNION{OPTIONAL{dbr:Eraring,_New_South_Wales dbo:country ?country} }
          UNION{OPTIONAL{dbr:Eraring,_New_South_Wales dbo:isPartOf ?isPartOf}}
          UNION{OPTIONAL{dbr:Eraring,_New_South_Wales <http://purl.org/linguistics/gold/hypernym> ?_type}}
          UNION{OPTIONAL{dbr:Eraring,_New_South_Wales dbo:capital ?capital}}
          UNION{OPTIONAL{dbr:Eraring,_New_South_Wales dbo:largestCity ?lcity}}
          UNION{OPTIONAL{dbr:Eraring,_New_South_Wales grs:point ?geopoint}}
  }

complete error report is as follows.

Virtuoso 37000 Error SP030: SPARQL compiler, line 21: syntax error at ',' before '_New_South_Wales' SPARQL query: #output-format:application/sparql-results+json define input:default-graph-uri PREFIX owl: PREFIX xsd: PREFIX rdfs: PREFIX rdf: PREFIX foaf: PREFIX dc: PREFIX : PREFIX dbpedia2: PREFIX dbpedia: PREFIX skos: PREFIX dbo: PREFIX dbp: PREFIX dbr: PREFIX grs: PREFIX geo: SELECT ?label ?country ?isPartOf ?_type ?capital ?lcity ?geopoint WHERE { { dbr:Eraring,_New_South_Wales rdfs:label ?label . FILTER(LANGMATCHES(LANG(?label), "en"))} UNION{OPTIONAL{dbr:Eraring,_New_South_Wales dbo:country ?country} } UNION{OPTIONAL{dbr:Eraring,_New_South_Wales dbo:isPartOf ?isPartOf}} UNION{OPTIONAL{dbr:Eraring,_New_South_Wales ?_type}} UNION{OPTIONAL{dbr:Eraring,_New_South_Wales dbo:capital ?capital}} UNION{OPTIONAL{dbr:Eraring,_New_South_Wales dbo:largestCity ?lcity}} UNION{OPTIONAL{dbr:Eraring,_New_South_Wales grs:point ?geopoint}} }

here is the link where you can test this query yourself. snorql

Upvotes: 0

Views: 644

Answers (1)

UninformedUser
UninformedUser

Reputation: 8465

You're trying to use prefixed names in your query and the error message indicates

dbr:Eraring,_New_South_Wales

as the source of error, in particular the comma makes the parser fail.

So let's have a look at the W3C recommendation:

4.1.1.1 Prefixed Names

The PREFIX keyword associates a prefix label with an IRI. A prefixed name is a prefix label and a local part, separated by a colon ":". A prefixed name is mapped to an IRI by concatenating the IRI associated with the prefix and the local part. The prefix label or the local part may be empty. Note that SPARQL local names allow leading digits while XML local names do not. SPARQL local names also allow the non-alphanumeric characters allowed in IRIs via backslash character escapes (e.g. ns:id\=123). SPARQL local names have more syntactic restrictions than CURIEs.

Given that the error occurs in the local name and comma is a non-alphanumeric character, the most important part here is

SPARQL local names also allow the non-alphanumeric characters allowed in IRIs via backslash character escapes (e.g. ns:id\=123).

That means, we can use the comma but have to escape it via \, i.e. we have to write

dbr:Eraring\,_New_South_Wales

Unfortunately, it looks like the Virtuoso parser has some issues with it (or I'm doing something wrong there) given that a query like

SELECT * { dbr:Eraring\,_New_South_Wales ?p ?o }

fails with

Virtuoso 37000 Error SP030: SPARQL compiler, line 0: Bad character '\' (0x5c) in SPARQL expression at '\'

An option that always works is indeed to just use the full IRI.

A query that works on the DBpedia endpoint is

PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX grs: <http://www.georss.org/georss/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>

SELECT ?label ?country ?isPartOf ?_type ?capital ?lcity ?geopoint WHERE {
 VALUES ?s {<http://dbpedia.org/resource/Eraring,_New_South_Wales>} 
 ?s rdfs:label ?label . 
 FILTER(LANGMATCHES(LANG(?label), "en")) 
 OPTIONAL{?s dbo:country ?country} 
 OPTIONAL{?s dbo:isPartOf ?isPartOf} 
 OPTIONAL{?s <http://purl.org/linguistics/gold/hypernym> ?_type} 
 OPTIONAL{?s dbo:capital ?capital} 
 OPTIONAL{?s dbo:largestCity ?lcity} 
 OPTIONAL{?s grs:point ?geopoint} 
}

You can see that I modified the query a bit:

  • there is no need for the UNION clauses – all you want is to get optional data if exist, so OPTIONAL is the correct SPARQL feature
  • I used VALUES keyword for inline data

Upvotes: 1

Related Questions