问题
I am trying to query the skos:broader
property for the DBPedia category "Diseases_of_oral_cavity,_salivary_glands_and_jaws". This category is available at the following URI:
http://dbpedia.org/resource/Category:Diseases_of_oral_cavity,_salivary_glands_and_jaws
The following query provides the desired output:
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?broaderCategory
WHERE {
<http://dbpedia.org/resource/Category:Diseases_of_oral_cavity,_salivary_glands_and_jaws> skos:broader ?broaderCategory
}
The requirement is to query the skos:broader
property for several categories using Python code and a Sparql wrapper. I am trying to make the code more readable by defining a PREFIX
for all DBPedia category URIs and using it in the WHERE
clause as follows:
PREFIX dbpcat: <http://dbpedia.org/resource/Category:>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?broaderCategory
WHERE {
dbpcat:Diseases_of_oral_cavity,_salivary_glands_and_jaws skos:broader ?broaderCategory
}
The second query returns a syntax error at the ',' in the category name. Replacing the comma with escape sequences (hex-unicode and html) hasn't helped. And, using a string literal (dbc:"[category]"
and dbc:'''[category]'''
) isn't correct syntax either.
How should the comma be handled in this case?
回答1:
This answer is based in the W3C recommendation for Turtle:
Some special characters are not allowed in the local part of prefixed IRIs. According to the section about IRIs
Prefixed names are a superset of XML QNames. They differ in that the local part of prefixed names may include:
- leading digits, e.g.
leg:3032571
orisbn13:9780136019701
- non leading colons, e.g.
og:video:height
- reserved character escape sequences, e.g.
wgs:lat\-long
In addition, the section about escape sequences gives us more insights:
%-encoded sequences are in the character range for IRIs and are explicitly allowed in local names. These appear as a '%' followed by two hex characters and represent that same sequence of three characters. These sequences are not decoded during processing. A term written as http://a.example/%66oo-bar in Turtle designates the IRI http://a.example/%66oo-bar and not IRI http://a.example/foo-bar. A term written as
ex:%66oo-bar
with a prefix@prefix ex: <http://a.example/>
also designates the IRI http://a.example/%66oo-bar.
Update (according to comment below)
As @AndyS pointed out,
reserved character escape sequences consist of a '\' followed by one of ~.-!$&'()*+,;=/?#@%_ and represent the character to the right of the '\'.
So escaping with \
works for commas, i.e. you can write \,
. Unfortunately, this still fails in the Virtuoso Web UI with
Virtuoso 37000 Error SP030: SPARQL compiler, line 0: Bad character '\' (0x5c) in SPARQL expression at '\'
So that should be a bug.
来源:https://stackoverflow.com/questions/39338506/handling-commas-when-using-a-namespace-prefix-in-a-sparql-where-clause