Search code examples
sparql

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?

P.S. If possible I'd like to only fetch 10 university names.


Solution

  • 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;