Skip to content

Instantly share code, notes, and snippets.

@CliffordAnderson
Last active March 21, 2016 13:41
Show Gist options
  • Save CliffordAnderson/65383ef72d000091995c to your computer and use it in GitHub Desktop.
Save CliffordAnderson/65383ef72d000091995c to your computer and use it in GitHub Desktop.
Just a quick example of searching the DPLA API using XQuery and returning a simple webpage.
module namespace page = 'http://library.vanderbilt.edu/dpla';
(: Just a quick example of searching the DPLA API
and returning a simple webpage. :)
declare
%rest:path("dpla/{$search}")
%rest:query-param("key", "{$key}")
%output:method("html")
function page:html($search as xs:string, $key as xs:string)
{
let $json-ld := fetch:text("http://api.dp.la/v2/items?q=" || $search || "&api_key=" || $key)
let $xml-data := json:parse($json-ld)
let $images :=
for $obj in ($xml-data/json/docs/_)
let $isShownAt := ($obj//isShownAt/text())[1] (: We only want a single result :)
let $title := ($obj//originalRecord/titleInfo/title/text(),$obj//title[@type="array"]/_/text(), "[Untitled]")[1] (: Takes the first good result or supplies "[Untited]" if there are none. :)
let $thumbnail := ($obj//_0040thumbnail/text(), $obj/object/text())[1]
where $thumbnail (: Still getting some results with urls that are not really to thubmnails :)
return <li>{$title}<br/><a href="{$isShownAt}"><img src="{$thumbnail}"/></a></li>
return
<html>
<head>
<title>DPLA Data</title>
</head>
<body>
<p>
<ul>{$images}</ul>
</p>
</body>
</html>
};
module namespace page = 'http://library.vanderbilt.edu/dpla';
(: Just a quick example of searching the DPLA API
and returning a simple webpage. :)
declare
%rest:path("dpla/{$search}")
%rest:query-param("key", "{$key}")
%output:method("html")
function page:html($search as xs:string, $key as xs:string)
{
let $json-ld := fn:json-doc("http://api.dp.la/v2/items?q=" || $search || "&amp;api_key=" || $key)
let $images :=
for $obj in $json-ld?docs?*
where $obj?isShownAt
let $isShownAt := $obj?isShownAt
let $object := $obj?object
let $title :=
if (exists($obj?originalRecord?titleInfo?title)) then $obj?originalRecord?titleInfo?title
else ("[Untitled]")
return <li>{$title}<br/><a href="{$isShownAt}"><img src="{$object}"/></a></li>
return
<html>
<head>
<title>DPLA Data</title>
</head>
<body>
<p>
<ul>{$images}</ul>
</p>
</body>
</html>
};
module namespace page = 'http://library.vanderbilt.edu/dpla';
(: Just a quick example of searching the DPLA API
and returning a simple webpage. :)
declare
%rest:path("dpla")
%output:method("html")
function page:html()
{
let $json-ld := fn:json-doc("http://api.dp.la/v2/items?q=weasels&amp;api_key=YOUR-KEY-HERE")
let $images :=
for $obj in $json-ld?docs?*
where $obj?isShownAt
let $isShownAt := $obj?isShownAt
let $object := $obj?object
let $title := $obj?originalRecord?titleInfo?title
return <li>{$title}<br/><a href="{$isShownAt}"><img src="{$object}"/></a></li>
return
<html>
<head>
<title>DPLA Data</title>
</head>
<body>
<p>
<ul>{$images}</ul>
</p>
</body>
</html>
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment