Search code examples
sparqlrdfsemantic-webontologylinked-data

Numeric properties that are returned as string by SPARQL


I'm having a problem with SPARQL when dealing with numeric data types.

I have an ontology (http://cabas.ugr.es/ontology/ugr) in which I have defined a pair of properties that represent the number of students who are of a particular sex:

<http://cabas.ugr.es/ontology/ugr#hombres>
                     a owl:DatatypeProperty, owl:FunctionalProperty, rdf:Property ;
  rdfs:label
    "hombres"@es,
    "men"@en ;
  rdfs:comment
    "Número de estudiantes hombres."@es,
    "Number of male students."@en ;
  rdfs:range xsd:nonNegativeInteger ;
  rdfs:isDefinedBy <http://cabas.ugr.es/ontology/ugr#> ;
  owl:sameAs <http://cabas.ugr.es/ontology/ugr#hombres> ;
  owl:inverseOf <http://cabas.ugr.es/ontology/ugr#mujeres> ;
  ns1:term_status "stable" .

<http://cabas.ugr.es/ontology/ugr#mujeres>
                     a owl:DatatypeProperty, owl:FunctionalProperty, rdf:Property ;
  rdfs:label
    "mujeres"@es,
    "women"@en ;
  rdfs:comment
    "Número de estudiantes mujeres."@es,
    "Number of female students."@en ;
  rdfs:range xsd:nonNegativeInteger ;
  rdfs:isDefinedBy <http://cabas.ugr.es/ontology/ugr#> ;
  owl:sameAs <http://cabas.ugr.es/ontology/ugr#mujeres> ;
  owl:inverseOf <http://cabas.ugr.es/ontology/ugr#hombres> ;
  ns1:term_status "stable" .

I have a SPARQL endpoint mounted on Virtuoso (http://cabas.ugr.es:8890/sparql), in which I enter for example the following query:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ugr: <http://cabas.ugr.es/ontology/ugr#>

SELECT ?X ?titulacion ?rama ?hombres ?mujeres
WHERE {
  ?X ugr:Titulación ?titulacion .
  ?X ugr:RamaConocimiento ?rama .
  ?X ugr:hombres ?hombres .
  ?X ugr:mujeres ?mujeres
}

(Which would correspond with this link)

It returns all the records, but the fields "hombres" and "mujeres" returns them to me as if it were a string instead of a numeric value, so for example it is impossible to apply a filter like FILTER (?hombres > 500). Any idea what I'm wrong about?

By the way, the ontology and the resource with the values are accessible through these links:


Solution

  • In order to treat the numbers as numbers, you need to define them as such.

    Right now you define them as strings:

    <http://cabas.ugr.es/resources/MatriculasGrado1516#21>
      ns0:hombres "91" ;
      ns0:mujeres "68" .
    

    To define them as integers, you need to set their type to xsd:integer:

    <http://cabas.ugr.es/resources/MatriculasGrado1516#21>
      ns0:hombres "91"^^xsd:integer ;
      ns0:mujeres "68"^^xsd:integer .
    

    Strings can also be cast to integer in queries, if needed. For example:

    FILTER(xsd:integer(?hombres) > 500)