SPARQL search for xsd:integer only, no decimals
Asked Answered
M

1

5

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?

Margerymarget answered 20/4, 2015 at 19:5 Comment(0)
C
7

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> |
-----------------------------------------------------------
Commissariat answered 20/4, 2015 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.