Eirini Televantou
Eirini Televantou

Reputation: 131

Querying non XSD, RDF, etc., datatypes in RDF with SPARQL

This is the rdf code:

 <rdf:Description rdf:about="http://id.southampton.ac.uk/building/42">
    <ns0:notation xmlns:ns0="http://www.w3.org/2004/02/skos/core#" rdf:datatype="http://id.southampton.ac.uk/ns/building-code-scheme">42</ns0:notation>
  </rdf:Description>

I need to get the number "42". I tried this:

PREFIX soton: < http://id.southampton.ac.uk/ns/ >
PREFIX skos: < http://www.w3.org/2004/02/skos/core# >

?location skos:notation  rdf:datatype=<http://id.southampton.ac.uk/ns/building-code-scheme>(?note)

or something like:

 ?location skos:notation soton:building-code-scheme(?note)

I know how to do it with the actual RDF datatypes like xsd:integer, but I don't know how to do it with the additional datatypes.

Upvotes: 2

Views: 231

Answers (1)

Joshua Taylor
Joshua Taylor

Reputation: 85873

It is much easier to approach these problems if you can provide complete working data that we can use. In this case, it's not too hard to make the RDF snippet into a complete RDF document. For this answer, I will use the following data:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:ns0="http://www.w3.org/2004/02/skos/core#"
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
  <rdf:Description rdf:about="http://id.southampton.ac.uk/building/42">
    <ns0:notation rdf:datatype="http://id.southampton.ac.uk/ns/building-code-scheme"
    >42</ns0:notation>
  </rdf:Description>
</rdf:RDF>

If you're trying to get the literal value that is the notation of the http://id.southampton.ac.uk/building/42, then you can just ask for it directly with a SPARQL query like this one.

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?notation WHERE { 
  <http://id.southampton.ac.uk/building/42> skos:notation ?notation
}

Using Jena's ARQ command line tools, we get output like the following:

$ arq --data data.rdf --query query.sparql
---------------------------------------------------------------
| notation                                                    |
===============================================================
| "42"^^<http://id.southampton.ac.uk/ns/building-code-scheme> |
---------------------------------------------------------------

If you want to get the lexical form of the literal, you can select that using str:

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT (STR(?notation) as ?strNotation) WHERE { 
  <http://id.southampton.ac.uk/building/42> skos:notation ?notation
}

which produces output that contains the string "42":

$ arq --data data.rdf --query query.sparql
---------------
| strNotation |
===============
| "42"        |
---------------

If you had wanted to find the building that had the literal as its skos:notation, you'd need to write the literal into the SPARL query using the syntax described in 2.3.3 Matching Literals with Arbitrary Datatypes of the SPARQL recommendation. That would look like this:

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?building WHERE { 
  ?building skos:notation "42"^^<http://id.southampton.ac.uk/ns/building-code-scheme>
}

You do not have to write the full IRI in the SPARQL query, however. Just as you can use xsd:integer when the xsd: prefix is defined, you can use ns:building-code-scheme if you first define the ns: prefix as in the following.

PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX ns: <http://id.southampton.ac.uk/ns/>
SELECT ?building WHERE { 
  ?building skos:notation "42"^^ns:building-code-scheme
}

Both queries produce the same output:

$ arq --data data.rdf --query query.sparql
---------------------------------------------
| building                                  |
=============================================
| <http://id.southampton.ac.uk/building/42> |
---------------------------------------------

Upvotes: 3

Related Questions