2014-06-12 113 views
1

您可以在Linked MDB SPARQL Explorer處運行以下查詢。 ?imdbID(最後一個變量)的值包含來自三個可能域(freebase.com,rottentomatoes.com或imdb.com)之一的IRI。我想知道如何應用filter,以便只保留來自imdb.com域的行。基於SParQL中的URI進行過濾

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 . 
} 
+1

在[排除基於URI前綴的DBpedia SPARQL查詢結果](http://stackoverflow.com/q/19044871/1281433)中的技術是否適合您?有了它,你只需'過濾(strstarts(str(?imdbID),「http://imdb.com」))',我想。 –

+1

哦,linkedMDB端點不支持SPARQL 1.1,所以你必須使用正則表達式,它顯示:'filter(regex(str(?imdbID),「^ http://www.imdb.com」)) '。該方法也列在其他問題中。 –

回答

1

不知道爲什麼,這不是張貼@JoshuaTaylor作爲一個答案,但添加@ JoshuaTaylor的過濾器來查詢是您所要求的內容:

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")) 
} 

將返回:

<?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> 

如果你想讓它不區分大小寫,你可以使用如下標誌:

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

如果你想了解更多關於過濾器的信息,請看3.1 Restricting the Values of Strings

+1

**不知道爲什麼這不是由@JoshuaTaylor發佈作爲答案**我沒有發佈它,因爲如果這種方法適用於OP,那麼這個問題應該作爲該問題的重複來關閉(因爲這種技術在這個問題的答案中提出)。 –