disabling the edit link for reference document

xquery version "1.0";

(: ~
 : Module Name:             reference document webpage
 :
 : Module Version           1.0
 :
 : Date                     12th
 :
 : Copyright                The cagrid consortium
 :
 : Module overview          Renders a Data element for viewing by the user of the metadata repository
 :
 :)

(:~
 :    Commented on refactoring SJH
 :
 :    @author Steve Harris
 :    @author Igor Toujilov
 :    @author Sui (Maria) Lin
 :    @since Sept 1st, 2006
 :    @version 3.0
~ :)
declare namespace openMDR = "http://www.cagrid.org/schema/openMDR";
declare namespace ISO11179= "http://www.cagrid.org/schema/ISO11179";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace session="http://exist-db.org/xquery/session";

import module namespace
   lib-util="http://www.cagrid.org/xquery/library/util"
   at "../library/m-lib-util.xquery";

import module namespace
   lib-rendering="http://www.cagrid.org/xquery/library/rendering"
   at "../web/m-lib-rendering.xquery";

declare option exist:serialize "media-type=text/html method=xhtml doctype-public=-//W3C//DTD XHTML 1.0 Transitional//EN doctype-system=http://www.w3.org/TR/2002/REC-xhtml1-20020801/DTD/xhtml1-transitional.dtd";

session:create(),
let $id := request:get-parameter("compound_id", "")

