How to check prefix of variables in SPARQL?

2019-03-02 12:26发布

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条回答
啃猪蹄的小仙女
2楼-- · 2019-03-02 12:52

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)

查看更多
登录 后发表回答