[英]SPARQL query universities
I am trying to fetch the names of universities using SPARQL: 我正在尝试使用SPARQL获取大学的名称:
The rdf data is the following (and a whole lot more, but that is irrelevant). rdf数据如下(还有更多,但这是无关紧要的)。
<Organization rdf:about="http://data.semanticweb.org/organization/the-university-of-queensland">
<rdfs:label>The University of Queensland</rdfs:label>
<homepage rdf:resource="http://www.uq.edu.au/"/>
<member rdf:resource="http://data.semanticweb.org/person/jane-hunter"/>
<member rdf:resource="http://data.semanticweb.org/person/kwok-cheung"/>
<member rdf:resource="http://data.semanticweb.org/person/robert-m-colomb"/>
<name>The University of Queensland</name>
</Organization>
I have written a java program which queries the data. 我编写了一个Java程序来查询数据。 My string to query the data is the following: 我查询数据的字符串如下:
queryString += "PREFIX swrc: <http://swrc.ontoware.org/ontology#> \n";
queryString += "PREFIX dc: <http://purl.org/dc/elements/1.1/> \n";
queryString += "PREFIX foaf: <http://xmlns.com/foaf/0.1/> \n";
queryString += "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> \n";
queryString += "PREFIX ical: <http://www.w3.org/2002/12/cal/ical#> \n";
queryString += "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n";
queryString += "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n";
queryString += "PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#> \n";
queryString += "PREFIX swrc_ext: <http://www.cs.vu.nl/~mcaklein/onto/swrc_ext/2005/05#> \n";
queryString += "SELECT ?name WHERE {\n";
queryString += "?university rdfs:label ?aff . \n ?university foaf:name ?name FILTER(str(?aff)='uni') }";
Unfortunately, this is not correct, as no result is returned: 不幸的是,这是不正确的,因为没有返回结果:
<?xml version='1.0' encoding='UTF-8'?>
<sparql xmlns='w3.org/2005/sparql-results#'>
<head>
<variable name='name'/>
</head>
<results>
</results>
</sparql>
Can anyone point me in the right direction? 谁能指出我正确的方向?
PS If possible I'd like to only fetch 10 university names. 附言:如果可能的话,我只想获取10个大学名称。
I understand that you're assuming all universities have the string "uni"
in their names. 我了解您假设所有大学的名称中都包含字符串"uni"
。
Notice that you're checking for equality of a value with the string "uni"
. 注意,您正在检查字符串"uni"
的值是否相等。 There really is no such instance in your data set. 您的数据集中确实没有这样的实例。
Replacing FILTER(str(?aff)='uni')
with FILTER regex(?aff, "uni", "i")
will allow you to match the values that contain the string "uni
" instead. 用FILTER regex(?aff, "uni", "i")
替换FILTER(str(?aff)='uni')
将允许您匹配包含字符串"uni
”的值。 It's a regular expression filter that takes three arguments. 这是一个带三个参数的正则表达式过滤器。
"i"
. 一组可选标志,在这种情况下,我使用了一个标志"i"
。 It means the match must be case-insensitive. 这意味着匹配必须不区分大小写。 In order to limit the number of results, you can just append the query with the LIMIT
keyword. 为了限制结果的数量,您只需在查询中附加LIMIT
关键字即可。
The resulting query should be: 结果查询应为:
PREFIX swrc: <http://swrc.ontoware.org/ontology#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX ical: <http://www.w3.org/2002/12/cal/ical#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
PREFIX swrc_ext: <http://www.cs.vu.nl/~mcaklein/onto/swrc_ext/2005/05#>
SELECT ?name WHERE {
?university rdfs:label ?aff .
?university foaf:name ?name
FILTER regex(?aff, "uni", "i")
} LIMIT 10;
In your RDF snippet does not have the "foaf:" namespace. 在您的RDF代码段中没有“ foaf:”名称空间。 So, maybe you should put in your RDF. 因此,也许您应该放入RDF。 Unless of course it is the default namespace in your RDF document. 当然,除非它是RDF文档中的默认名称空间。
您编写的代码确实对每个人都有帮助,使每个人都惊讶地理解了惊人的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.