let $item := lib-util:mdrElement("reference_document", $id)
let $title := concat('Reference document: ', $item/openMDR:reference_document_title)
return
   if (request:get-parameter("as-xml",()))
   then (lib-rendering:as-xml($item))
   else (
      if (request:get-parameter("return_doc", ())='true')
      then (
              let $h1 := response:set-header("Content-Disposition", concat('attachment; filename="', $item//openMDR:file_name, '"'))
              return
                 response:stream-binary($item//openMDR:reference_document_uri, $item//openMDR:file_type)
           )
      else
         lib-rendering:txfrm-webpage(
            $title,
            $item,
            false(),
            false(),
            $id,
            'reference_document.xquery','../edit/editReferenceDocument.xquery'))

xquery version "1.0";

(: ~
 : Module Name:             reference document webpage
 :
 : Module Version           1.0
 :
 : Date                     12th
 :
 : Copyright                The cagrid consortium
 :
 : Module overview          Renders a Data element for viewing by the user of the metadata repository
 :
 :)

(:~
 :    Commented on refactoring SJH
 :
 :    @author Steve Harris
 :    @author Igor Toujilov
 :    @author Sui (Maria) Lin
 :    @since Sept 1st, 2006
 :    @version 3.0
~ :)
declare namespace openMDR = "http://www.cagrid.org/schema/openMDR";
declare namespace ISO11179= "http://www.cagrid.org/schema/ISO11179";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace session="http://exist-db.org/xquery/session";

import module namespace
   lib-util="http://www.cagrid.org/xquery/library/util"
   at "../library/m-lib-util.xquery";

import module namespace
   lib-rendering="http://www.cagrid.org/xquery/library/rendering"
   at "../web/m-lib-rendering.xquery";

declare option exist:serialize "media-type=text/html method=xhtml doctype-public=-//W3C//DTD XHTML 1.0 Transitional//EN doctype-system=http://www.w3.org/TR/2002/REC-xhtml1-20020801/DTD/xhtml1-transitional.dtd";

session:create(),
let $id := request:get-parameter("compound_id", "")

let $item := lib-util:mdrElement("reference_document", $id)
let $title := concat('Reference document: ', $item/openMDR:reference_document_title)
return
   if (request:get-parameter("as-xml",()))
   then (lib-rendering:as-xml($item))
   else (
      if (request:get-parameter("return_doc", ())='true')
      then (
              let $h1 := response:set-header("Content-Disposition", concat('attachment; filename="', $item//openMDR:file_name, '"'))
              return
                 response:stream-binary($item//openMDR:reference_document_uri, $item//openMDR:file_type)
           )
      else
         lib-rendering:txfrm-webpage(
            $title,
            $item,
            true(),
            false(),
            $id,
            'reference_document.xquery','../edit/editReferenceDocument.xquery'))


added overview-output, format-parameter

added overview-output, format-parameter

xquery version "1.0";
module namespace smc = "http://clarin.eu/smc";

import module namespace repo-utils = "http://aac.ac.at/content_repository/utils" at  "xmldb:exist:///db/cr/repo-utils.xqm";
import module namespace crday  = "http://aac.ac.at/content_repository/data-ay" at "/db/cr/crday.xqm";
import module namespace fcs = "http://clarin.eu/fcs/1.0" at "/db/cr/fcs.xqm";

declare namespace sru = "http://www.loc.gov/zing/srw/";

declare variable $smc:termsets := doc("data/termsets.xml");
declare variable $smc:dcr-terms := doc("data/dcr-terms.xml");
declare variable $smc:cmd-terms := doc("data/cmd-terms.xml");

(:~ mappings overview
only process already created maps (i.e. (for now) don't run mappings on demand,
because that would induce ay-xml - which takes very long time and is not safe
:)
declare function smc:mappings-overview($config, $format as xs:string) as item()* {

let $cache_path := repo-utils:config-value($config, 'cache.path'),
    $mappings := collection($cache_path)/map[*]

let $overview := if (contains($format, "table")) then

index
                         {for $map in $mappings return {concat($map/@profile-name, ' ', $map/@context)}	 }

                        { for $ix in distinct-values($mappings//index/xs:string(@key))
                             let $datcat := $smc:dcr-terms//Concept[Term[@type='id']/concat(@set,':',text()) = $ix],
                                  $datcat-label := $datcat/Term[@type='mnemonic'],
                                 $datcat-type := $datcat/@datcat-type
                             return
{({$datcat-label},
, concat($ix, ' ', $datcat-type))}
                                        {for $map in $mappings
                                                 let $paths := $map/index[xs:string(@key)=$ix]/path
                                        return
{for $path in $paths
                                                            return
{$path/text()}

                                                         }

                                         }

                           }

                    else

                        { for $ix in distinct-values($mappings//index/xs:string(@key))
                             let $datcat := $smc:dcr-terms//Concept[Term[@type='id']/concat(@set,':',text()) = $ix],
                                  $datcat-label := $datcat/Term[@type='mnemonic'],
                                 $datcat-type := $datcat/@datcat-type
                             return
{({$datcat-label},
, concat($ix, ' ', $datcat-type))}
                                        {for $map in $mappings
                                                 let $paths := $map/index[xs:string(@key)=$ix]/path
                                        return
{for $path in $paths
                                                            return
{$path/text()}

                                                         }

                                         }

                           }

       return if ($format eq 'raw') then
                   $overview
                else
                   repo-utils:serialise-as($overview, $format, 'html', $config, ())
};

(:~ create and store mapping for every profile in given nodeset
expects the CMD-format

@param format [raw, htmlpage, html] - raw: return only the produced table, html* : serialize as html
:)
declare function smc:get-mappings($config, $x-context as xs:string+, $run-flag as xs:boolean, $format as xs:string) as item()* {

let $scan-profiles :=  fcs:scan('cmd.profile', $x-context, 1, 50, 1, 1, "text", $config),
    $target_path := repo-utils:config-value($config, 'cache.path')

(: for every profile in the data-set :)
let $result := for $profile in $scan-profiles//sru:term
                    let $profile-name := xs:string($profile/sru:displayTerm)
                    let $map-doc-name := repo-utils:gen-cache-id("map", ($x-context, $profile-name), '')

                    return
                        if (repo-utils:is-in-cache($map-doc-name, $config) and not($run-flag)) then
                            repo-utils:get-from-cache($map-doc-name, $config)
                        else
                            let $ay-data := crday:get-ay-xml($config, $x-context, $profile-name, $crday:defaultMaxDepth, false(), 'raw')
                            let $mappings := <map profile-id="{$profile/sru:value}" profile-name="{$profile-name}" context="{$x-context}">
                                                {smc:match-paths($ay-data, $profile)}
                                               </map>
                            return repo-utils:store-in-cache($map-doc-name, $mappings, $config)

   return if ($format eq 'raw') then
                   <map context="{$x-context}" >{$result}</map>
                else
                   repo-utils:serialise-as(<map context="{$x-context}" >{$result}</map>, $format, 'default', $config, ())
};

(:~ expects a summary of data, matches the resulting paths with paths in cmd-terms
and returns dcr-indexes, that have a path in the input-data

be kind and try to map on profile-name if profile-id not available (or did not match)
:)
declare function smc:match-paths($ay-data as item(), $profile as node()) as item()* {

let $data-paths := $ay-data//Term/replace(replace(xs:string(@path),'//',''),'/','.')

let $profile-id := xs:string($profile/sru:value)
let $profile-name := xs:string($profile/sru:displayTerm)

let $match-by-id := if ($profile-id ne '') then $smc:cmd-terms//Termset[@id=$profile-id and @type="CMD_Profile"]/Term[xs:string(@path) = $data-paths ] else ()
let $match := if (exists($match-by-id)) then $match-by-id
                    else $smc:cmd-terms//Termset[xs:string(@name)=$profile-name and @type="CMD_Profile"]/Term[xs:string(@path) = $data-paths ]

let $mapping := for $datcat in distinct-values($match//xs:string(@datcat))
                    let $key := smc:shorten-uri($datcat)
                    return <index key="{$key}" >
                                { for $path in $match[xs:string(@datcat) = $datcat]/xs:string(@path)
                                    return <path count="">{$path}</path>
                                }
                            </index>
return $mapping
};

(:~ replace url_prefix in the url by the short key
based on definitions in $smc:termsets
:)
declare function smc:shorten-uri($url as xs:string) as xs:string {
    let $termset := $smc:termsets//Termset[url_prefix][starts-with($url, url_prefix)][$url ne '']
    let $url-suffix := substring-after ($url, $termset/url_prefix)
    return if (exists($termset)) then concat($termset/key, ':', $url-suffix) else $url
};

xquery version "1.0";
module namespace smc = "http://clarin.eu/smc";

import module namespace repo-utils = "http://aac.ac.at/content_repository/utils" at  "xmldb:exist:///db/cr/repo-utils.xqm";
import module namespace crday  = "http://aac.ac.at/content_repository/data-ay" at "/db/cr/crday.xqm";
import module namespace fcs = "http://clarin.eu/fcs/1.0" at "/db/cr/fcs.xqm";

declare namespace sru = "http://www.loc.gov/zing/srw/";

declare variable $smc:termsets := doc("data/termsets.xml");
declare variable $smc:cmd-terms := doc("data/cmd-terms.xml");

(:~ create and store mapping for every profile in given nodeset
expects the CMD-format
:)
declare function smc:get-mappings($x-context as xs:string, $config, $run-flag as xs:boolean) as item()* {

let $scan-profiles :=  fcs:scan('cmd.profile', $x-context, 1, 50, 1, 1, "text", $config),
    $target_path := repo-utils:config-value($config, 'cache.path')

(: for every profile in the data-set :)
for $profile in $scan-profiles//sru:term
    let $profile-name := xs:string($profile/sru:displayTerm)
    let $map-doc-name := repo-utils:gen-cache-id("map", ($x-context, $profile-name), '')

    let $result :=
        if (repo-utils:is-in-cache($map-doc-name, $config) and not($run-flag)) then
            repo-utils:get-from-cache($map-doc-name, $config)
        else
            let $ay-data := crday:get-ay-xml($config, $x-context, $profile-name, $crday:defaultMaxDepth, false(), 'raw')
            let $mappings := <map profile-id="{$profile/sru:value}" profile-name="{$profile-name}" context="{$x-context}">
                                {smc:match-paths($ay-data, $profile/sru:value/text())}
                               </map>
            return repo-utils:store-in-cache($map-doc-name, $mappings, $config)
   return $result
(:  TODO?: add html-serialization
    return repo-utils:serialise-as($result, 'htmlpage', 'terms', $config, ()):)
};

(:~ expects a summary of data, matches the resulting paths with paths in cmd-terms
and returns dcr-indexes, that have a path in the input-data
:)
declare function smc:match-paths($ay-data as item(), $profile-id as xs:string) as item()* {

let $data-paths := $ay-data//Term/replace(replace(xs:string(@path),'//',''),'/','.')
let $match := $smc:cmd-terms//Termset[@id=$profile-id and @type="CMD_Profile"]/Term[xs:string(@path) = $data-paths ]

let $mapping := for $datcat in distinct-values($match//xs:string(@datcat))
                    let $key := smc:shorten-uri($datcat)
                    return <index key="{$key}" >
                                { for $path in $match[xs:string(@datcat) = $datcat]/xs:string(@path)
                                    return <path>{$path}</path>
                                }
                            </index>
return $mapping
};

(:~ replace url_prefix in the url by the short key
based on definitions in $smc:termsets
:)
declare function smc:shorten-uri($url as xs:string) as xs:string {
    let $termset := $smc:termsets//Termset[url_prefix][starts-with($url, url_prefix)][$url ne '']
    let $url-suffix := substring-after ($url, $termset/url_prefix)
    return if (exists($termset)) then concat($termset/key, ':', $url-suffix) else $url
};


fix

fix

svn path=/trunk/eXist/; revision=6752

xquery version "1.0";
(: $Id$ :)
(:
    Module: examples setup
:)

module namespace setup="http://exist-db.org/xquery/admin-interface/setup";

declare namespace xdb="http://exist-db.org/xquery/xmldb";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace session="http://exist-db.org/xquery/session";
declare namespace util="http://exist-db.org/xquery/util";
declare namespace system="http://exist-db.org/xquery/system";

declare function setup:main() as element()
{

Examples Setup

        {
            let $action := request:get-parameter("action", ()) return
                if($action) then
                (
                    if($action eq "Import Example Data") then
                    (
                        setup:importLocal()
                    )
                    else if($action eq "Import Remote Files") then
                    (
                        setup:importFromURLs()
                    )
                    else
                    (
                        setup:page3()
                    )
                )
                else
                (
                    setup:page1()
                )
        }

};

declare function setup:importLocal() as element()+
{
    let $home := system:get-exist-home(),
    $pathSep := util:system-property("file.separator"),
    $dir := if (doc-available(concat("file:///", $home, "/exist-samples/examples.xml"))) then
        (
            concat($home, $pathSep, "exist-samples")
        )
        else if(ends-with($home, "WEB-INF")) then
        (
            concat(substring-before($home, "WEB-INF"), "samples")
        )
        else
        (
            concat($home, $pathSep, "samples")
        )
    return (
        setup:page2(),

Loading from directory: {$dir}.

Actions:

            {
                setup:create-collection("/db", "shakespeare"),
                setup:create-collection("/db/shakespeare", "plays"),
                setup:store-files("/db/shakespeare/plays", $dir,  "shakespeare/*.xml", "text/xml"),
                setup:store-files("/db/shakespeare/plays", $dir, "shakespeare/*.xsl", "text/xml"),
                setup:store-files("/db/shakespeare/plays", $dir, "shakespeare/*.css", "text/css"),
                setup:create-collection("/db", "xinclude"),
                setup:store-files("/db/xinclude", $dir, "xinclude/*.xsl", "text/xml"),
                setup:store-files("/db/xinclude", $dir, "xinclude/*.xml", "text/xml"),
                setup:store-files("/db/xinclude", $dir, "xinclude/*.xq", "application/xquery"),
                setup:store-files("/db/xinclude", $dir, "xinclude/*.jpg", "image/jpeg"),
                setup:store-files("/db/xinclude", $dir, "xinclude/*.css", "text/css"),
                setup:create-collection("/db", "library"),
                setup:store-files("/db/library", $dir, "*.rdf", "text/xml"),
                setup:create-collection("/db", "mods"),
                setup:store-files("/db/mods", $dir, "mods/*.xml", "text/xml"),
                setup:store-files("/db", $dir, "*.xml", "text/xml"),
                setup:create-collection("/db/system/config", "db"),
                setup:create-collection("/db/system/config/db", "mondial"),
                setup:store-files("/db/system/config/db/mondial", $dir, "mondial.xconf", "text/xml")
            }

    )
};

declare function setup:importFromURLs() as element()+
{
    (
        setup:page3(),

Actions:

            {
                let $includeXmlad := request:get-parameter("xmlad", ()),
                $includeMondial := request:get-parameter("mondial", ()) return
                (
                    if($includeXmlad) then
                    (
                        setup:create-collection("/db", "xmlad"),
                        setup:load-URL("/db/xmlad", xs:anyURI("http://surfnet.dl.sourceforge.net/sourceforge/xmlad/xmlad.xml"), "xmlad.xml")
                    )else (),
                    if($includeMondial) then
                    (
                        setup:create-collection("/db", "mondial"),
                        setup:load-URL("/db/mondial", xs:anyURI("http://www.dbis.informatik.uni-goettingen.de/Mondial/mondial-europe.xml"), "mondial.xml")
                    )else ()
                )
            }

    )
};

declare function setup:load-URL($collection as xs:string, $url as xs:anyURI, $docName as xs:string) as element()
{
    let $x := xdb:store($collection, $docName, $url) return

File {$docName} imported from url: {$url}

};

declare function setup:store-files($collection as xs:string, $home as xs:string, $patterns as xs:string, $mimeType as xs:string) as element()*
{
    let $stored := xdb:store-files-from-pattern($collection, $home, $patterns, $mimeType)
    for $doc in $stored return

Uploaded: {$doc}

};

declare function setup:create-collection($parent as xs:string, $name as xs:string) as element()
{
    let $col := xdb:create-collection($parent, $name) return

Created collection: {$col}

};

declare function setup:page1() as element()
{
    <form action="{session:encode-url(request:get-uri())}" method="post">

eXist ships with a number of XQuery examples. Some of these
        require certain documents to be stored in the database. Clicking on the button
        below will import the required data from the samples directory:

        <input type="submit" name="action" value="Import Example Data"/>
        <input type="hidden" name="panel" value="setup"/>
    </form>
};

declare function setup:page2() as element()
{
    <form action="{session:encode-url(request:get-uri())}" method="post">

The XQuery examples also use some XML data not included with the distribution.
        I can try to download the corresponding data. Do you want me to do so?

        <input type="checkbox" name="xmlad" checked="true"/>
        The XML Acronym Demystifier
        (approx. 384K)

        <input type="checkbox" name="mondial"/>
        The Mondial Database (Europe)
        (approx. 410K)

<input type="submit" name="action" value="Import Remote Files"/>
        <input type="submit" name="action" value="Skip"/>

        <input type="hidden" name="panel" value="setup"/>
    </form>
};

declare function setup:page3() as element()
{

Files have been loaded. You can now go to the
    examples page.

};

xquery version "1.0";
(: $Id$ :)
(:
    Module: examples setup
:)

module namespace setup="http://exist-db.org/xquery/admin-interface/setup";

declare namespace xdb="http://exist-db.org/xquery/xmldb";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace session="http://exist-db.org/xquery/session";
declare namespace util="http://exist-db.org/xquery/util";
declare namespace system="http://exist-db.org/xquery/system";

declare function setup:main() as element()
{

Examples Setup

        {
            let $action := request:get-parameter("action", ()) return
                if($action) then
                (
                    if($action eq "Import Example Data") then
                    (
                        setup:importLocal()
                    )
                    else if($action eq "Import Remote Files") then
                    (
                        setup:importFromURLs()
                    )
                    else
                    (
                        setup:page3()
                    )
                )
                else
                (
                    setup:page1()
                )
        }

};

declare function setup:importLocal() as element()+
{
    let $home := system:get-exist-home(),
    $pathSep := util:system-property("file.separator"),
    $dir := if (doc-available(concat("file:///", $home, "/exist-samples/examples.xml"))) then
        (
            concat($home, $pathSep, "exist-samples")
        )
        else if(ends-with($home, "WEB-INF")) then
        (
            concat(substring-before($home, "WEB-INF"), "samples")
        )
        else
        (
            concat($home, $pathSep, "samples")
        )
    return (
        setup:page2(),

Loading from directory: {$dir}.

Actions:

            {
                setup:create-collection("/db", "shakespeare"),
                setup:create-collection("/db/shakespeare", "plays"),
                setup:store-files("/db/shakespeare/plays", $dir,  "shakespeare/*.xml", "text/xml"),
                setup:store-files("/db/shakespeare/plays", $dir, "shakespeare/*.xsl", "text/xml"),
                setup:store-files("/db/shakespeare/plays", $dir, "shakespeare/*.css", "text/css"),
                setup:create-collection("/db", "xinclude"),
                setup:store-files("/db/xinclude", $dir, "xinclude/*.xsl", "text/xml"),
                setup:store-files("/db/xinclude", $dir, "xinclude/*.xml", "text/xml"),
                setup:store-files("/db/xinclude", $dir, "xinclude/*.xq", "application/xquery"),
                setup:store-files("/db/xinclude", $dir, "xinclude/*.jpg", "image/jpeg"),
                setup:store-files("/db/xinclude", $dir, "xinclude/*.css", "text/css"),
                setup:create-collection("/db", "library"),
                setup:store-files("/db/library", $dir, "*.rdf", "text/xml"),
                setup:create-collection("/db", "mods"),
                setup:store-files("/db/mods", $dir, "mods/*.xml", "text/xml"),
                setup:store-files("/db", $dir, "*.xml", "text/xml"),
                setup:create-collection("/db/system/config", "db"),
                setup:create-collection("/db/system/config/db", "mondial"),
                setup:store-files("/db/system/config/db/mondial", $dir, "mondial.xconf", "text/xml")
            }

    )
};

declare function setup:importFromURLs() as element()+
{
    (
        setup:page3(),

Actions:

            {
                let $includeXmlad := request:get-parameter("xmlad", ()),
                $includeMondial := request:get-parameter("mondial", ()) return
                (
                    if($includeXmlad) then
                    (
                        setup:create-collection("/db", "xmlad"),
                        setup:load-URL("/db/xmlad", "http://surfnet.dl.sourceforge.net/sourceforge/xmlad/xmlad.xml", "xmlad.xml")
                    )else (),
                    if($includeMondial) then
                    (
                        setup:create-collection("/db", "mondial"),
                        setup:load-URL("/db/mondial", "http://www.dbis.informatik.uni-goettingen.de/Mondial/mondial-europe.xml", "mondial.xml")
                    )else ()
                )
            }

    )
};

declare function setup:load-URL($collection as xs:string, $url as xs:anyURI, $docName as xs:string) as element()
{
    let $x := xdb:store($collection, $docName, $url) return

File {$docName} imported from url: {$url}

};

declare function setup:store-files($collection as xs:string, $home as xs:string, $patterns as xs:string, $mimeType as xs:string) as element()*
{
    let $stored := xdb:store-files-from-pattern($collection, $home, $patterns, $mimeType)
    for $doc in $stored return

Uploaded: {$doc}

};

declare function setup:create-collection($parent as xs:string, $name as xs:string) as element()
{
    let $col := xdb:create-collection($parent, $name) return

Created collection: {$col}

};

declare function setup:page1() as element()
{
    <form action="{session:encode-url(request:get-uri())}" method="post">

eXist ships with a number of XQuery examples. Some of these
        require certain documents to be stored in the database. Clicking on the button
        below will import the required data from the samples directory:

        <input type="submit" name="action" value="Import Example Data"/>
        <input type="hidden" name="panel" value="setup"/>
    </form>
};

declare function setup:page2() as element()
{
    <form action="{session:encode-url(request:get-uri())}" method="post">

The XQuery examples also use some XML data not included with the distribution.
        I can try to download the corresponding data. Do you want me to do so?

        <input type="checkbox" name="xmlad" checked="true"/>
        The XML Acronym Demystifier
        (approx. 384K)

        <input type="checkbox" name="mondial"/>
        The Mondial Database (Europe)
        (approx. 410K)

<input type="submit" name="action" value="Import Remote Files"/>
        <input type="submit" name="action" value="Skip"/>

        <input type="hidden" name="panel" value="setup"/>
    </form>
};

declare function setup:page3() as element()
{

Files have been loaded. You can now go to the
    examples page.

};


alphaebtize test documents

alphaebtize test documents

module namespace page = 'http://basex.org/modules/web-page';
import module namespace csd = "urn:ihe:iti:csd:2013" at "../repo/csd_base_library.xqm";
import module namespace csr_proc = "https://github.com/his-interop/openinfoman/csr_proc" at "../repo/csr_processor.xqm";

declare
  %rest:path("/CSD/test/{$test}")
  %rest:GET
  function page:test(
    $test as xs:string)
{
let $test_doc := page:get_test_doc($test)
return if ($test_doc) then
 for $doc in collection('provider_directory')
 where matches(document-uri($doc), 'providers.xml')
 return csr_proc:process_CSR($test_doc/csd:careServicesRequest,$doc)
else

Shame on you

};

declare
  %rest:path("/CSD/test_source/{$test}")
  %rest:GET
  %output:method("xml")
  function page:test_source(
    $test as xs:string)
{
let $test_doc := page:get_test_doc($test)
return if ($test_doc) then
 $test_doc
else

Shame on you

};

declare function page:get_test_doc($test) {
let $dir_base := "../test_docs/"
let $dir := file:resolve-path($dir_base)
let $file := file:resolve-path(concat($dir_base,$test,".xml"))
return if (starts-with($file,$dir) and file:exists($file))
then
  doc($file)
else
  ()
};

declare
  %rest:path("/CSD/test")
  %rest:GET
  %output:method("xhtml")
  function page:list_tests()
{

Tests

        {for $test_doc in file:list("../test_docs/",boolean('false'),"*.xml")
	 order by $test_doc
	 let $test := file:base-name($test_doc,".xml")
         return

	   {$test}: (process on server)
	    (see source doc)

        }

};

module namespace page = 'http://basex.org/modules/web-page';
import module namespace csd = "urn:ihe:iti:csd:2013" at "../repo/csd_base_library.xqm";
import module namespace csr_proc = "https://github.com/his-interop/openinfoman/csr_proc" at "../repo/csr_processor.xqm";

declare
  %rest:path("/CSD/test/{$test}")
  %rest:GET
  function page:test(
    $test as xs:string)
{
let $test_doc := page:get_test_doc($test)
return if ($test_doc) then
 for $doc in collection('provider_directory')
 where matches(document-uri($doc), 'providers.xml')
 return csr_proc:process_CSR($test_doc/csd:careServicesRequest,$doc)
else

Shame on you

};

declare
  %rest:path("/CSD/test_source/{$test}")
  %rest:GET
  %output:method("xml")
  function page:test_source(
    $test as xs:string)
{
let $test_doc := page:get_test_doc($test)
return if ($test_doc) then
 $test_doc
else

Shame on you

};

declare function page:get_test_doc($test) {
let $dir_base := "../test_docs/"
let $dir := file:resolve-path($dir_base)
let $file := file:resolve-path(concat($dir_base,$test,".xml"))
return if (starts-with($file,$dir) and file:exists($file))
then
  doc($file)
else
  ()
};

declare
  %rest:path("/CSD/test")
  %rest:GET
  %output:method("xhtml")
  function page:list_tests()
{

Tests

        {for $test_doc in file:list("../test_docs/",boolean('false'),"*.xml")
	 let $test := file:base-name($test_doc,".xml")
         return

	   {$test}: (process on server)
	    (see source doc)

        }

};


Update q5.xq

Update q5.xq

let $p := doc("posting.xml")
let $r := doc("resume.xml")

let $resumes := count($r//resume)

return
	distinct-values(
		for $rs in $p//reqSkill
		let $s := $rs/@what

		let $resumesWithSkill := count($r//skill[@what = $s])
		let $resumesAboveThree := count($r//skill[@what = $s and @level > 3])

		where ($resumes div 2) > $resumesWithSkill or ($resumesWithSkill div 2) > $resumesAboveThree
		return $rs/../@pID
	)

declare function get-skill-num() as element()*{
	for $p in doc("posting.xml")//posting, $r in doc("resume.xml")//resume
	let $skills := $p/reqSkill
	for $s in $skills
	return if($r//skill[@what=$s/@what])
		   then <post pID="{$p/@pID}" skill="{$s/@what}" />
		   else () )
}

for posting$ in doc("posting.xml"), x$ in get-skill-num()
let $resumes := doc("resume.xml")
let $count := count($resume)
return data($count)