Skip to content

Instantly share code, notes, and snippets.

@zopyx
Created February 16, 2015 18:30
Show Gist options
  • Save zopyx/eee05f7a421a6c0b6b80 to your computer and use it in GitHub Desktop.
Save zopyx/eee05f7a421a6c0b6b80 to your computer and use it in GitHub Desktop.
RestXQ script
xquery version "3.0";
module namespace services = "http://my/services";
import module namespace transform = "http://exist-db.org/xquery/transform";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace rest = "http://exquery.org/ns/restxq";
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare namespace json="http://www.json.org";
declare
%rest:GET
%rest:path("/all-documents.html")
%output:media-type("text/html")
%output:method("html5")
function services:home() {
transform:transform(services:func(), doc("/db/scripts/all-documents.xslt"), ())
};
declare
%rest:GET
%rest:path("/all-documents.json")
%rest:produces("application/json")
%output:method("json")
function services:home-json() {
services:func()
};
declare
%rest:GET
%rest:path("/all-documents.xml")
%rest:produces("application/xml")
function services:home-xml() {
services:func()
};
declare function local:trim($arg as xs:string?) as xs:string {
replace(replace($arg,'\s+$',''),'^\s+','')
} ;
declare
%private
function services:func() {
<items> {
let $data-collection := '/db/onkopedia'
for $doc in collection($data-collection)[ends-with(base-uri(.), '.xml')]
return
<item>
<uri>{base-uri($doc)}</uri>
<title>{local:trim($doc//meta[@name="Titel"]/value/text())}</title>
<language>{local:trim($doc//meta[@name="Sprache"]/value/text())}</language>
<document_type>{local:trim($doc//meta[@name="Dokumentart"]/value/text())}</document_type>
<status>{local:trim($doc//meta[@name="Status"]/value/text())}</status>
<id>{local:trim($doc//meta[@name="ID"]/value/text())}</id>
<area>{local:trim($doc//meta[@name="Bereich"]/value/text())}</area>
{
for $node in $doc//meta[@name="MedizinischeKlassifizierung"]/value
return
<medical_id json:array="true">{$node/text()}</medical_id>
}
{
for $node in $doc//meta[@name="Fachgesellschaften"]/value
return
<societies json:array="true">{$node/text()}</societies>
}
{
for $node in $doc//meta[@name="Klassifizierung"]/value
return
<classifications json:array="true">{$node/text()}</classifications>
}
{
for $node in $doc//meta[@name="Autoren"]/value
return <authors json:array="true">{$node/text()}</authors>
}
</item>
}
</items>
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment