Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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)
xquery version "3.1";
let $uri := "http://openlibrary.org/search.json?title=digital+humanities"
let $json := fetch:text($uri)
return $json
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
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
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
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
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
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

joewiz commented Oct 24, 2016

Thanks for posting! Adding the ".xq" file extension to each file will cause Gist to enable syntax highlighting on your XQuery code.

joewiz commented Oct 24, 2016

E.g., see my forked version with this change: https://gist.github.com/joewiz/a5bae0eced6f788fb49e34f264c765f7.

Owner

AdamSteffanick commented Oct 26, 2016

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.

joewiz commented Oct 26, 2016

Very nice, thanks! Really nice work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment