简体   繁体   中英

SPARQL query universities

I am trying to fetch the names of universities using SPARQL:

The rdf data is the following (and a whole lot more, but that is irrelevant).

<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. 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.

I understand that you're assuming all universities have the string "uni" in their names.

Notice that you're checking for equality of a value with the string "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. It's a regular expression filter that takes three arguments.

  1. a variable
  2. a regular expression compliant with the syntax described here
  3. a set of optional flags, in this case, I used one flag, "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.

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. So, maybe you should put in your RDF. Unless of course it is the default namespace in your RDF document.

您编写的代码确实对每个人都有帮助,使每个人都惊讶地理解了惊人的代码。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM