5

You can run the query below at the Linked MDB SPARQL Explorer. The values of ?imdbID (the last variable) contains IRIs from one of three possible domains (freebase.com, rottentomatoes.com or imdb.com). I would like to know how to apply a filter such that only the rows from the imdb.com domain are retained.

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX movie: <http://data.linkedmdb.org/resource/movie/>

SELECT ?title ?date ?director ?imdbID 
WHERE {
 ?film foaf:page ?imdbID.  
 ?film dc:title ?title.
 ?film dc:date ?date .
 ?film movie:director ?directorURI.
 ?directorURI rdfs:label ?director .
}
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Ihe Onwuka
  • 467
  • 1
  • 3
  • 11
  • 1
    Does the technique in [Exclude results from DBpedia SPARQL query based on URI prefix](http://stackoverflow.com/q/19044871/1281433) work for you? With it, you'd just `FILTER ( strstarts(str(?imdbID), "http://imdb.com") )`, I think. – Joshua Taylor Jun 12 '14 at 12:14
  • 1
    Oh, the linkedMDB endpoint doesn't support SPARQL 1.1, so you'll have to use regex, it appears: `filter( regex( str(?imdbID), "^http://www.imdb.com" ) )`. That approach is also listed in the other question. – Joshua Taylor Jun 12 '14 at 12:21

1 Answers1

5

Not sure why this wasn't posted by @JoshuaTaylor as an answer but adding @JoshuaTaylor's filter to your query is what you are requesting:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/terms/>
PREFIX movie: <http://data.linkedmdb.org/resource/movie/>

SELECT ?title ?date ?director ?imdbID 
WHERE {
 ?film foaf:page ?imdbID.  
 ?film dc:title ?title.
 ?film dc:date ?date .
 ?film movie:director ?directorURI.
 ?directorURI rdfs:label ?director .
 FILTER(regex(str(?imdbID), "www.imdb.com" ) )
}

Which returns:

<?xml version="1.0"?>
<sparql
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:xs="http://www.w3.org/2001/XMLSchema#"
    xmlns="http://www.w3.org/2005/sparql-results#" >
  <head>
    <variable name="title"/>
    <variable name="date"/>
    <variable name="director"/>
    <variable name="imdbID"/>
  </head>
  <results>
    <result>
      <binding name="title">
        <literal>Buffy the Vampire Slayer</literal>
      </binding>
      <binding name="date">
        <literal>1992-07-31</literal>
      </binding>
      <binding name="director">
        <literal>Fran Rubel Kuzui (Director)</literal>
      </binding>
      <binding name="imdbID">
        <uri>http://www.imdb.com/title/tt0103893</uri>
      </binding>
    </result>
    <result>
      <binding name="title">
        <literal>Batman</literal>
      </binding>
      <binding name="date">
        <literal>1989-06-23</literal>
      </binding>
      <binding name="director">
        <literal>Tim Burton (Director)</literal>
      </binding>
      <binding name="imdbID">
        <uri>http://www.imdb.com/title/tt0096895</uri>
      </binding>
    </result>
    <result>
      <binding name="title">
        <literal>Batman</literal>
      </binding>
      <binding name="date">
        <literal>1966-07-30</literal>
      </binding>
      <binding name="director">
        <literal>Leslie H. Martinson (Director)</literal>
      </binding>
      <binding name="imdbID">
        <uri>http://www.imdb.com/title/tt0060153</uri>
      </binding>
    </result>
    <result>
      <binding name="title">
        <literal>Batman &amp; Robin</literal>
      </binding>
      <binding name="date">
        <literal>1997-06-20</literal>
      </binding>
      <binding name="director">
        <literal>Joel Schumacher (Director)</literal>
      </binding>
      <binding name="imdbID">
        <uri>http://www.imdb.com/title/tt0118688</uri>
      </binding>
    </result>
    <result>
      <binding name="title">
        <literal>Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb</literal>
      </binding>

If you want to make it case insensitive you can use the i flag like so:

FILTER(regex(str(?imdbID), "www.IMDB.com", "i" )

If you want to see some more information on filter have a look at 3.1 Restricting the Values of Strings.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Noelkd
  • 7,686
  • 2
  • 29
  • 43
  • 1
    **Not sure why this wasn't posted by @JoshuaTaylor as an answer** I didn't post it, because if this approach works for OP, then this question should be closed as a duplicate of that question (since this technique was presented in the answer to that question). – Joshua Taylor Nov 03 '15 at 15:27