如何使用 sparql 提取具有特定谓词的 RDF 三元组

How to extract RDF triples with specific predicates using sparql

我将一组 RDF 三元组上传到本地 Virtuoso 端点。

在所有这些三元组中,我想只提取主语至少有谓词 http://www.w3.org/2000/01/rdf-schema#label and http://www.w3.org/2000/01/rdf-schema#comment.

的那些

例如,从这些三元组:

<http://dbpedia.org/resource/AccessibleComputing> <http://www.w3.org/2000/01/rdf-schema#label> "AccessibleComputing"@en .
<http://dbpedia.org/resource/AfghanistanGeography> <http://www.w3.org/2000/01/rdf-schema#label> "AfghanistanGeography"@en .
<http://dbpedia.org/resource/AfghanistanGeography> <http://www.w3.org/2000/01/rdf-schema#comment> " ... " .
<http://dbpedia.org/resource/Austroasiatic_languages> <http://www.w3.org/2000/01/rdf-schema#comment> " ... " .
<http://dbpedia.org/resource/AccessibleComputing> <http://dbpedia.org/ontology/wikiPageWikiLink> <http://dbpedia.org/resource/Computer_accessibility> .
<http://dbpedia.org/resource/AfghanistanGeography> <http://dbpedia.org/ontology/wikiPageWikiLink> <http://dbpedia.org/resource/Afghanistan_Geography> .

我想得到:

<http://dbpedia.org/resource/AfghanistanGeography> <http://www.w3.org/2000/01/rdf-schema#label> "AfghanistanGeography"@en .
<http://dbpedia.org/resource/AfghanistanGeography> <http://www.w3.org/2000/01/rdf-schema#comment> " ... " .
<http://dbpedia.org/resource/AfghanistanGeography> <http://dbpedia.org/ontology/wikiPageWikiLink> <http://dbpedia.org/resource/Afghanistan_Geography> .

是否可以使用一个(或多个)SPARQL 查询来实现这一点?

感谢您的帮助

一种方法是使用DESCRIBE,例如:

DESCRIBE ?s 
WHERE {
  ?s rdfs:label ?label .
  ?s rdfs:comment ?comment .
}

或者 CONSTRUCT :

CONSTRUCT { ?subject ?predicate ?object} 
WHERE {
  ?subject ?predicate ?object .
  FILTER EXISTS {
     ?subject rdfs:label ?label .
     ?subject rdfs:comment ?comment .
  }
}

这可以通过 CONSTRUCT WHERE 查询来完成:

CONSTRUCT WHERE {
    ?s rdfs:label ?label.
    ?s rdfs:comment ?comment.
    ?s ?p ?o
}

这是CONSTRUCT的简化形式,可以在CONSTRUCT {}部分和WHERE {}部分相同时使用。