问题描述
我想查找RDF中有多少资源,但找不到任何教程来解释如何检查SPARQL中的变量前缀。
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.
我已经尝试过:
select count(?x) where {
res:?x ?p ?v
}
,但语法有错误。我正在为DBPedia使用virtuoso
but it has syntax error. I am using virtuoso for DBPedia
推荐答案
您可以使用 strstarts(string,prefix)检查是否字符串以前缀开头。您可以使用 str 函数获取IRI的字符串表示形式,包括从前缀生成的IRI。例如,如果您具有前缀:< http://example.org/> ,则 ex:本身就是合法的IRI,而 str(例如:)生成 http://example.org/ 。这意味着您可以通过执行 strstarts(str(?x来检查IRI是否是变量?x 的值)是否以某些特定前缀 p:开头。 ),str(p:))。然后,您可以对其进行过滤或计数,等等。
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.
以下是将事物绑定到一些不同值的示例,其中一些其中以 dbpedia-owl:前缀开头:
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 )
}
您也可以对此进行过滤,a nd然后计算结果:
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中变量的前缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!