Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPARQL search for xsd:integer only, no decimals

Using the following query:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX uni: <http://localhost/SemanticSearch/semanticsearch.owl#>

SELECT
    DISTINCT * 
WHERE {
    ?uri uni:altLabel "5"^^xsd:integer.
    ?uri rdf:type ?type
}

Also returns URIs which have an altLabel with xsd:decimal 5.x I really need it to return only the ?uri which have altLabel of xsd:integer. Is there anyway to achieve this?

like image 612
vincent kleine Avatar asked Apr 20 '15 19:04

vincent kleine


1 Answers

It's always easier if you can provide actual data that we can query. In the future, please provide data that we can query. because then we can actually test queries against it. In any case, here's a very simple dataset with two resources, one which has an xsd:decimal value and one with an xsd:integer value.

@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
@prefix uni: <http://localhost/SemanticSearch/semanticsearch.owl#>.
@prefix : <urn:ex:>.

:a uni:altLabel "5"^^xsd:integer ; a :somethingWithAnInteger .
:b uni:altLabel "5"^^xsd:decimal ; a :somethingWithADecimal .

You can filter for the particular datatypes that you want using the datatype function:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX uni: <http://localhost/SemanticSearch/semanticsearch.owl#>

SELECT DISTINCT * WHERE {
  ?uri uni:altLabel ?altLabel .
  ?uri rdf:type ?type
  filter(?altLabel = "5"^^xsd:integer && datatype(?altLabel) = xsd:integer)
}

-----------------------------------------------------------
| uri        | altLabel | type                            |
===========================================================
| <urn:ex:a> | 5        | <urn:ex:somethingWithAnInteger> |
-----------------------------------------------------------
like image 110
Joshua Taylor Avatar answered Oct 17 '22 13:10

Joshua Taylor