sparql - Sparql 查询仅获取匹配条件的最顶层元素
问题描述
我有多个树,其中节点链接在一起hasParent
,每个节点都有一些特定的颜色。我需要按颜色过滤树并提取最顶层的元素(过滤后排除嵌套元素)。
这是我的数据示例:
我需要的结果是:[A, B, I]
.
我知道如何实现返回的基本树过滤器[A, B, I, J, L, O]
:
SELECT DISTINCT ?s WHERE {
?s <http://looneytunes-graph.com/color> "yellow"^^xsd:string
}
有没有办法实现这种额外的过滤?还是我需要重新考虑我的解决方案并使用其他东西而不是 graph db (AWS Neptune)?
我正在使用https://sparql-playground.sib.swiss/data和以下数据对其进行测试:
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
<http://looneytunes-graph.com/A> <http://looneytunes-graph.com/color> "yellow"^^xsd:string .
<http://looneytunes-graph.com/B> <http://looneytunes-graph.com/color> "yellow"^^xsd:string .
<http://looneytunes-graph.com/C> <http://looneytunes-graph.com/color> "green"^^xsd:string .
<http://looneytunes-graph.com/D> <http://looneytunes-graph.com/color> "red"^^xsd:string ;
<http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/A> .
<http://looneytunes-graph.com/E> <http://looneytunes-graph.com/color> "blue"^^xsd:string ;
<http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/A> .
<http://looneytunes-graph.com/F> <http://looneytunes-graph.com/color> "blue"^^xsd:string ;
<http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/B> .
<http://looneytunes-graph.com/G> <http://looneytunes-graph.com/color> "green"^^xsd:string ;
<http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/B> .
<http://looneytunes-graph.com/H> <http://looneytunes-graph.com/color> "red"^^xsd:string ;
<http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/C> .
<http://looneytunes-graph.com/I> <http://looneytunes-graph.com/color> "yellow"^^xsd:string ;
<http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/C> .
<http://looneytunes-graph.com/J> <http://looneytunes-graph.com/color> "yellow"^^xsd:string ;
<http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/D> .
<http://looneytunes-graph.com/K> <http://looneytunes-graph.com/color> "green"^^xsd:string ;
<http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/D> .
<http://looneytunes-graph.com/L> <http://looneytunes-graph.com/color> "yellow"^^xsd:string ;
<http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/F> .
<http://looneytunes-graph.com/M> <http://looneytunes-graph.com/color> "red"^^xsd:string ;
<http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/G> .
<http://looneytunes-graph.com/N> <http://looneytunes-graph.com/color> "blue"^^xsd:string ;
<http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/G> .
<http://looneytunes-graph.com/O> <http://looneytunes-graph.com/color> "yellow"^^xsd:string ;
<http://looneytunes-graph.com/hasParent> <http://looneytunes-graph.com/I> .
解决方案
SELECT DISTINCT ?s WHERE {
?s <http://looneytunes-graph.com/color> "yellow"^^xsd:string .
FILTER NOT EXISTS {
?s hasParent+ [<http://looneytunes-graph.com/color> "yellow"^^xsd:string] .
}
}
使用此查询,您将过滤掉具有黄色父节点的节点(具有父节点的父节点具有...)。
推荐阅读
- amazon-web-services - 调用 ImportCertificate 操作时发生错误(ValidationException):不支持私钥
- google-bigquery - 在 BigQuery/StandardSQL 上将 DATE 转换为 TIMESTAMP
- java - 使用 @Async 注释的方法未异步调用,并且在应用程序上下文启动之前接收到 ContextRefreshedEvent
- json - Groovy - 不知道每个键的 XML 到 JSON
- visual-studio - 如何使用 Windbg 命令查看 CMap 条目
- git - --allow-unrelated-histories 标志的后果
- javascript - jQuery 选择器上预期的对象
- java - 我的应用程序崩溃了,我不知道为什么
- android - 看不到文本视图
- php - 无法使用 PHP 7.2 安装 PHP zip 存档