Tags

SPARQLs to remember

SPARQL is the standard query language for the Semantic Web.
SPARQL provides the SELECT keyword to extract information out of an RDF/OWL repository.
SPARQL also provides the CONSTRUCT keyword to construct new triples from existing ones.

Filtering based on namespace information

I was confronted with following situation.
Documents have been assigned concepts which have been modeled using SKOS.
These concepts can come from two different vocabularies, hence having different namespaces.

Example

:Document_1
a owms:Document ;
owms:subject sc:BasisonderwijsEnSpeciaalOnderwijs .

Prefix sc standing for: "http://www.test.be/vocab/sc/"

:Document_2
a owms:Document ;
owms:subject vsr:Basisonderwijs ;

Prefix vsr standing for: "http://www.test.be/vocab/vsr/"

sc:BasisonderwijsEnSpeciaalOnderwijs
a skos:Concept ;
skos:prefLabel "Basisonderwijs en Speciaal Onderwijs"^^xsd:string ;

Or another example graphically depicted using Topbraid Composer.
owms subject
Now the idea was to construct two new properties:
sc:subject with the prefered label of the value of property owms:subject if belonging to the sc namespace and
vsr:subject with the prefered label of the value of property owms:subject if belonging to the vsr namespace.

Wished result for Document 8:


namespaced subjectWe start our SPARQL Construct

CONSTRUCT { ?subject vsr:subject ?value.}
WHERE { ?subject owms:subject ?object.
?object skos:prefLabel ?value ......

BUT only if ?object belongs to the vsr namespace. So we need a FILTER construct to constrain the tripples we want to work on.

SPARQL itself doesn't offer a function to test on namespaces. Luckily there is the JENA ARQ function libary that offers us this functionality.

URL: afn:namespace
Description: Returns the namespace of a URI resource. Based on splitting the IRI, not on any prefixes in the query or dataset. For example, the namespace of http://test.com/my#Example is http://test.com/my#.
Arguments:
  • resource () - The URI resource to get the namespace of.
Returns: xsd:string

So our SPARQL construct becomes:

CONSTRUCT { ?subject vsr:subject ?value.}
WHERE { ?subject owms:subject ?object.
?object skos:prefLabel ?value
FILTER (afn:namespace(?object) = 'http://www.test.be/vocab/vsr/' ).}

Filtering based on language

One of the available datasources on the web is DBPedia.

Let's take a look at what data are available for the city of Hasselt, Belgium.

Hasselt DBPedia entry

You see that several propertyvalues such as for abstract are available in several languages. I'm only interested in the Dutch entries.

Using the following SPARQL SELECT in SPARQL Explorer for http://dbpedia.org/sparql

SELECT * WHERE {
<http://dbpedia.org/resource/Hasselt> rdfs:label ?label;
dbpedia2:abstract ?abstract.
}

leads to


without language filtering

SELECT * WHERE {
<http://dbpedia.org/resource/Hasselt> rdfs:label ?label;
dbpedia2:abstract ?abstract
FILTER (lang(?label) = 'nl')
FILTER (lang(?abstract) = 'nl').
}

gives us what we want.

with language filtering


Comments