Find which direct property applied in a SPARQL query

﹥>﹥吖頭↗ 提交于 2019-12-10 14:56:15

问题


I have a list of properties I want to apply to a specific entity mathematics: wd:Q395. In this case:

instanceOf: 'wdt:P31'
subclassOf: 'wdt:P279'

The results are:

Mathematics is instance of academic discipline and Mathematics is subclass of exact science and formal science

Instead of making two different queries I would like to make them all at once:

SELECT ?field ?fieldLabel ?propertyApplied
WHERE {
      wd:Q395 wdt:P31 | wdt:P279  ?field. 
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
      BIND("" AS ?propertyApplied)
}

How can I know which property applied to fill the right column? ( for example next to academic discipline I would like that it appears instance of)

I tried this but it looks weird and the results repeat themselves.

SELECT ?instanceOf ?subclassOf ?instanceOfLabel ?subclassOfLabel
WHERE {
      OPTIONAL { wd:Q395 wdt:P31 ?instanceOf. }
      OPTIONAL { wd:Q395 wdt:P279 ?subclassOf. }
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}


回答1:


Use VALUES or UNION:

SELECT ?field ?fieldLabel ?propertyLabel WHERE {
      VALUES (?predicate) {(wdt:P31) (wdt:P279)}
      wd:Q395 ?predicate ?field . 
      ?property wikibase:directClaim ?predicate .
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

Try it!

SELECT ?field ?fieldLabel ?propertyLabel {
     { wd:Q395 wdt:P31 ?field . BIND (wd:P31 AS ?property) }
      UNION
     { wd:Q395 wdt:P279 ?field . BIND (wd:P279 AS ?property) }
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

Try it!



来源:https://stackoverflow.com/questions/51103215/find-which-direct-property-applied-in-a-sparql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!