Last active
October 26, 2016 15:33
-
-
Save AdamSteffanick/650a7dfb98895e9c69f33e0645f7f66d to your computer and use it in GitHub Desktop.
Open Library (these are inefficient queries intended to teach using functions and an API; please visit https://openlibrary.org/developers/dumps to obtain data for local storage)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "3.1"; | |
let $uri := "http://openlibrary.org/search.json?title=digital+humanities" | |
let $json := fetch:text($uri) | |
return $json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "3.1"; | |
let $uri := "http://openlibrary.org/search.json?title=digital+humanities" | |
let $json := fetch:text($uri) | |
let $xml := fn:json-to-xml($json) | |
return $xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "3.1"; | |
declare namespace xf = "http://www.w3.org/2005/xpath-functions"; | |
let $uri := "http://openlibrary.org/search.json?title=digital+humanities" | |
let $json := fetch:text($uri) | |
let $xml := fn:json-to-xml($json) | |
let $isbns := | |
for $isbn in $xml//xf:array[@key="isbn"]/xf:string/text() | |
return element isbn {$isbn} | |
return $isbns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "3.1"; | |
declare namespace xf = "http://www.w3.org/2005/xpath-functions"; | |
declare function local:get-isbn-by-title($book-title as xs:string) as element()* { | |
let $url := "http://openlibrary.org/search.json?title=" || fn:translate($book-title, " ", "+") | |
let $json := fetch:text($url) | |
let $isbn-data := fn:json-to-xml($json) | |
for $isbn in $isbn-data//xf:array[@key="isbn"]/xf:string/text() | |
return element isbn {$isbn} | |
}; | |
let $book := "Digital Humanities" | |
let $isbns := local:get-isbn-by-title($book) | |
return $isbns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "3.1"; | |
declare function local:get-volume-by-isbn($isbn as xs:string) as element()* { | |
let $url := "http://openlibrary.org/api/volumes/brief/isbn/" || $isbn || ".json" | |
let $json := fetch:text($url) | |
let $volume-data := fn:json-to-xml($json) | |
for $volume in $volume-data | |
return element volume {$volume} | |
}; | |
let $isbn := "0262018470" | |
let $volume := local:get-volume-by-isbn($isbn) | |
return $volume |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "3.1"; | |
declare namespace xf = "http://www.w3.org/2005/xpath-functions"; | |
declare function local:get-isbn-by-title($book-title as xs:string) as element()* { | |
let $url := "http://openlibrary.org/search.json?title=" || fn:translate($book-title, " ", "+") | |
let $json := fetch:text($url) | |
let $isbn-data := fn:json-to-xml($json) | |
for $isbn in $isbn-data//xf:array[@key="isbn"]/xf:string/text() | |
return element isbn {$isbn} | |
}; | |
declare function local:get-volume-by-isbn($isbn as xs:string) as element()* { | |
let $url := "http://openlibrary.org/api/volumes/brief/isbn/" || $isbn || ".json" | |
let $json := fetch:text($url) | |
let $volume-data := fn:json-to-xml($json) | |
for $volume in $volume-data | |
return element volume {$volume} | |
}; | |
let $book := "Digital Humanities" | |
let $records := local:get-isbn-by-title($book) | |
let $volumes := | |
for $record in $records | |
return local:get-volume-by-isbn($record) | |
return $volumes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "3.1"; | |
declare namespace xf = "http://www.w3.org/2005/xpath-functions"; | |
declare function local:get-isbn-by-title($book-title as xs:string) as element()* { | |
let $url := "http://openlibrary.org/search.json?title=" || fn:translate($book-title, " ", "+") | |
let $json := fetch:text($url) | |
let $isbn-data := fn:json-to-xml($json) | |
for $isbn in $isbn-data//xf:array[@key="isbn"]/xf:string/text() | |
return element isbn {$isbn} | |
}; | |
declare function local:get-recorduri-by-isbn($isbn as xs:string) as element()* { | |
let $url := "http://openlibrary.org/api/volumes/brief/isbn/" || $isbn || ".json" | |
let $json := fetch:text($url) | |
let $volume-data := fn:json-to-xml($json) | |
for $record-uri in $volume-data//xf:string[@key="recordURL"]/text() | |
return element record_uri {$record-uri} | |
}; | |
let $book := "Digital Humanities" | |
let $records := local:get-isbn-by-title($book) | |
let $uris := | |
for $record in $records | |
return local:get-recorduri-by-isbn($record) | |
return $uris |
E.g., see my forked version with this change: https://gist.github.com/joewiz/a5bae0eced6f788fb49e34f264c765f7.
Thank you. I've updated my gist and added a note at the top. I don't recommend using these queries for purposes other than learning XQuery.
Very nice, thanks! Really nice work!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for posting! Adding the ".xq" file extension to each file will cause Gist to enable syntax highlighting on your XQuery code.