Handling commas when using a namespace PREFIX in a Sparql WHERE clause

早过忘川 提交于 2019-12-11 06:15:13

问题


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:

  1. leading digits, e.g. leg:3032571 or isbn13:9780136019701
  2. non leading colons, e.g. og:video:height
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!