Skip to content

Instantly share code, notes, and snippets.

@AdamSteffanick
Last active October 26, 2016 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AdamSteffanick/650a7dfb98895e9c69f33e0645f7f66d to your computer and use it in GitHub Desktop.
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)
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
Copy link

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
Copy link

joewiz commented Oct 24, 2016

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

@AdamSteffanick
Copy link
Author

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
Copy link

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