0

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

TallTed
  • 9,069
  • 2
  • 22
  • 37
Billi
  • 336
  • 1
  • 5
  • you can't use a comma like in `dbr:Eraring,_New_South_Wales` - prefixed URIs have some restrictions on the syntax. Use the full URI here instead. – UninformedUser Jan 23 '19 at 06:39
  • by the way, your query is weird. Why the `UNION`? – UninformedUser Jan 23 '19 at 06:39
  • (omitted prefixes here): `SELECT ?label ?country ?isPartOf ?_type ?capital ?lcity ?geopoint WHERE { VALUES ?s {} ?s rdfs:label ?label . FILTER(LANGMATCHES(LANG(?label), "en")) OPTIONAL{?s dbo:country ?country} OPTIONAL{?s dbo:isPartOf ?isPartOf} OPTIONAL{?s ?_type} OPTIONAL{?s dbo:capital ?capital} OPTIONAL{?s dbo:largestCity ?lcity} OPTIONAL{?s grs:point ?geopoint} }` – UninformedUser Jan 23 '19 at 06:44
  • 1
    They can be escaped: `dbr:Eraring\,_New_South_Wales` – AndyS Jan 23 '19 at 12:18
  • @AndyS I know this, but not on DBpedia ... try `select * {dbr:Eraring\,_New_South_Wales ?p ?o}` -> `Virtuoso 37000 Error SP030: SPARQL compiler, line 0: Bad character '\' (0x5c) in SPARQL expression at '\'`. But ok, I see that my initial statement was too generic. – UninformedUser Jan 23 '19 at 13:06
  • @AKSW can you turn this into an answer? – Jeen Broekstra Jan 24 '19 at 20:52
  • @AndyS dbr:Eraring\,_New_South_Wales does not work. Tried it. – Billi Jan 25 '19 at 02:20
  • @AKSW thanks for the ans, its working **** – Billi Jan 25 '19 at 02:22
  • Some useful links for this issue: - https://github.com/RDFLib/sparqlwrapper/issues/94 - https://stackoverflow.com/questions/39338506/handling-commas-when-using-a-namespace-prefix-in-a-sparql-where-clause - https://github.com/openlink/virtuoso-opensource/issues/318 – Carlos Tejo Feb 18 '19 at 07:59

1 Answers1

1

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
UninformedUser
  • 8,397
  • 1
  • 14
  • 23