在 Sparql WHERE 子句中使用名称空间前缀时处理逗号
Handling commas when using a namespace PREFIX in a Sparql WHERE clause
我正在尝试在 skos:broader
属性 中查询 DBPedia 类别 "Diseases_of_oral_cavity,_salivary_glands_and_jaws"。此类别在以下 URI 中可用:
http://dbpedia.org/resource/Category:Diseases_of_oral_cavity,_salivary_glands_and_jaws
以下查询提供了所需的输出:
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
}
要求是使用 Python 代码和 Sparql 包装器查询 skos:broader
属性 的几个类别。我试图通过为所有 DBPedia 类别 URI 定义 PREFIX
并在 WHERE
子句中使用它来使代码更具可读性,如下所示:
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
}
第二个查询 returns 类别名称中“,”处的语法错误。用转义序列(hex-unicode 和 html)替换逗号没有帮助。而且,使用字符串文字(dbc:"[category]"
和 dbc:'''[category]'''
)也不是正确的语法。
在这种情况下应该如何处理逗号?
此答案基于 W3C recommendation for Turtle:
前缀 IRI 的本地部分不允许使用某些特殊字符。根据 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
or isbn13:9780136019701
- non leading colons, e.g.
og:video:height
- reserved character escape sequences, e.g.
wgs:lat\-long
此外,关于escape sequences的部分给了我们更多的见解:
%-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.
更新(根据下方评论)
正如@AndyS 指出的那样,
reserved character escape sequences consist of a '\' followed by one of ~.-!$&'()*+,;=/?#@%_ and represent the character to the right of the '\'.
所以用 \
转义适用于逗号,即你可以写 \,
。不幸的是,这在 Virtuoso Web UI 和
中仍然失败
Virtuoso 37000 Error SP030: SPARQL compiler, line 0: Bad character '\' (0x5c) in SPARQL expression at '\'
所以这应该是一个错误。
我正在尝试在 skos:broader
属性 中查询 DBPedia 类别 "Diseases_of_oral_cavity,_salivary_glands_and_jaws"。此类别在以下 URI 中可用:
http://dbpedia.org/resource/Category:Diseases_of_oral_cavity,_salivary_glands_and_jaws
以下查询提供了所需的输出:
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
}
要求是使用 Python 代码和 Sparql 包装器查询 skos:broader
属性 的几个类别。我试图通过为所有 DBPedia 类别 URI 定义 PREFIX
并在 WHERE
子句中使用它来使代码更具可读性,如下所示:
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
}
第二个查询 returns 类别名称中“,”处的语法错误。用转义序列(hex-unicode 和 html)替换逗号没有帮助。而且,使用字符串文字(dbc:"[category]"
和 dbc:'''[category]'''
)也不是正确的语法。
在这种情况下应该如何处理逗号?
此答案基于 W3C recommendation for Turtle:
前缀 IRI 的本地部分不允许使用某些特殊字符。根据 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
此外,关于escape sequences的部分给了我们更多的见解:
%-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.
更新(根据下方评论)
正如@AndyS 指出的那样,
reserved character escape sequences consist of a '\' followed by one of ~.-!$&'()*+,;=/?#@%_ and represent the character to the right of the '\'.
所以用 \
转义适用于逗号,即你可以写 \,
。不幸的是,这在 Virtuoso Web UI 和
Virtuoso 37000 Error SP030: SPARQL compiler, line 0: Bad character '\' (0x5c) in SPARQL expression at '\'
所以这应该是一个错误。