Search code examples
selectsparqlrdf

SPARQL select based on the content of the subject


I'm new to SPARQL, and I am trying to select a property based on the content of the subject. For example, using the RDF data below, I want to return the result containing "var2_1":

<rdf:Description rdf:about="http://www.ontologydesignpatterns.org/ont/fred/domain.owl#var0">
    <rdf:type rdf:resource="http://www.ontologydesignpatterns.org/ont/fred/domain.owl#var2_0"/>
</rdf:Description>
<rdf:Description rdf:about="http://www.ontologydesignpatterns.org/ont/fred/domain.owl#var1">
    <rdf:type rdf:resource="http://www.ontologydesignpatterns.org/ont/fred/domain.owl#var2_1"/>
</rdf:Description>

This is the query I am writing, but it returns nothing, and I can't seem to find a way to specify that the subject should contain "var1":

SELECT ?t 
WHERE {
   ?s rdf:type ?t
   FILTER regex(?s, "var1")
}

I would appreciate help on the right way to do this.


Solution

  • The subject is not a string literal value, but an IRI: http://www.ontologydesignpatterns.org/ont/fred/domain.owl#var1. To match this, you should not be using a regular expression, but instead use the actual IRI itself:

    SELECT ?t 
    WHERE {
      ?s rdf:type ?t
      FILTER(?s =  <http://www.ontologydesignpatterns.org/ont/fred/domain.owl#var1>)
    }
    

    or more succinct:

    SELECT ?t 
    WHERE {
       <http://www.ontologydesignpatterns.org/ont/fred/domain.owl#var1> rdf:type ?t
    }