How to check prefix of variables in SPARQL?

☆樱花仙子☆ 提交于 2019-12-02 09:43:48

问题


I want to find how many resource is there in an RDF but I can't find any tutorial to explain how can I check the prefix of variables in my SPARQL.

I have tried this:

select count(?x) where {
  res:?x ?p ?v
}

but it has syntax error. I am using virtuoso for DBPedia


回答1:


You can use strstarts(string,prefix) to check whether string begins with prefix. You can use the str function to get the string representation of a IRI, including IRIs generated from prefixes. E.g., if you have prefix ex: <http://example.org/>, then ex: by itself is a legal IRI, and str(ex:) produces "http://example.org/". That means that you can check whether an IRI that is the value of a variable ?x begins with some particular prefix p: by doing strstarts(str(?x),str(p:)). Then you can filter on that, or count it, etc.

Here's an example that binds ?thing to a few different values, some of which begin with the dbpedia-owl: prefix:

select * where {
   values ?thing { dbpedia-owl:a dbpedia-owl:b dbpprop:c }
   bind( strstarts(str(?thing),str(dbpedia-owl:)) as ?startsWithDBpediaOwl )
}

SPARQL results (a and b get true, c gets false)

You can filter on that too, and then count the results:

select (count(*) as ?n) where {
   values ?thing { dbpedia-owl:a dbpedia-owl:b dbpprop:c }
   filter strstarts(str(?thing),str(dbpedia-owl:))
}

SPARQL results (2)



来源:https://stackoverflow.com/questions/30051339/how-to-check-prefix-of-variables-in-sparql

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