Skip to content Skip to sidebar Skip to footer

Filter Out Certain Properties From Sparql Query Result

I'm trying to filter out certain properties (e.g dbpprop: ones) from the result of a sparql query. I'm querying dbpedia sparql endoint. More precisely what I'd like to do is expres

Solution 1:

This one is working for me at http://dbpedia.org/snorql/?query=

PREFIX dbo: <http://dbpedia.org/ontology/>

SELECT DISTINCT ?qoi ?property ?value
WHERE { 
    ?qoi a foaf:Person. 
    ?qoi ?property ?value. 
OPTIONAL{ 
    ?property rdf:type ?type 
FILTER( ?type != "rdf:Property" ) }
}
LIMIT 100

I am not sure if this is the answer you are looking for

Solution 2:

You can filter out dbprop: properties,

I'm trying to filter out certain properties (e.g., dbpprop: ones) from the result of a SPARQL query. I'm querying the DBpedia SPARQL endpoint.

If you're binding properties in your result set, and you want to exclude ones whose URI begins with the dbpprop: prefix, you can do that with

filter(!strstarts(str(?p),str(dbpprop:)))

or you can filter out properties that don't have type rdf:Property.

Your pseudocode looks like it's doing something slightly different though:

quantity_of_interest, property, value, property_type = sparqlQuery(query)
if property_type == rdf:Property:
    passelse:
    return quantity_of_interest, property, value

Every property could have the type rdf:Property, since it's the class of RDF properties. DBpedia might not have the triple

p rdf:type rdf:Property

for each property p, of course, so you may still be able to filter things out like this. If that's what you want to do, you can do it with filter not exists { … }:

filter not exists { ?p rdf:type rdf:Property }

For DBpedia, it's the same right now,

As it turns out, these will have the same effect on DBpedia, since there are no properties that have type rdf:Property and aren't also dbpprop: properties; the following query returns 0:

select (count(*) as ?num) where {
 ?pa rdf:Property
 filter( !strstarts( str(?p), str(dbpprop:) ) )
}
limit 100

but one option is more future compatible.

I'd strongly suggest using the strstarts filter rather than the not exist here, though. While the URI of a property can't change over time (i.e., a URI is constant), the triples about it can change. Thus, if you filter out dbpprop: properties, then you'll never accidentally filter out more than you're expecting. However, if you filter out things that have rdf:Property as a type, then you can easily lose results in the future if more p rdf:type rdf:Property triples are added (and it seems like such an addition would be logically compatible).

Post a Comment for "Filter Out Certain Properties From Sparql Query Result"