Search code examples
sparqldbpedia

SPARQL subquery in DBpedia


I want to filter a resource that is used as "join" in the query. For example, given a DBpedia resource, i need to return the label of resources linked by sameAs property and has "pt" in its URI. I am using the following query:

SELECT ?label
{ <http://dbpedia.org/resource/Category:Algorithms> owl:sameAs ?nomePT.
   ?nomePT rdfs:label ?label
    FILTER regex(str(?nomePT), "pt", "i") 
}

However, it returns empty because the variable "?NomePT" always contains the first resource from the list. see:

SELECT ?nomePT
{ <http://dbpedia.org/resource/Category:Algorithms> owl:sameAs ?nomePT.
   ?nomePT rdfs:label ?label
}

But the resource has several sameAs links:

SELECT ?nomePT
{ <http://dbpedia.org/resource/Category:Algorithms> owl:sameAs ?nomePT.}

what's wrong in the query?

thanks in advance.


Solution

  • If you write the query:

    SELECT distinct * {
        ?nomePT owl:sameAs category:Algorithms.
    }
    

    You will get all links that are owl:sameAs your resource category:Algorithms. However, you are already limiting the scope of the triple. So if you ask for:

    SELECT distinct ?nomePT {
        ?nomePT owl:sameAs category:Algorithms.
        ?nomePT rdfs:label ?label
    }
    

    enter image description here

    If first finds your specific resource and then looks for the label only for that resource. However, if you don't bound your triple at the start and only filter your resource will give you all links you need:

    SELECT distinct ?resource {
        ?nomePT owl:sameAs ?resource.
        ?nomePT rdfs:label ?label.
    filter( ?nomePT=category:Algorithms )
    }
    

    enter image description here

    Thus you should first find all the resources and then filter them based on a specific resource such as category:Algorithms and since sameAs is reflexive:

    SELECT distinct * {
        ?nomePT owl:sameAs ?resource.
        ?nomePT rdfs:label ?label.
    filter( ?nomePT=category:Algorithms && regex(str(?resource), "pt", "i") )
    }
    

    enter image description here