Sparql 查询仅获取匹配条件的最顶层元素
Sparql query to get only top-most elements matching criteria
我有多个节点与 hasParent
链接的树,每个节点都有一些特定的颜色。我需要按颜色过滤一棵树并提取最顶层的元素(过滤后排除嵌套元素)。
这是我的数据示例:
我需要的结果是:[A, B, I]
.
我知道如何实现 returns [A, B, I, J, L, O]
:
的基本树过滤器
SELECT DISTINCT ?s WHERE {
?s <http://looneytunes-graph.com/color> "yellow"^^xsd:string
}
有没有办法实现这种额外的过滤?还是我需要重新考虑我的解决方案并使用其他东西代替图形数据库 (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] .
}
}
通过此查询,您将过滤掉具有黄色父节点(一个父节点具有一个父节点具有...)的节点。
我有多个节点与 hasParent
链接的树,每个节点都有一些特定的颜色。我需要按颜色过滤一棵树并提取最顶层的元素(过滤后排除嵌套元素)。
这是我的数据示例:
我需要的结果是:[A, B, I]
.
我知道如何实现 returns [A, B, I, J, L, O]
:
SELECT DISTINCT ?s WHERE {
?s <http://looneytunes-graph.com/color> "yellow"^^xsd:string
}
有没有办法实现这种额外的过滤?还是我需要重新考虑我的解决方案并使用其他东西代替图形数据库 (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] .
}
}
通过此查询,您将过滤掉具有黄色父节点(一个父节点具有一个父节点具有...)的节点。