在Sparql WHERE子句中使用名称空间PREFIX时处理逗号 [英] Handling commas when using a namespace PREFIX in a Sparql WHERE clause

查看:236
本文介绍了在Sparql WHERE子句中使用名称空间PREFIX时处理逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查询 skos:broader 属性的DBPedia类别 Diseases_of_oral_cavity,_salivary_glands_and_jaws。可在以下URI中找到此类别:
http://dbpedia.org/resource/类别: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
}

要求是查询 skos:broader 属性用于使用Python代码和Sparql包装器的多个类别。我试图通过为所有DBPedia类别URI定义 PREFIX 并在 WHERE 中使用它来使代码更具可读性。子句如下:

  PREFIX dbpcat:< http://dbpedia.org/resource/Category:> 
PREFIX skos:< http://www.w3.org/2004/02/skos/core#>

选择?broaderCategory
WHERE {
dbpcat:Diseases_of_oral_cavity,_salivary_glands_and_jaws skos:broader?broaderCategory
}

第二个查询在类别名称的,处返回语法错误。用转义序列(十六进制unicode和html)替换逗号并没有帮助。并且,使用字符串文字( dbc: [category] dbc:'''[category]''')也不正确的语法。



在这种情况下应如何处理逗号?

解决方案

此答案基于针对Turtle的W3C建议



带前缀的IRI的本地部分不允许使用某些特殊字符。根据关于IRI的部分


前缀是XML QName的超集。它们的不同之处在于,前缀名称的本地部分可能包括:


  1. 前导数字,例如 leg:3032571 isbn13:9780136019701

  2. 非前导冒号,例如 og:video:height

  3. 保留的字符转义序列,例如 wgs:lat\-long


此外,有关转义序列的部分为我们提供了更多见解:


%编码的序列在IRI的字符范围内,并在本地名称中明确允许。它们显示为%,后跟两个十六进制字符,并表示相同的三个字符序列。这些序列在处理期间不会解码。在Turtle中写为 http://a.example/%66oo-bar 的术语表示IRI < a href = http://a.example/%66oo-bar rel = nofollow> http://a.example/%66oo-bar 而非IRI http://a.example/foo-bar 。用 ex:%66oo-bar 开头的前缀 @prefix ex:< http://a.example/> 还指定IRI http://a.example/%66oo-bar




更新(根据下面的评论)



@AndyS指出,


保留的字符转义序列由一个'followed'后跟〜.-!$&''()* +中的一个组成,; == ??#@%_并表示'\'右边的字符。


因此用 \ 适用于逗号,即,您可以编写 \,。不幸的是,这在Virtuoso Web UI中仍然失败,

  Virtuoso 37000错误SP030:SPARQL编译器,第0行:错误字符'\ SPARQL表达式中的\''(0x5c)位于'\'

所以应该是一个错误。 / p>

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?

解决方案

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.

这篇关于在Sparql WHERE子句中使用名称空间PREFIX时处理逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