Page:Aaron Swartz s A Programmable Web An Unfinished Work.pdf/53

CHAPTER 6 41

APIs are nice and all, but they’re fairly limiting: they only give you the answers to questions you already know how to ask. Want to ﬁnd out more about book 3j7is? Sure, it’ll tell you. But want to know which books published recently share an author with a book published over a hundred years ago? That’s a little more complicated.

But, luckily, not impossible. It seems ridiculous to come up with your own API that could answer any sort of question like this. But remember those RDF query languages we were making fun of in the last chapter? This turns out to be just the sort of thing they’re perfect at.

The ofﬁcial RDF query language is called SPARQL (SPARQL Protocol And RDF Query Language—pronounced “sparkle”). If you’re familiar with SQL, the standard database query language, SPARQL will look similar, only with RDF stuck in all the right places. Here’s how you express our previous question in SPARQL:

>   PREFIX : 

>   SELECT ?booknew, ?bookold

>   WHERE {

>     ?booknew :author ?author.

>     ?booknew :publication_year ?yearnew.

>     FILTER ( ?yearnew >= 2008 )

>     ?bookold :author ?author.

>     ?bookold :publication_year ?yearold.

>     FILTER ( ?yearold <= 1908 )

>   }