SPARQL
SELECT ?c0 ?l0
WHERE
{
?c0 rdf:type <XXXX>.
OPTIONAL
{
?c0 rdfs:label ?l0.
}
}
显示两列 c0
和 l0
。有什么方法可以只显示一列 c0
,但将 ?l0
的内容作为 ?c0
的标题?结果看起来像 select ?co as ...
最佳答案
如果我没理解错的话,您可以使用 coalesce 来完成此操作,它会返回其第一个非错误绑定(bind)参数。首先,这里有一些数据,其中有一个带有标签 "Dog" 的类 :dog 和一个没有标签的类 :cat:
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix : <http://example.org/> .
:dog a rdfs:Class ;
rdfs:label "Dog" .
:cat a rdfs:Class .
这里是查询和结果:
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select (coalesce(?label,?class) as ?co) where {
?class a rdfs:Class
optional {
?class rdfs:label ?label
}
}
----------------------------
| co |
============================
| <http://example.org/cat> |
| "Dog" |
----------------------------
关于rdf - 如何使类标签显示为类的列名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26114572/