Use the XQuery function syntax for the eXist-db scheduler built-in functions.

xquery version "3.0";
(:~
 : eXist-db Quartz Scheduler module functions
 :
 : @see http://exist-db.org/exist/apps/fundocs/view.html?uri=http://exist-db.org/xquery/scheduler&location=java:org.exist.xquery.modules.scheduler.SchedulerModule&details=true
 :)
module namespace scheduler = "http://exist-db.org/xquery/scheduler";

declare namespace a = "http://reecedunn.co.uk/xquery/annotations";
declare namespace o = "http://reecedunn.co.uk/xquery/options";

declare %a:since("exist", "4.4") function scheduler:delete-scheduled-job($job-name as xs:string) as xs:boolean external;
declare %a:since("exist", "4.4") function scheduler:get-scheduled-jobs() as node() external;
declare %a:since("exist", "4.4") function scheduler:pause-scheduled-job($job-name as xs:string) as xs:boolean external;
declare %a:since("exist", "4.4") function scheduler:resume-scheduled-job($job-name as xs:string) as xs:boolean external;
declare %a:since("exist", "4.4") function scheduler:schedule-java-cron-job($java-classname as xs:string, $cron-expression as xs:string, $job-name as xs:string) as xs:boolean external;
declare %a:since("exist", "4.4") function scheduler:schedule-java-cron-job($java-classname as xs:string, $cron-expression as xs:string, $job-name as xs:string, $job-parameters as element()?) as xs:boolean external;
declare %a:since("exist", "4.4") function scheduler:schedule-java-periodic-job($java-classname as xs:string, $period as xs:integer, $job-name as xs:string, $job-parameters as element()?, $delay as xs:integer, $repeat as xs:integer) as xs:boolean external;
declare %a:since("exist", "4.4") function scheduler:schedule-xquery-cron-job($xquery-resource as xs:string, $cron-expression as xs:string, $job-name as xs:string) as xs:boolean external;
declare %a:since("exist", "4.4") function scheduler:schedule-xquery-cron-job($xquery-resource as xs:string, $cron-expression as xs:string, $job-name as xs:string, $job-parameters as element()?) as xs:boolean external;
declare %a:since("exist", "4.4") function scheduler:schedule-xquery-cron-job($xquery-resource as xs:string, $cron-expression as xs:string, $job-name as xs:string, $job-parameters as element()?, $unschedule as xs:boolean) as xs:boolean external;
declare %a:since("exist", "4.4") function scheduler:schedule-xquery-periodic-job($xquery-resource as xs:string, $period as xs:integer, $job-name as xs:string, $job-parameters as element()?, $delay as xs:integer, $repeat as xs:integer) as xs:boolean external;
declare %a:since("exist", "4.4") function scheduler:schedule-xquery-periodic-job($xquery-resource as xs:string, $period as xs:integer, $job-name as xs:string, $job-parameters as element()?, $delay as xs:integer, $repeat as xs:integer, $unschedule as xs:boolean) as xs:boolean external;

xquery version "3.0";
(:~
 : eXist-db Quartz Scheduler module functions
 :
 : @see http://exist-db.org/exist/apps/fundocs/view.html?uri=http://exist-db.org/xquery/scheduler&location=java:org.exist.xquery.modules.scheduler.SchedulerModule&details=true
 :)
module namespace scheduler = "http://exist-db.org/xquery/scheduler";

declare namespace a = "http://reecedunn.co.uk/xquery/annotations";
declare namespace o = "http://reecedunn.co.uk/xquery/options";

scheduler:delete-scheduled-job($job-name as xs:string) as xs:boolean
scheduler:get-scheduled-jobs() as node()
scheduler:pause-scheduled-job($job-name as xs:string) as xs:boolean
scheduler:resume-scheduled-job($job-name as xs:string) as xs:boolean
scheduler:schedule-java-cron-job($java-classname as xs:string, $cron-expression as xs:string, $job-name as xs:string) as xs:boolean
scheduler:schedule-java-cron-job($java-classname as xs:string, $cron-expression as xs:string, $job-name as xs:string, $job-parameters as element()?) as xs:boolean
scheduler:schedule-java-periodic-job($java-classname as xs:string, $period as xs:integer, $job-name as xs:string, $job-parameters as element()?, $delay as xs:integer, $repeat as xs:integer) as xs:boolean
scheduler:schedule-xquery-cron-job($xquery-resource as xs:string, $cron-expression as xs:string, $job-name as xs:string) as xs:boolean
scheduler:schedule-xquery-cron-job($xquery-resource as xs:string, $cron-expression as xs:string, $job-name as xs:string, $job-parameters as element()?) as xs:boolean
scheduler:schedule-xquery-cron-job($xquery-resource as xs:string, $cron-expression as xs:string, $job-name as xs:string, $job-parameters as element()?, $unschedule as xs:boolean) as xs:boolean
scheduler:schedule-xquery-periodic-job($xquery-resource as xs:string, $period as xs:integer, $job-name as xs:string, $job-parameters as element()?, $delay as xs:integer, $repeat as xs:integer) as xs:boolean
scheduler:schedule-xquery-periodic-job($xquery-resource as xs:string, $period as xs:integer, $job-name as xs:string, $job-parameters as element()?, $delay as xs:integer, $repeat as xs:integer, $unschedule as xs:boolean) as xs:boolean


Added join for all stations.

Added join for all stations.

(: XQuery Join Aggregate Query :)
(: Find the lowest recorded temperature (TMIN) for each station on 2000-01-01.:)
let $station_collection := "/tmp/1.0_partition_ghcnd_all_xml/stations"
for $s in collection($station_collection)/stationCollection/station

let $sensor_collection := "/tmp/1.0_partition_ghcnd_all_xml/sensors"
for $r in collection($sensor_collection)/dataCollection/data

let $date := xs:date(fn:substring(xs:string(fn:data($r/date)), 0, 11))
where $s/id eq $r/station
    and $r/dataType eq "TMAX"
    and $date eq xs:date("2000-01-01")
return ($s/displayName, $r/value)

(: XQuery Join Aggregate Query :)
(: Find the lowest recorded temperature (TMIN) for each station 2001.         :)
let $station_collection := "/tmp/1.0_partition_ghcnd_all_xml/stations"
for $s in collection($station_collection)/stationCollection/stations

return fn:min(
    let $sensor_collection := "/tmp/1.0_partition_ghcnd_all_xml/sensors"
    for $r in collection($sensor_collection)/dataCollection/data

    let $date := xs:date(fn:substring(xs:string(fn:data($r/date)), 0, 11))
    where $s/id eq $r/station
        and $r/dataType eq "TMIN"
        and fn:year-from-date($date) eq 2001
    return $r/value
) div 10


XQuery which normalises back from UI version of HTML to saved version.

declare default element namespace "http://www.w3.org/1999/xhtml";

declare function local:filter-descendants($item as node()) as node()* {
        let
            $attributes := for $attribute in $item/@* return local:filter-item($attribute),
            $children := for $child in $item/node() return local:filter-item($child)
        return
            ($attributes,$children)
};

declare function local:filter-item($item as node()) as node()? {
    typeswitch($item)
       case element() return
            let $name := local-name($item)
            return
            if( $name = 'script' ) then
                ()
            else
                element {$name} {
                    local:filter-descendants($item)
                }
       case attribute() return
           let $name := local-name($item)
           return
           if ( $name = 'contenteditable' ) then
               ()
           else
               attribute {QName('', $name)} {
                    string($item)
               }
       case text() return
           text{$item}
       case processing-instruction() return
           $item
       case comment() return
           $item
       default return
           local:filter-descendants($item) (: Just passes back children if any - handles case of root/doc node? :)
};

local:filter-item(.)


changing url format to start with doc name for FHIR history data

module namespace page = 'http://basex.org/modules/web-page';
import module namespace csd_bl = "https://github.com/openhie/openinfoman/csd_bl";
import module namespace functx = 'http://www.functx.com';
import module namespace csr_proc = "https://github.com/openhie/openinfoman/csr_proc";
import module namespace csd_webui =  "https://github.com/openhie/openinfoman/csd_webui";
import module namespace csd_dm = "https://github.com/openhie/openinfoman/csd_dm";
import module namespace svs_lsvs = "https://github.com/openhie/openinfoman/svs_lsvs";
declare namespace csd =  "urn:ihe:iti:csd:2013";
declare namespace svs = "urn:ihe:iti:svs:2008";
declare default element  namespace   "urn:ihe:iti:csd:2013";

declare function page:get_location ($doc_name,$_id,$page,$_count,$_since)
{
  <CSD xmlns:csd="urn:ihe:iti:csd:2013">
    {
      let $search_collection :=
      <collection>
        <search directory="facilityDirectory">urn:ihe:iti:csd:2014:stored-function:facility-search</search>
        <search directory="organizationDirectory">urn:ihe:iti:csd:2014:stored-function:organization-search</search>
      </collection>
      for $search in $search_collection/search
      let $search_name := $search/text()
      return
      (
        let $careServicesSubRequest :=
        <csd:careServicesRequest>
          <csd:function urn="{$search_name}" resource="{$doc_name}" base_url="{csd_webui:generateURL('')}">
            <csd:requestParams>
              {
                let $id := $_id
                return if (functx:all-whitespace($id)) then () else <csd:id entityID="{$id}"/>
              }

              {
                let $t_start := $page
                return if (functx:is-a-number($t_start))
                  then
                    let $start := max((xs:int($t_start),1))
                    let $t_count := $_count
                    let $count := if(functx:is-a-number($t_count)) then  max((xs:int($t_count),1)) else 50
                    let $startIndex := ($start - 1)*$count + 1
                    return <csd:start>{$startIndex}</csd:start>
                  else ()
              }
         {
           let $count := $_count
           return
              if(functx:is-a-number($count))
              then  <csd:max>{max((xs:int($count),1))} </csd:max>
              else ()
         }
         {
          let $since := $_since
          return if (functx:all-whitespace($since)) then () else <csd:record updated="{$since}"/>
         }
            </csd:requestParams>
          </csd:function>
        </csd:careServicesRequest>

        let $doc :=  csd_dm:open_document($doc_name)
        let $contents := csr_proc:process_CSR_stored_results( $doc , $careServicesSubRequest)
        let $directory := $search/@directory/string()
        return $contents/*[name() = $directory]
      )
    }
  </CSD>
};

declare function page:get_provider ($doc_name,$_id,$page,$_count,$_since,$organization,$location)
{
  let $search_name := "urn:ihe:iti:csd:2014:stored-function:provider-search"
  return
  <CSD xmlns:csd="urn:ihe:iti:csd:2013">
    {
      let $careServicesSubRequest :=
      <csd:careServicesRequest>
        <csd:function urn="{$search_name}" resource="{$doc_name}" base_url="{csd_webui:generateURL('')}">
          <csd:requestParams>
            {
              let $id := $_id
              return if (functx:all-whitespace($id)) then () else <csd:id entityID="{$id}"/>
            }

            {
              let $t_start := $page
              return if (functx:is-a-number($t_start))
                then
                  let $start := max((xs:int($t_start),1))
                  let $t_count := $_count
                  let $count := if(functx:is-a-number($t_count)) then  max((xs:int($t_count),1)) else 50
                  let $startIndex := ($start - 1)*$count + 1
                  return <csd:start>{$startIndex}</csd:start>
                else ()
            }
            {
              let $org := $organization
              return if (functx:all-whitespace($org)) then () else  <csd:organizations><csd:organization>{$org}</csd:organization></csd:organizations>
            }
            {
              let $loc := $location
              return if (functx:all-whitespace($loc)) then () else <csd:facilities><csd:facility>{$loc}</csd:facility></csd:facilities>
            }
       {
         let $count := $_count
         return
            if(functx:is-a-number($count))
            then  <csd:max>{max((xs:int($count),1))} </csd:max>
            else ()
       }
       {
        let $since := $_since
        return if (functx:all-whitespace($since)) then () else <csd:record updated="{$since}"/>
       }
          </csd:requestParams>
        </csd:function>
      </csd:careServicesRequest>

      let $doc :=  csd_dm:open_document($doc_name)
      let $contents := csr_proc:process_CSR_stored_results( $doc , $careServicesSubRequest)
      return $contents/providerDirectory
    }
  </CSD>
};

declare function page:get_services ($doc_name,$_id,$page,$_count,$_since)
{
  let $search_name := "urn:ihe:iti:csd:2014:stored-function:service-search"
  return
  <CSD xmlns:csd="urn:ihe:iti:csd:2013">
    {
      let $careServicesSubRequest :=
      <csd:careServicesRequest>
        <csd:function urn="{$search_name}" resource="{$doc_name}" base_url="{csd_webui:generateURL('')}">
          <csd:requestParams>
            {
              let $id := $_id
              return if (functx:all-whitespace($id)) then () else <csd:id entityID="{$id}"/>
            }

            {
              let $t_start := $page
              return if (functx:is-a-number($t_start))
                then
                  let $start := max((xs:int($t_start),1))
                  let $t_count := $_count
                  let $count := if(functx:is-a-number($t_count)) then  max((xs:int($t_count),1)) else 50
                  let $startIndex := ($start - 1)*$count + 1
                  return <csd:start>{$startIndex}</csd:start>
                else ()
            }
            {
              let $count := $_count
              return
                  if(functx:is-a-number($count))
                  then  <csd:max>{max((xs:int($count),1))} </csd:max>
                  else ()
            }
            {
              let $since := $_since
              return if (functx:all-whitespace($since)) then () else <csd:record updated="{$since}"/>
            }
          </csd:requestParams>
        </csd:function>
      </csd:careServicesRequest>

      let $doc :=  csd_dm:open_document($doc_name)
      let $contents := csr_proc:process_CSR_stored_results( $doc , $careServicesSubRequest)
      return $contents/serviceDirectory
    }
  </CSD>
};

declare
  %rest:path("/fhir")
  %rest:GET
  function page:FHIR()
{
  let $output :=
                <json type='object'>
                  <issue type="array">
                    <_ type="object">
                      exception

                        <text>Internal server error</text>

                      <severity>fatal</severity>
                    </_>
                  </issue>
                  <resourceType>OperationOutcome</resourceType>
                </json>
    return (
              <rest:response>
                <output:serialization-parameters>
                  <output:media-type value='application/json'/>
                </output:serialization-parameters>
              </rest:response>,
              json:serialize($output,map{"format":"direct",'escape': 'no'})
            )
};

declare
  %rest:path("/{$doc_name}/fhir/Location/{$id}")
  %rest:query-param("page", "{$page}")
  %rest:query-param("_count", "{$_count}")
  %rest:query-param("_since", "{$_since}")
  %rest:GET
  function page:FHIRLocationID($id,$page,$_count,$_since,$doc_name)
{
  page:FHIRLocation($id,$page,$_count,$_since,$doc_name)
};

declare
  %rest:path("/{$doc_name}/fhir/Location/_history")
  %rest:query-param("_id", "{$_id}")
  %rest:query-param("page", "{$page}")
  %rest:query-param("_count", "{$_count}")
  %rest:query-param("_since", "{$_since}")
  %rest:GET
  function page:FHIRLocation($_id,$page,$_count,$_since,$doc_name)
{
let $contents := page:get_location ($doc_name,$_id,$page,$_count,$_since)

let $location :=
<json type='object'>
<resourceType>Bundle</resourceType>
<total>{count($contents/facilityDirectory/facility) + count($contents/organizationDirectory/organization)}</total>
<type>history</type>
<entry type="array">
{
for $content in ($contents/facilityDirectory/facility,$contents/organizationDirectory/organization)
return
<_ type="object">
  <fullURL>{csd_webui:generateURL("fhir/" || $doc_name || "/Location/" || $content/@entityID)}</fullURL>
  {
    if($content/record/@created/string() != $content/record/@updated/string())
    then
    <request type="object">
      <method>PUT</method>
      <url>{$doc_name}/Location/{$content/@entityID/string()}</url>
    </request>
    else()
  }
  <resource type="object">
    <resourceType>Location</resourceType>
    <id>{$content/@entityID/string()}</id>
    {
     if(exists($content/otherID))
     then
     <identifier type="array">
      {
      for $otherid in $content/otherID
      return
      <_ type="object">
        <system>{$otherid/@assigningAuthorityName/string()}</system>
        <value>{$otherid/text()}</value>
      </_>
      }
      <_ type="object">
        <system>urn:ihe:iti:csd:2013:entityID</system>
        <value>{$content/@entityID/string()}</value>
      </_>
     </identifier>
     else ()
    }
    {
      let $search_name := "urn:ihe:iti:csd:2014:stored-function:organization-search"
      let $parent :=
      if(exists($content/organizations/organization))
      then
      $content/organizations/organization/@entityID/string()
      else
      if (exists($content/parent))
      then $content/parent/@entityID/string()
      else ()

      return if($parent)
      then
      let $careServicesSubRequest :=
      <csd:careServicesRequest>
        <csd:function urn="{$search_name}" resource="{$doc_name}" base_url="{csd_webui:generateURL()}">
          <csd:requestParams>
            <csd:id entityID="{$parent}"/>
          </csd:requestParams>
        </csd:function>
      </csd:careServicesRequest>
      let $doc :=  csd_dm:open_document($doc_name)
      let $organization := csr_proc:process_CSR_stored_results( $doc , $careServicesSubRequest)
      return if(exists($organization/organizationDirectory/organization))
      then
      <partOf type="object">
        <reference>{csd_webui:generateURL("fhir/" || $doc_name || "/Location/" || $organization/organizationDirectory/organization/@entityID/string())}</reference>
        <display>{$organization/organizationDirectory/organization/primaryName/text()}</display>
      </partOf>
      else ()
      else ()
    }
    {
      let $entityType := $content/name()
      return  <physicalType type="object">
                <coding type="array">
                  <_ type="object">
                    <system>http://hl7.org/fhir/location-physical-type</system>
                    {
                      if($entityType = "csd:facility" or $entityType = "facility") then
                      ( bu,
                        <display>Building</display>
                      )
                      else if($entityType = "csd:organization" or $entityType = "organization") then
                      (
                        jdn,
                        <display>Jurisdiction</display>
                      )
                      else
                      (
                      )
                    }
                  </_>
                </coding>
              </physicalType>
    }
    {
    if(exists($content/contactPoint))
    then
    <telecom type="array">
     {
     for $contactPoint in $content/contactPoint
     return ()
     }
    </telecom>
    else ()
    }
    {
      if(exists($content/geocode))
      then
      <position type="object">
        <longitude>{$content/geocode/longitude/text()}</longitude>
        <latitude>{$content/geocode/latitude/text()}</latitude>
      </position>
      else ()
    }
    {
      if(exists($content/codedType))
      then
        <type type="object">
          <coding type="array">
            {
              for $codedType in $content/codedType
                return
                <_ type="object">
                  {$codedType/@code/string()}
                  <system>{$codedType/@codingScheme/string()}</system>
                  {
                  if(functx:all-whitespace($codedType/text()))
                  then ()
                  else
                  <display>{$codedType/text()}</display>
                  }
                </_>
            }
          </coding>
        </type>
      else ()
    }
    <meta type="object">
      <lastUpdated>{$content/record/@updated/string()}</lastUpdated>
      <tag type="array">
        <_ type="object">
          {$content/record/@sourceDirectory/string()}
          <system>https://github.com/openhie/openinfoman/tags/fhir/sourceDirectory</system>
        </_>
      </tag>
    </meta>
    <status>{$content/record/@status/string()}</status>
    <name>{$content/primaryName/text()}</name>
    <mode>instance</mode>
  </resource>
</_>
}
</entry>
</json>

return (
  <rest:response>
    <output:serialization-parameters>
      <output:media-type value='application/json'/>
    </output:serialization-parameters>
  </rest:response>,
  json:serialize($location,map{"format":"direct",'escape': 'no'})
  )

};

declare
  %rest:path("/{$doc_name}/fhir/Practitioner/{$id}")
  %rest:query-param("page", "{$page}")
  %rest:query-param("_count", "{$_count}")
  %rest:query-param("_since", "{$_since}")
  %rest:query-param("organization", "{$organization}")
  %rest:query-param("location", "{$location}")
  %rest:GET
  function page:FHIRPractitionerID($id,$page,$_count,$_since,$doc_name,$organization,$location)
{
  page:FHIRPractitioner($id,$page,$_count,$_since,$doc_name,$organization,$location)
};

declare
  %rest:path("/{$doc_name}/fhir/Practitioner/_history")
  %rest:query-param("_id", "{$_id}")
  %rest:query-param("page", "{$page}")
  %rest:query-param("_count", "{$_count}")
  %rest:query-param("_since", "{$_since}")
  %rest:query-param("organization", "{$organization}")
  %rest:query-param("location", "{$location}")
  %rest:GET
  function page:FHIRPractitioner($_id,$page,$_count,$_since,$doc_name,$organization,$location)
{
let $contents := page:get_provider ($doc_name,$_id,$page,$_count,$_since,$organization,$location)
let $practitioner :=
<json type='object'>
<resourceType>Bundle</resourceType>
<total>{count($contents/providerDirectory/provider)}</total>
<type>history</type>
<entry type="array">
{
  for $content in $contents/providerDirectory/provider
  return
  <_ type="object">
    <fullURL>{csd_webui:generateURL("fhir/" || $doc_name || "/Practitioner/" || $content/@entityID)}</fullURL>
    {
      if($content/record/@created/string() != $content/record/@updated/string())
      then
      <request type="object">
        <method>PUT</method>
        <url>{$doc_name}/Practitioner/{$content/@entityID/string()}</url>
      </request>
      else()
    }
    <resource type="object">
      <resourceType>Practitioner</resourceType>
      <id>{$content/@entityID/string()}</id>
      {
        if(exists($content/otherID))
        then
        <identifier type="array">
        {
          for $otherid in $content/otherID
          return
          <_ type="object">
            <system>{$otherid/@assigningAuthorityName/string()}</system>
            <value>{$otherid/text()}</value>
          </_>
        }
          <_ type="object">
            <system>urn:ihe:iti:csd:2013:entityID</system>
            <value>{$content/@entityID/string()}</value>
          </_>
        </identifier>
        else ()
      }
      <active>
        {
          let $status := lower-case($content/record/@status/string())
          return if($status = "active") then "true"
                  else if($status = "inactive" or $status = "in-active") then "false"
                  else()
        }
      </active>
      {
        if(exists($content/demographic/contactPoint/codedType))
        then
        <telecom type="array">
          {
            for $contactPoint in $content/demographic/contactPoint
            return if (exists($contactPoint/codedType))
                    then
                    <_ type="object">
                      <system>
                      {
                        let $code := $contactPoint/codedType/@code/string()
                        return if($code = "BP") then "phone"
                               else if ($code = "EMAIL") then "email"
                               else if ($code = "FAX") then "fax"
                               else "other"
                      }
                      </system>
                      <value>{$contactPoint/codedType/text()}</value>
                    </_>
                   else ()
          }
        </telecom>
        else ()
      }
      {
        if(exists($content/demographic/address/addressLine))
        then
        <address type="array">
          {
            for $address in $content/demographic/address
            return
              if(exists($address/addressLine))
              then
              <_ type="object">
                {
                  let $csdtype := lower-case($address/@type/string())
                  let $fhirtype := if($csdtype = "mailing" or $csdtype = "mailing address") then "postal"
                                    else if ($csdtype = "physical" or $csdtype = "physical address") then "physical"
                                    else ()
                  return if($fhirtype)
                          then <type>{$fhirtype}</type>
                          else ()
                }
                {
                  for $addressLine in $address/addressLine
                  return if($addressLine/@component = "streetAddress")
                          then <line>{$addressLine/text()}</line>
                          else if($addressLine/@component = "city")
                          then <city>{$addressLine/text()}</city>
                          else if($addressLine/@component = "stateProvince")
                          then <state>{$addressLine/text()}</state>
                          else if($addressLine/@component = "country")
                          then <country>{$addressLine/text()}</country>
                          else if($addressLine/@component = "postalCode")
                          then <postalCode>{$addressLine/text()}</postalCode>
                          else()
                }
              </_>
              else ()
          }
        </address>
        else ()
      }
      {
        if(exists($content/credential)) then
        <qualification type="array">
        {
          for $credential in $content/credential
          return
          <_ type="object">
            <identifier type="array">
            {
              for $number in $credential/number
              return  <_ type="object">
                        <value>{$credential/number/text()}</value>
                      </_>
            }
            </identifier>

              <coding type="array">
              {
                for $codedType in $credential/codedType
                return
                <_ type="object">
                  {
                    if($codedType/@codingScheme/string()) then
                    <system>{$codedType/@codingScheme/string()}</system>
                    else ()
                  }
                  {
                    if($codedType/@code/string()) then
                    {$codedType/@code/string()}
                    else ()
                  }
                  <display>{$codedType/text()}</display>
                </_>
              }
              </coding>

            {
              if($credential/issuingAuthority/text()) then
              <issuer type="object">
                <display>{$credential/issuingAuthority/text()}</display>
              </issuer>
              else ()
            }
            {
              if(exists($credential/credentialIssueDate) or exists($credential/credentialRenewalDate)) then
              <period type="object">
              {
                if($credential/credentialIssueDate/text()) then
                <start>{$credential/credentialIssueDate/text()}</start>
                else ()
              }
              {
                if($credential/credentialRenewalDate/text()) then
                <end>{$credential/credentialRenewalDate/text()}</end>
                else ()
              }
              </period>
              else ()
            }
          </_>
        }
        </qualification>
        else ()
      }
      {
        if(exists($content/language)) then
        <communication type="array">
        {
          for $language in $content/language
          return
          <_ type="object">
            <coding type="array">
              {
                <_ type="object">
                  {
                    if($language/@codingScheme/string()) then
                    <system>{$language/@codingScheme/string()}</system>
                    else ()
                  }
                  {
                    if($language/@code/string()) then
                    {$language/@code/string()}
                    else ()
                  }
                    <display>{$language/text()}</display>
                </_>
              }
            </coding>
            {
              if($language/text()) then
              <text>{($language/text())}</text>
              else ()
            }
          </_>
        }
        </communication>
        else ()
      }
      <name type="array">
      {
        for $name in $content/demographic/name
        return
        <_ type="object">
          <text>{($name/commonName/text())[1]}</text>
          <family>{($name/surname/text())[1]}</family>
          <given>{($name/forename/text())[1] || ' ' || ($name/otherName/text())[1]}</given>
          <prefix>{($name/honorific/text())[1]}</prefix>
          <suffix>{($name/suffix/text())[1]}</suffix>
        </_>
      }
      </name>
      {
        if(exists($content/demographic/gender))
        then
        <gender>
          {
            let $gender := $content/demographic/gender/text()
            return if($gender = "F") then "female"
                    else if($gender = "M") then "male"
                    else lower-case($gender)
          }
        </gender>
        else ()
      }
      {
        if(exists($content/demographic/dateOfBirth))
        then
        <birthDate>{$content/demographic/dateOfBirth/text()}</birthDate>
        else ()
      }
      <meta type="object">
        <lastUpdated>{$content/record/@updated/string()}</lastUpdated>
        <tag type="array">
          <_ type="object">
            {$content/record/@sourceDirectory/string()}
            <system>https://github.com/openhie/openinfoman/tags/fhir/sourceDirectory</system>
          </_>
        </tag>
      </meta>
    </resource>
  </_>
}
</entry>
</json>
return (
          <rest:response>
            <output:serialization-parameters>
              <output:media-type value='application/json'/>
            </output:serialization-parameters>
          </rest:response>,
          json:serialize($practitioner,map{"format":"direct",'escape': 'no'})
        )
};

declare
  %rest:path("/{$doc_name}/fhir/PractitionerRole/{$id}")
  %rest:query-param("page", "{$page}")
  %rest:query-param("_count", "{$_count}")
  %rest:query-param("_since", "{$_since}")
  %rest:query-param("organization", "{$organization}")
  %rest:query-param("location", "{$location}")
  %rest:GET
  function page:FHIRPractitionerRoleID($id,$page,$_count,$_since,$doc_name,$organization,$location)
{
  page:FHIRPractitionerRole($id,$page,$_count,$_since,$doc_name,$organization,$location)
};

declare
  %rest:path("/{$doc_name}/fhir/PractitionerRole/_history")
  %rest:query-param("_id", "{$_id}")
  %rest:query-param("page", "{$page}")
  %rest:query-param("_count", "{$_count}")
  %rest:query-param("_since", "{$_since}")
  %rest:query-param("organization", "{$organization}")
  %rest:query-param("location", "{$location}")
  %rest:GET
  function page:FHIRPractitionerRole($_id,$page,$_count,$_since,$doc_name,$organization,$location)
{
let $contents := page:get_provider ($doc_name,$_id,$page,$_count,$_since,$organization,$location)
let $practitionerRole :=
<json type='object'>
<resourceType>Bundle</resourceType>
<total>{count($contents/providerDirectory/provider)}</total>
<type>history</type>
<entry type="array">
{
  for $content in $contents/providerDirectory/provider
  return
  <_ type="object">
    <fullURL>{csd_webui:generateURL("fhir/" || $doc_name || "/PractitionerRole/" || $content/@entityID)}</fullURL>
    {
      if($content/record/@created/string() != $content/record/@updated/string())
      then
      <request type="object">
        <method>PUT</method>
        <url>{$doc_name}/PractitionerRole/{$content/@entityID/string()}</url>
      </request>
      else()
    }
    <resource type="object">
      <resourceType>PractitionerRole</resourceType>
      <id>{$content/@entityID/string()}</id>
      {
        if(exists($content/otherID))
        then
        <identifier type="array">
        {
          for $otherid in $content/otherID
          return
          <_ type="object">
            <system>{$otherid/@assigningAuthorityName/string()}</system>
            <value>{$otherid/text()}</value>
          </_>
        }
        <_ type="object">
          <system>urn:ihe:iti:csd:2013:entityID</system>
          <value>{$content/@entityID/string()}</value>
        </_>
        </identifier>
        else ()
      }
      <practitioner type="object">
        <reference>Practitioner/{$content/@entityID/string()}</reference>
        <display>{$content/demographic/name[1]/commonName[1]/string()}</display>
      </practitioner>
      <location type="array">
        {
        for $facility at $index in $content/facilities/facility
        return
        <_ type="object">
          <reference>{csd_webui:generateURL("fhir/" || $doc_name || "/Location/" || $facility/@entityID/string())}</reference>
          {
            let $search_name := "urn:ihe:iti:csd:2014:stored-function:facility-search"
            let $careServicesSubRequest :=
            <csd:careServicesRequest>
              <csd:function urn="{$search_name}" resource="{$doc_name}" base_url="{csd_webui:generateURL()}">
                <csd:requestParams>
                  <csd:id entityID="{$facility/@entityID/string()}"/>
                </csd:requestParams>
              </csd:function>
            </csd:careServicesRequest>
            let $doc :=  csd_dm:open_document($doc_name)
            let $fac := csr_proc:process_CSR_stored_results( $doc , $careServicesSubRequest)
            return <display>{$fac//primaryName/text()}</display>
          }
        </_>
        }
        {
          for $organization at $index in $content/organizations/organization
          return
          <_ type="object">
          <reference>{csd_webui:generateURL("fhir/" || $doc_name || "/Location/" || $organization/@entityID/string())}</reference>
          {
            let $search_name := "urn:ihe:iti:csd:2014:stored-function:organization-search"
            let $careServicesSubRequest :=
            <csd:careServicesRequest>
              <csd:function urn="{$search_name}" resource="{$doc_name}" base_url="{csd_webui:generateURL()}">
                <csd:requestParams>
                  <csd:id entityID="{$organization/@entityID/string()}"/>
                </csd:requestParams>
              </csd:function>
            </csd:careServicesRequest>
            let $doc :=  csd_dm:open_document($doc_name)
            let $org := csr_proc:process_CSR_stored_results( $doc , $careServicesSubRequest)
            return <display>{$org//primaryName/text()}</display>
          }
          </_>
        }
      </location>
      <healthcareService type="array">
        {
        for $facility at $index in $content/facilities/facility
        for $service in $facility/service
        return
        <_ type="object">
          <reference>{csd_webui:generateURL("fhir/" || $doc_name || "/HealthcareService/" || $service/@entityID/string())}</reference>
          {
            let $search_name := "urn:ihe:iti:csd:2014:stored-function:service-search"
            let $careServicesSubRequest :=
            <csd:careServicesRequest>
              <csd:function urn="{$search_name}" resource="{$doc_name}" base_url="{csd_webui:generateURL()}">
                <csd:requestParams>
                  <csd:id entityID="{$service/@entityID/string()}"/>
                </csd:requestParams>
              </csd:function>
            </csd:careServicesRequest>
            let $doc :=  csd_dm:open_document($doc_name)
            let $serv := csr_proc:process_CSR_stored_results( $doc , $careServicesSubRequest)
            let $codedType := $serv//codedType
            let $svsCode := svs_lsvs:get_single_version_code($codedType/@code/string(),$codedType/@codingScheme/string(),"")
            return <display>{$svsCode//svs:Concept/@displayName/string()}</display>
          }
        </_>
        }
      </healthcareService>
      <specialty type="array">
        {
        for $specialty at $index in $content/specialty
        return
        <_ type="object">

            <coding type="array">
            {
              for $codedType in $specialty/codedType
              return
              <_ type="object">
                {
                  if($codedType/@codingScheme/string()) then
                  <system>{$codedType/@codingScheme/string()}</system>
                  else ()
                }
                {
                  if($codedType/@code/string()) then
                  {$codedType/@code/string()}
                  else ()
                }
                {
                  let $svsCode := svs_lsvs:get_single_version_code($codedType/@code/string(),$codedType/@codingScheme/string(),"")
                  return <display>{$svsCode//svs:Concept/@displayName/string()}</display>
                }
              </_>
            }
            </coding>
            {
              if($specialty/codedType/text()) then
              <text>{($specialty/codedType/text())[1]}</text>
              else ()
            }

        </_>
        }
      </specialty>
      <availableTime type="array">
        {
          for $service in $content/facilities/facility/service
          return <_ type="object">
            {
              for $operatingHours in $service/operatingHours
              let $CSDdayOfTheWeek := $operatingHours/dayOfTheWeek/text()
              let $FHIRdayOfTheWeek := page:getDayOfTheWeek($CSDdayOfTheWeek)
              return (
                      <daysOfWeek>{$FHIRdayOfTheWeek}</daysOfWeek>,
                      <availableStartTime>{$operatingHours/beginningHour/text()}</availableStartTime>,
                      <availableEndTime>{$operatingHours/endingHour/text()}</availableEndTime>
                      )
            }
            </_>
        }
        {
          for $operatingHours in $content/facilities/facility/operatingHours
          return
          <_ type="object">
          {
            let $CSDdayOfTheWeek := $operatingHours/dayOfTheWeek/text()
              let $FHIRdayOfTheWeek := page:getDayOfTheWeek($CSDdayOfTheWeek)
              return (
                      <daysOfWeek>{$FHIRdayOfTheWeek}</daysOfWeek>,
                      <availableStartTime>{$operatingHours/beginningHour/text()}</availableStartTime>,
                      <availableEndTime>{$operatingHours/endingHour/text()}</availableEndTime>
                      )
          }
          </_>
        }
      </availableTime>
      {
        if(exists($content/organizations/organization/contactPoint/codedType))
        then
        <telecom type="array">
          {
            for $contactPoint in $content/organizations/organization/contactPoint
            return if (exists($contactPoint/codedType))
                    then
                    <_ type="object">
                      <system>
                      {
                        let $code := $contactPoint/codedType/@code/string()
                        return if($code = "BP") then "phone"
                               else if ($code = "EMAIL") then "email"
                               else if ($code = "FAX") then "fax"
                               else "other"
                      }
                      </system>
                      <value>{$contactPoint/codedType/text()}</value>
                    </_>
                   else ()
          }
        </telecom>
        else ()
      }
      <active>
        {
          let $status := lower-case($content/record/@status/string())
          return if($status = "active") then "true"
                  else if($status = "inactive" or $status = "in-active") then "false"
                  else()
        }
      </active>
      <meta type="object">
        <lastUpdated>{$content/record/@updated/string()}</lastUpdated>
        <tag type="array">
          <_ type="object">
            {$content/record/@sourceDirectory/string()}
            <system>https://github.com/openhie/openinfoman/tags/fhir/sourceDirectory</system>
          </_>
        </tag>
      </meta>
    </resource>
  </_>
}
</entry>
</json>
return (
          <rest:response>
            <output:serialization-parameters>
              <output:media-type value='application/json'/>
            </output:serialization-parameters>
          </rest:response>,
          json:serialize($practitionerRole,map{"format":"direct",'escape': 'no'})
        )
};

declare
  %rest:path("/{$doc_name}/fhir/HealthcareService/{$id}")
  %rest:query-param("page", "{$page}")
  %rest:query-param("_count", "{$_count}")
  %rest:query-param("_since", "{$_since}")
  %rest:GET
  function page:FHIRHealthcareServiceID($id,$page,$_count,$_since,$doc_name)
{
  page:FHIRHealthcareService($id,$page,$_count,$_since,$doc_name)
};

declare
  %rest:path("/{$doc_name}/fhir/HealthcareService/_history")
  %rest:query-param("_id", "{$_id}")
  %rest:query-param("page", "{$page}")
  %rest:query-param("_count", "{$_count}")
  %rest:query-param("_since", "{$_since}")
  %rest:GET
  function page:FHIRHealthcareService($_id,$page,$_count,$_since,$doc_name)
{
let $contents := page:get_services ($doc_name,$_id,$page,$_count,$_since)
let $HealthcareService :=
<json type='object'>
  <resourceType>Bundle</resourceType>
  <total>{count($contents/serviceDirectory/service)}</total>
  <type>history</type>
  <entry type="array">
  {
    for $content in $contents/serviceDirectory/service
    return
    <_ type="object">
      <fullURL>{csd_webui:generateURL("fhir/" || $doc_name || "/HealthcareService/" || $content/@entityID)}</fullURL>
      {
        if($content/record/@created/string() != $content/record/@updated/string())
        then
        <request type="object">
          <method>PUT</method>
          <url>{$doc_name}/HealthcareService/{$content/@entityID/string()}</url>
        </request>
        else()
      }
      <resource type="object">
        <resourceType>HealthcareService</resourceType>
        <id>{$content/@entityID/string()}</id>
        {
          let $codedType := $content/codedType
          let $svsCode := svs_lsvs:get_single_version_code($codedType/@code/string(),$codedType/@codingScheme/string(),"")
          return <name>{$svsCode//svs:Concept/@displayName/string()}</name>
        }
        {
          <identifier type="array">
          {
            for $otherid in $content/otherID
            return
            <_ type="object">
              <system>{$otherid/@assigningAuthorityName/string()}</system>
              <value>{$otherid/text()}</value>
            </_>
          }
            <_ type="object">
              <system>urn:ihe:iti:csd:2013:entityID</system>
              <value>{$content/@entityID/string()}</value>
            </_>
          </identifier>
        }
        <active>
          {
            let $status := lower-case($content/record/@status/string())
            return if($status = "active") then "true"
                    else if($status = "inactive" or $status = "in-active") then "false"
                    else()
          }
        </active>
      </resource>
    </_>
  }
  </entry>
</json>

return (
          <rest:response>
            <output:serialization-parameters>
              <output:media-type value='application/json'/>
            </output:serialization-parameters>
          </rest:response>,
          json:serialize($HealthcareService,map{"format":"direct",'escape': 'no'})
        )
};

declare function page:getDayOfTheWeek ($day) {
  if($day = 1) then "mon"
                else if ($day = 2) then "tue"
                else if ($day = 3) then "wed"
                else if ($day = 4) then "thu"
                else if ($day = 5) then "fri"
                else if ($day = 6) then "sat"
                else if ($day = 7) then "sun"
                else ()
};

module namespace page = 'http://basex.org/modules/web-page';
import module namespace csd_bl = "https://github.com/openhie/openinfoman/csd_bl";
import module namespace functx = 'http://www.functx.com';
import module namespace csr_proc = "https://github.com/openhie/openinfoman/csr_proc";
import module namespace csd_webui =  "https://github.com/openhie/openinfoman/csd_webui";
import module namespace csd_dm = "https://github.com/openhie/openinfoman/csd_dm";
import module namespace svs_lsvs = "https://github.com/openhie/openinfoman/svs_lsvs";
declare namespace csd =  "urn:ihe:iti:csd:2013";
declare namespace svs = "urn:ihe:iti:svs:2008";
declare default element  namespace   "urn:ihe:iti:csd:2013";

declare function page:get_location ($doc_name,$_id,$page,$_count,$_since)
{
  <CSD xmlns:csd="urn:ihe:iti:csd:2013">
    {
      let $search_collection :=
      <collection>
        <search directory="facilityDirectory">urn:ihe:iti:csd:2014:stored-function:facility-search</search>
        <search directory="organizationDirectory">urn:ihe:iti:csd:2014:stored-function:organization-search</search>
      </collection>
      for $search in $search_collection/search
      let $search_name := $search/text()
      return
      (
        let $careServicesSubRequest :=
        <csd:careServicesRequest>
          <csd:function urn="{$search_name}" resource="{$doc_name}" base_url="{csd_webui:generateURL('')}">
            <csd:requestParams>
              {
                let $id := $_id
                return if (functx:all-whitespace($id)) then () else <csd:id entityID="{$id}"/>
              }

              {
                let $t_start := $page
                return if (functx:is-a-number($t_start))
                  then
                    let $start := max((xs:int($t_start),1))
                    let $t_count := $_count
                    let $count := if(functx:is-a-number($t_count)) then  max((xs:int($t_count),1)) else 50
                    let $startIndex := ($start - 1)*$count + 1
                    return <csd:start>{$startIndex}</csd:start>
                  else ()
              }
         {
           let $count := $_count
           return
              if(functx:is-a-number($count))
              then  <csd:max>{max((xs:int($count),1))} </csd:max>
              else ()
         }
         {
          let $since := $_since
          return if (functx:all-whitespace($since)) then () else <csd:record updated="{$since}"/>
         }
            </csd:requestParams>
          </csd:function>
        </csd:careServicesRequest>

        let $doc :=  csd_dm:open_document($doc_name)
        let $contents := csr_proc:process_CSR_stored_results( $doc , $careServicesSubRequest)
        let $directory := $search/@directory/string()
        return $contents/*[name() = $directory]
      )
    }
  </CSD>
};

declare function page:get_provider ($doc_name,$_id,$page,$_count,$_since,$organization,$location)
{
  let $search_name := "urn:ihe:iti:csd:2014:stored-function:provider-search"
  return
  <CSD xmlns:csd="urn:ihe:iti:csd:2013">
    {
      let $careServicesSubRequest :=
      <csd:careServicesRequest>
        <csd:function urn="{$search_name}" resource="{$doc_name}" base_url="{csd_webui:generateURL('')}">
          <csd:requestParams>
            {
              let $id := $_id
              return if (functx:all-whitespace($id)) then () else <csd:id entityID="{$id}"/>
            }

            {
              let $t_start := $page
              return if (functx:is-a-number($t_start))
                then
                  let $start := max((xs:int($t_start),1))
                  let $t_count := $_count
                  let $count := if(functx:is-a-number($t_count)) then  max((xs:int($t_count),1)) else 50
                  let $startIndex := ($start - 1)*$count + 1
                  return <csd:start>{$startIndex}</csd:start>
                else ()
            }
            {
              let $org := $organization
              return if (functx:all-whitespace($org)) then () else  <csd:organizations><csd:organization>{$org}</csd:organization></csd:organizations>
            }
            {
              let $loc := $location
              return if (functx:all-whitespace($loc)) then () else <csd:facilities><csd:facility>{$loc}</csd:facility></csd:facilities>
            }
       {
         let $count := $_count
         return
            if(functx:is-a-number($count))
            then  <csd:max>{max((xs:int($count),1))} </csd:max>
            else ()
       }
       {
        let $since := $_since
        return if (functx:all-whitespace($since)) then () else <csd:record updated="{$since}"/>
       }
          </csd:requestParams>
        </csd:function>
      </csd:careServicesRequest>

      let $doc :=  csd_dm:open_document($doc_name)
      let $contents := csr_proc:process_CSR_stored_results( $doc , $careServicesSubRequest)
      return $contents/providerDirectory
    }
  </CSD>
};

declare function page:get_services ($doc_name,$_id,$page,$_count,$_since)
{
  let $search_name := "urn:ihe:iti:csd:2014:stored-function:service-search"
  return
  <CSD xmlns:csd="urn:ihe:iti:csd:2013">
    {
      let $careServicesSubRequest :=
      <csd:careServicesRequest>
        <csd:function urn="{$search_name}" resource="{$doc_name}" base_url="{csd_webui:generateURL('')}">
          <csd:requestParams>
            {
              let $id := $_id
              return if (functx:all-whitespace($id)) then () else <csd:id entityID="{$id}"/>
            }

            {
              let $t_start := $page
              return if (functx:is-a-number($t_start))
                then
                  let $start := max((xs:int($t_start),1))
                  let $t_count := $_count
                  let $count := if(functx:is-a-number($t_count)) then  max((xs:int($t_count),1)) else 50
                  let $startIndex := ($start - 1)*$count + 1
                  return <csd:start>{$startIndex}</csd:start>
                else ()
            }
            {
              let $count := $_count
              return
                  if(functx:is-a-number($count))
                  then  <csd:max>{max((xs:int($count),1))} </csd:max>
                  else ()
            }
            {
              let $since := $_since
              return if (functx:all-whitespace($since)) then () else <csd:record updated="{$since}"/>
            }
          </csd:requestParams>
        </csd:function>
      </csd:careServicesRequest>

      let $doc :=  csd_dm:open_document($doc_name)
      let $contents := csr_proc:process_CSR_stored_results( $doc , $careServicesSubRequest)
      return $contents/serviceDirectory
    }
  </CSD>
};

declare
  %rest:path("/fhir")
  %rest:GET
  function page:FHIR()
{
  let $output :=
                <json type='object'>
                  <issue type="array">
                    <_ type="object">
                      exception

                        <text>Internal server error</text>

                      <severity>fatal</severity>
                    </_>
                  </issue>
                  <resourceType>OperationOutcome</resourceType>
                </json>
    return (
              <rest:response>
                <output:serialization-parameters>
                  <output:media-type value='application/json'/>
                </output:serialization-parameters>
              </rest:response>,
              json:serialize($output,map{"format":"direct",'escape': 'no'})
            )
};

declare
  %rest:path("/fhir/{$doc_name}/Location/{$id}")
  %rest:query-param("page", "{$page}")
  %rest:query-param("_count", "{$_count}")
  %rest:query-param("_since", "{$_since}")
  %rest:GET
  function page:FHIRLocationID($id,$page,$_count,$_since,$doc_name)
{
  page:FHIRLocation($id,$page,$_count,$_since,$doc_name)
};

declare
  %rest:path("/fhir/{$doc_name}/Location/_history")
  %rest:query-param("_id", "{$_id}")
  %rest:query-param("page", "{$page}")
  %rest:query-param("_count", "{$_count}")
  %rest:query-param("_since", "{$_since}")
  %rest:GET
  function page:FHIRLocation($_id,$page,$_count,$_since,$doc_name)
{
let $contents := page:get_location ($doc_name,$_id,$page,$_count,$_since)

let $location :=
<json type='object'>
<resourceType>Bundle</resourceType>
<total>{count($contents/facilityDirectory/facility) + count($contents/organizationDirectory/organization)}</total>
<type>history</type>
<entry type="array">
{
for $content in ($contents/facilityDirectory/facility,$contents/organizationDirectory/organization)
return
<_ type="object">
  <fullURL>{csd_webui:generateURL("fhir/" || $doc_name || "/Location/" || $content/@entityID)}</fullURL>
  {
    if($content/record/@created/string() != $content/record/@updated/string())
    then
    <request type="object">
      <method>PUT</method>
      <url>{$doc_name}/Location/{$content/@entityID/string()}</url>
    </request>
    else()
  }
  <resource type="object">
    <resourceType>Location</resourceType>
    <id>{$content/@entityID/string()}</id>
    {
     if(exists($content/otherID))
     then
     <identifier type="array">
      {
      for $otherid in $content/otherID
      return
      <_ type="object">
        <system>{$otherid/@assigningAuthorityName/string()}</system>
        <value>{$otherid/text()}</value>
      </_>
      }
      <_ type="object">
        <system>urn:ihe:iti:csd:2013:entityID</system>
        <value>{$content/@entityID/string()}</value>
      </_>
     </identifier>
     else ()
    }
    {
      let $search_name := "urn:ihe:iti:csd:2014:stored-function:organization-search"
      let $parent :=
      if(exists($content/organizations/organization))
      then
      $content/organizations/organization/@entityID/string()
      else
      if (exists($content/parent))
      then $content/parent/@entityID/string()
      else ()

      return if($parent)
      then
      let $careServicesSubRequest :=
      <csd:careServicesRequest>
        <csd:function urn="{$search_name}" resource="{$doc_name}" base_url="{csd_webui:generateURL()}">
          <csd:requestParams>
            <csd:id entityID="{$parent}"/>
          </csd:requestParams>
        </csd:function>
      </csd:careServicesRequest>
      let $doc :=  csd_dm:open_document($doc_name)
      let $organization := csr_proc:process_CSR_stored_results( $doc , $careServicesSubRequest)
      return if(exists($organization/organizationDirectory/organization))
      then
      <partOf type="object">
        <reference>{csd_webui:generateURL("fhir/" || $doc_name || "/Location/" || $organization/organizationDirectory/organization/@entityID/string())}</reference>
        <display>{$organization/organizationDirectory/organization/primaryName/text()}</display>
      </partOf>
      else ()
      else ()
    }
    {
      let $entityType := $content/name()
      return  <physicalType type="object">
                <coding type="array">
                  <_ type="object">
                    <system>http://hl7.org/fhir/location-physical-type</system>
                    {
                      if($entityType = "csd:facility" or $entityType = "facility") then
                      ( bu,
                        <display>Building</display>
                      )
                      else if($entityType = "csd:organization" or $entityType = "organization") then
                      (
                        jdn,
                        <display>Jurisdiction</display>
                      )
                      else
                      (
                      )
                    }
                  </_>
                </coding>
              </physicalType>
    }
    {
    if(exists($content/contactPoint))
    then
    <telecom type="array">
     {
     for $contactPoint in $content/contactPoint
     return ()
     }
    </telecom>
    else ()
    }
    {
      if(exists($content/geocode))
      then
      <position type="object">
        <longitude>{$content/geocode/longitude/text()}</longitude>
        <latitude>{$content/geocode/latitude/text()}</latitude>
      </position>
      else ()
    }
    {
      if(exists($content/codedType))
      then
        <type type="object">
          <coding type="array">
            {
              for $codedType in $content/codedType
                return
                <_ type="object">
                  {$codedType/@code/string()}
                  <system>{$codedType/@codingScheme/string()}</system>
                  {
                  if(functx:all-whitespace($codedType/text()))
                  then ()
                  else
                  <display>{$codedType/text()}</display>
                  }
                </_>
            }
          </coding>
        </type>
      else ()
    }
    <meta type="object">
      <lastUpdated>{$content/record/@updated/string()}</lastUpdated>
      <tag type="array">
        <_ type="object">
          {$content/record/@sourceDirectory/string()}
          <system>https://github.com/openhie/openinfoman/tags/fhir/sourceDirectory</system>
        </_>
      </tag>
    </meta>
    <status>{$content/record/@status/string()}</status>
    <name>{$content/primaryName/text()}</name>
    <mode>instance</mode>
  </resource>
</_>
}
</entry>
</json>

return (
  <rest:response>
    <output:serialization-parameters>
      <output:media-type value='application/json'/>
    </output:serialization-parameters>
  </rest:response>,
  json:serialize($location,map{"format":"direct",'escape': 'no'})
  )

};

declare
  %rest:path("/fhir/{$doc_name}/Practitioner/{$id}")
  %rest:query-param("page", "{$page}")
  %rest:query-param("_count", "{$_count}")
  %rest:query-param("_since", "{$_since}")
  %rest:query-param("organization", "{$organization}")
  %rest:query-param("location", "{$location}")
  %rest:GET
  function page:FHIRPractitionerID($id,$page,$_count,$_since,$doc_name,$organization,$location)
{
  page:FHIRPractitioner($id,$page,$_count,$_since,$doc_name,$organization,$location)
};

declare
  %rest:path("/fhir/{$doc_name}/Practitioner/_history")
  %rest:query-param("_id", "{$_id}")
  %rest:query-param("page", "{$page}")
  %rest:query-param("_count", "{$_count}")
  %rest:query-param("_since", "{$_since}")
  %rest:query-param("organization", "{$organization}")
  %rest:query-param("location", "{$location}")
  %rest:GET
  function page:FHIRPractitioner($_id,$page,$_count,$_since,$doc_name,$organization,$location)
{
let $contents := page:get_provider ($doc_name,$_id,$page,$_count,$_since,$organization,$location)
let $practitioner :=
<json type='object'>
<resourceType>Bundle</resourceType>
<total>{count($contents/providerDirectory/provider)}</total>
<type>history</type>
<entry type="array">
{
  for $content in $contents/providerDirectory/provider
  return
  <_ type="object">
    <fullURL>{csd_webui:generateURL("fhir/" || $doc_name || "/Practitioner/" || $content/@entityID)}</fullURL>
    {
      if($content/record/@created/string() != $content/record/@updated/string())
      then
      <request type="object">
        <method>PUT</method>
        <url>{$doc_name}/Practitioner/{$content/@entityID/string()}</url>
      </request>
      else()
    }
    <resource type="object">
      <resourceType>Practitioner</resourceType>
      <id>{$content/@entityID/string()}</id>
      {
        if(exists($content/otherID))
        then
        <identifier type="array">
        {
          for $otherid in $content/otherID
          return
          <_ type="object">
            <system>{$otherid/@assigningAuthorityName/string()}</system>
            <value>{$otherid/text()}</value>
          </_>
        }
          <_ type="object">
            <system>urn:ihe:iti:csd:2013:entityID</system>
            <value>{$content/@entityID/string()}</value>
          </_>
        </identifier>
        else ()
      }
      <active>
        {
          let $status := lower-case($content/record/@status/string())
          return if($status = "active") then "true"
                  else if($status = "inactive" or $status = "in-active") then "false"
                  else()
        }
      </active>
      {
        if(exists($content/demographic/contactPoint/codedType))
        then
        <telecom type="array">
          {
            for $contactPoint in $content/demographic/contactPoint
            return if (exists($contactPoint/codedType))
                    then
                    <_ type="object">
                      <system>
                      {
                        let $code := $contactPoint/codedType/@code/string()
                        return if($code = "BP") then "phone"
                               else if ($code = "EMAIL") then "email"
                               else if ($code = "FAX") then "fax"
                               else "other"
                      }
                      </system>
                      <value>{$contactPoint/codedType/text()}</value>
                    </_>
                   else ()
          }
        </telecom>
        else ()
      }
      {
        if(exists($content/demographic/address/addressLine))
        then
        <address type="array">
          {
            for $address in $content/demographic/address
            return
              if(exists($address/addressLine))
              then
              <_ type="object">
                {
                  let $csdtype := lower-case($address/@type/string())
                  let $fhirtype := if($csdtype = "mailing" or $csdtype = "mailing address") then "postal"
                                    else if ($csdtype = "physical" or $csdtype = "physical address") then "physical"
                                    else ()
                  return if($fhirtype)
                          then <type>{$fhirtype}</type>
                          else ()
                }
                {
                  for $addressLine in $address/addressLine
                  return if($addressLine/@component = "streetAddress")
                          then <line>{$addressLine/text()}</line>
                          else if($addressLine/@component = "city")
                          then <city>{$addressLine/text()}</city>
                          else if($addressLine/@component = "stateProvince")
                          then <state>{$addressLine/text()}</state>
                          else if($addressLine/@component = "country")
                          then <country>{$addressLine/text()}</country>
                          else if($addressLine/@component = "postalCode")
                          then <postalCode>{$addressLine/text()}</postalCode>
                          else()
                }
              </_>
              else ()
          }
        </address>
        else ()
      }
      {
        if(exists($content/credential)) then
        <qualification type="array">
        {
          for $credential in $content/credential
          return
          <_ type="object">
            <identifier type="array">
            {
              for $number in $credential/number
              return  <_ type="object">
                        <value>{$credential/number/text()}</value>
                      </_>
            }
            </identifier>

              <coding type="array">
              {
                for $codedType in $credential/codedType
                return
                <_ type="object">
                  {
                    if($codedType/@codingScheme/string()) then
                    <system>{$codedType/@codingScheme/string()}</system>
                    else ()
                  }
                  {
                    if($codedType/@code/string()) then
                    {$codedType/@code/string()}
                    else ()
                  }
                  <display>{$codedType/text()}</display>
                </_>
              }
              </coding>

            {
              if($credential/issuingAuthority/text()) then
              <issuer type="object">
                <display>{$credential/issuingAuthority/text()}</display>
              </issuer>
              else ()
            }
            {
              if(exists($credential/credentialIssueDate) or exists($credential/credentialRenewalDate)) then
              <period type="object">
              {
                if($credential/credentialIssueDate/text()) then
                <start>{$credential/credentialIssueDate/text()}</start>
                else ()
              }
              {
                if($credential/credentialRenewalDate/text()) then
                <end>{$credential/credentialRenewalDate/text()}</end>
                else ()
              }
              </period>
              else ()
            }
          </_>
        }
        </qualification>
        else ()
      }
      {
        if(exists($content/language)) then
        <communication type="array">
        {
          for $language in $content/language
          return
          <_ type="object">
            <coding type="array">
              {
                <_ type="object">
                  {
                    if($language/@codingScheme/string()) then
                    <system>{$language/@codingScheme/string()}</system>
                    else ()
                  }
                  {
                    if($language/@code/string()) then
                    {$language/@code/string()}
                    else ()
                  }
                    <display>{$language/text()}</display>
                </_>
              }
            </coding>
            {
              if($language/text()) then
              <text>{($language/text())}</text>
              else ()
            }
          </_>
        }
        </communication>
        else ()
      }
      <name type="array">
      {
        for $name in $content/demographic/name
        return
        <_ type="object">
          <text>{($name/commonName/text())[1]}</text>
          <family>{($name/surname/text())[1]}</family>
          <given>{($name/forename/text())[1] || ' ' || ($name/otherName/text())[1]}</given>
          <prefix>{($name/honorific/text())[1]}</prefix>
          <suffix>{($name/suffix/text())[1]}</suffix>
        </_>
      }
      </name>
      {
        if(exists($content/demographic/gender))
        then
        <gender>
          {
            let $gender := $content/demographic/gender/text()
            return if($gender = "F") then "female"
                    else if($gender = "M") then "male"
                    else lower-case($gender)
          }
        </gender>
        else ()
      }
      {
        if(exists($content/demographic/dateOfBirth))
        then
        <birthDate>{$content/demographic/dateOfBirth/text()}</birthDate>
        else ()
      }
      <meta type="object">
        <lastUpdated>{$content/record/@updated/string()}</lastUpdated>
        <tag type="array">
          <_ type="object">
            {$content/record/@sourceDirectory/string()}
            <system>https://github.com/openhie/openinfoman/tags/fhir/sourceDirectory</system>
          </_>
        </tag>
      </meta>
    </resource>
  </_>
}
</entry>
</json>
return (
          <rest:response>
            <output:serialization-parameters>
              <output:media-type value='application/json'/>
            </output:serialization-parameters>
          </rest:response>,
          json:serialize($practitioner,map{"format":"direct",'escape': 'no'})
        )
};

declare
  %rest:path("/fhir/{$doc_name}/PractitionerRole/{$id}")
  %rest:query-param("page", "{$page}")
  %rest:query-param("_count", "{$_count}")
  %rest:query-param("_since", "{$_since}")
  %rest:query-param("organization", "{$organization}")
  %rest:query-param("location", "{$location}")
  %rest:GET
  function page:FHIRPractitionerRoleID($id,$page,$_count,$_since,$doc_name,$organization,$location)
{
  page:FHIRPractitionerRole($id,$page,$_count,$_since,$doc_name,$organization,$location)
};

declare
  %rest:path("/fhir/{$doc_name}/PractitionerRole/_history")
  %rest:query-param("_id", "{$_id}")
  %rest:query-param("page", "{$page}")
  %rest:query-param("_count", "{$_count}")
  %rest:query-param("_since", "{$_since}")
  %rest:query-param("organization", "{$organization}")
  %rest:query-param("location", "{$location}")
  %rest:GET
  function page:FHIRPractitionerRole($_id,$page,$_count,$_since,$doc_name,$organization,$location)
{
let $contents := page:get_provider ($doc_name,$_id,$page,$_count,$_since,$organization,$location)
let $practitionerRole :=
<json type='object'>
<resourceType>Bundle</resourceType>
<total>{count($contents/providerDirectory/provider)}</total>
<type>history</type>
<entry type="array">
{
  for $content in $contents/providerDirectory/provider
  return
  <_ type="object">
    <fullURL>{csd_webui:generateURL("fhir/" || $doc_name || "/PractitionerRole/" || $content/@entityID)}</fullURL>
    {
      if($content/record/@created/string() != $content/record/@updated/string())
      then
      <request type="object">
        <method>PUT</method>
        <url>{$doc_name}/PractitionerRole/{$content/@entityID/string()}</url>
      </request>
      else()
    }
    <resource type="object">
      <resourceType>PractitionerRole</resourceType>
      <id>{$content/@entityID/string()}</id>
      {
        if(exists($content/otherID))
        then
        <identifier type="array">
        {
          for $otherid in $content/otherID
          return
          <_ type="object">
            <system>{$otherid/@assigningAuthorityName/string()}</system>
            <value>{$otherid/text()}</value>
          </_>
        }
        <_ type="object">
          <system>urn:ihe:iti:csd:2013:entityID</system>
          <value>{$content/@entityID/string()}</value>
        </_>
        </identifier>
        else ()
      }
      <practitioner type="object">
        <reference>Practitioner/{$content/@entityID/string()}</reference>
        <display>{$content/demographic/name[1]/commonName[1]/string()}</display>
      </practitioner>
      <location type="array">
        {
        for $facility at $index in $content/facilities/facility
        return
        <_ type="object">
          <reference>{csd_webui:generateURL("fhir/" || $doc_name || "/Location/" || $facility/@entityID/string())}</reference>
          {
            let $search_name := "urn:ihe:iti:csd:2014:stored-function:facility-search"
            let $careServicesSubRequest :=
            <csd:careServicesRequest>
              <csd:function urn="{$search_name}" resource="{$doc_name}" base_url="{csd_webui:generateURL()}">
                <csd:requestParams>
                  <csd:id entityID="{$facility/@entityID/string()}"/>
                </csd:requestParams>
              </csd:function>
            </csd:careServicesRequest>
            let $doc :=  csd_dm:open_document($doc_name)
            let $fac := csr_proc:process_CSR_stored_results( $doc , $careServicesSubRequest)
            return <display>{$fac//primaryName/text()}</display>
          }
        </_>
        }
        {
          for $organization at $index in $content/organizations/organization
          return
          <_ type="object">
          <reference>{csd_webui:generateURL("fhir/" || $doc_name || "/Location/" || $organization/@entityID/string())}</reference>
          {
            let $search_name := "urn:ihe:iti:csd:2014:stored-function:organization-search"
            let $careServicesSubRequest :=
            <csd:careServicesRequest>
              <csd:function urn="{$search_name}" resource="{$doc_name}" base_url="{csd_webui:generateURL()}">
                <csd:requestParams>
                  <csd:id entityID="{$organization/@entityID/string()}"/>
                </csd:requestParams>
              </csd:function>
            </csd:careServicesRequest>
            let $doc :=  csd_dm:open_document($doc_name)
            let $org := csr_proc:process_CSR_stored_results( $doc , $careServicesSubRequest)
            return <display>{$org//primaryName/text()}</display>
          }
          </_>
        }
      </location>
      <healthcareService type="array">
        {
        for $facility at $index in $content/facilities/facility
        for $service in $facility/service
        return
        <_ type="object">
          <reference>{csd_webui:generateURL("fhir/" || $doc_name || "/HealthcareService/" || $service/@entityID/string())}</reference>
          {
            let $search_name := "urn:ihe:iti:csd:2014:stored-function:service-search"
            let $careServicesSubRequest :=
            <csd:careServicesRequest>
              <csd:function urn="{$search_name}" resource="{$doc_name}" base_url="{csd_webui:generateURL()}">
                <csd:requestParams>
                  <csd:id entityID="{$service/@entityID/string()}"/>
                </csd:requestParams>
              </csd:function>
            </csd:careServicesRequest>
            let $doc :=  csd_dm:open_document($doc_name)
            let $serv := csr_proc:process_CSR_stored_results( $doc , $careServicesSubRequest)
            let $codedType := $serv//codedType
            let $svsCode := svs_lsvs:get_single_version_code($codedType/@code/string(),$codedType/@codingScheme/string(),"")
            return <display>{$svsCode//svs:Concept/@displayName/string()}</display>
          }
        </_>
        }
      </healthcareService>
      <specialty type="array">
        {
        for $specialty at $index in $content/specialty
        return
        <_ type="object">

            <coding type="array">
            {
              for $codedType in $specialty/codedType
              return
              <_ type="object">
                {
                  if($codedType/@codingScheme/string()) then
                  <system>{$codedType/@codingScheme/string()}</system>
                  else ()
                }
                {
                  if($codedType/@code/string()) then
                  {$codedType/@code/string()}
                  else ()
                }
                {
                  let $svsCode := svs_lsvs:get_single_version_code($codedType/@code/string(),$codedType/@codingScheme/string(),"")
                  return <display>{$svsCode//svs:Concept/@displayName/string()}</display>
                }
              </_>
            }
            </coding>
            {
              if($specialty/codedType/text()) then
              <text>{($specialty/codedType/text())[1]}</text>
              else ()
            }

        </_>
        }
      </specialty>
      <availableTime type="array">
        {
          for $service in $content/facilities/facility/service
          return <_ type="object">
            {
              for $operatingHours in $service/operatingHours
              let $CSDdayOfTheWeek := $operatingHours/dayOfTheWeek/text()
              let $FHIRdayOfTheWeek := page:getDayOfTheWeek($CSDdayOfTheWeek)
              return (
                      <daysOfWeek>{$FHIRdayOfTheWeek}</daysOfWeek>,
                      <availableStartTime>{$operatingHours/beginningHour/text()}</availableStartTime>,
                      <availableEndTime>{$operatingHours/endingHour/text()}</availableEndTime>
                      )
            }
            </_>
        }
        {
          for $operatingHours in $content/facilities/facility/operatingHours
          return
          <_ type="object">
          {
            let $CSDdayOfTheWeek := $operatingHours/dayOfTheWeek/text()
              let $FHIRdayOfTheWeek := page:getDayOfTheWeek($CSDdayOfTheWeek)
              return (
                      <daysOfWeek>{$FHIRdayOfTheWeek}</daysOfWeek>,
                      <availableStartTime>{$operatingHours/beginningHour/text()}</availableStartTime>,
                      <availableEndTime>{$operatingHours/endingHour/text()}</availableEndTime>
                      )
          }
          </_>
        }
      </availableTime>
      {
        if(exists($content/organizations/organization/contactPoint/codedType))
        then
        <telecom type="array">
          {
            for $contactPoint in $content/organizations/organization/contactPoint
            return if (exists($contactPoint/codedType))
                    then
                    <_ type="object">
                      <system>
                      {
                        let $code := $contactPoint/codedType/@code/string()
                        return if($code = "BP") then "phone"
                               else if ($code = "EMAIL") then "email"
                               else if ($code = "FAX") then "fax"
                               else "other"
                      }
                      </system>
                      <value>{$contactPoint/codedType/text()}</value>
                    </_>
                   else ()
          }
        </telecom>
        else ()
      }
      <active>
        {
          let $status := lower-case($content/record/@status/string())
          return if($status = "active") then "true"
                  else if($status = "inactive" or $status = "in-active") then "false"
                  else()
        }
      </active>
      <meta type="object">
        <lastUpdated>{$content/record/@updated/string()}</lastUpdated>
        <tag type="array">
          <_ type="object">
            {$content/record/@sourceDirectory/string()}
            <system>https://github.com/openhie/openinfoman/tags/fhir/sourceDirectory</system>
          </_>
        </tag>
      </meta>
    </resource>
  </_>
}
</entry>
</json>
return (
          <rest:response>
            <output:serialization-parameters>
              <output:media-type value='application/json'/>
            </output:serialization-parameters>
          </rest:response>,
          json:serialize($practitionerRole,map{"format":"direct",'escape': 'no'})
        )
};

declare
  %rest:path("/fhir/{$doc_name}/HealthcareService/{$id}")
  %rest:query-param("page", "{$page}")
  %rest:query-param("_count", "{$_count}")
  %rest:query-param("_since", "{$_since}")
  %rest:GET
  function page:FHIRHealthcareServiceID($id,$page,$_count,$_since,$doc_name)
{
  page:FHIRHealthcareService($id,$page,$_count,$_since,$doc_name)
};

declare
  %rest:path("/fhir/{$doc_name}/HealthcareService/_history")
  %rest:query-param("_id", "{$_id}")
  %rest:query-param("page", "{$page}")
  %rest:query-param("_count", "{$_count}")
  %rest:query-param("_since", "{$_since}")
  %rest:GET
  function page:FHIRHealthcareService($_id,$page,$_count,$_since,$doc_name)
{
let $contents := page:get_services ($doc_name,$_id,$page,$_count,$_since)
let $HealthcareService :=
<json type='object'>
  <resourceType>Bundle</resourceType>
  <total>{count($contents/serviceDirectory/service)}</total>
  <type>history</type>
  <entry type="array">
  {
    for $content in $contents/serviceDirectory/service
    return
    <_ type="object">
      <fullURL>{csd_webui:generateURL("fhir/" || $doc_name || "/HealthcareService/" || $content/@entityID)}</fullURL>
      {
        if($content/record/@created/string() != $content/record/@updated/string())
        then
        <request type="object">
          <method>PUT</method>
          <url>{$doc_name}/HealthcareService/{$content/@entityID/string()}</url>
        </request>
        else()
      }
      <resource type="object">
        <resourceType>HealthcareService</resourceType>
        <id>{$content/@entityID/string()}</id>
        {
          let $codedType := $content/codedType
          let $svsCode := svs_lsvs:get_single_version_code($codedType/@code/string(),$codedType/@codingScheme/string(),"")
          return <name>{$svsCode//svs:Concept/@displayName/string()}</name>
        }
        {
          <identifier type="array">
          {
            for $otherid in $content/otherID
            return
            <_ type="object">
              <system>{$otherid/@assigningAuthorityName/string()}</system>
              <value>{$otherid/text()}</value>
            </_>
          }
            <_ type="object">
              <system>urn:ihe:iti:csd:2013:entityID</system>
              <value>{$content/@entityID/string()}</value>
            </_>
          </identifier>
        }
        <active>
          {
            let $status := lower-case($content/record/@status/string())
            return if($status = "active") then "true"
                    else if($status = "inactive" or $status = "in-active") then "false"
                    else()
          }
        </active>
      </resource>
    </_>
  }
  </entry>
</json>

return (
          <rest:response>
            <output:serialization-parameters>
              <output:media-type value='application/json'/>
            </output:serialization-parameters>
          </rest:response>,
          json:serialize($HealthcareService,map{"format":"direct",'escape': 'no'})
        )
};

declare function page:getDayOfTheWeek ($day) {
  if($day = 1) then "mon"
                else if ($day = 2) then "tue"
                else if ($day = 3) then "wed"
                else if ($day = 4) then "thu"
                else if ($day = 5) then "fri"
                else if ($day = 6) then "sat"
                else if ($day = 7) then "sun"
                else ()
};

declare
  %rest:path("/test")
  %rest:GET
  function page:test()
{
  let $val := svs_lsvs:get_single_version_code("101-001","1.3.6.1.4.1.21367.200.107","")
  return
  <display>{$val//svs:Concept/@displayName/string()}</display>

};


Remove the math documentation now that the W3C spec files are used for that.

xquery version "3.0" encoding "UTF-8";
(:~
 : XPath and XQuery Functions and Operators: Trigonometric and exponential functions
 :
 : @see https://www.w3.org/TR/2014/REC-xpath-functions-30-20140408/#trigonometry
 :
 : This documentation includes material copied from or derived from the XPath and
 : XQuery Functions and Operators 3.0 specification. Copyright © 2017 W3C®
 : (MIT, ERCIM, Keio, Beihang).
 :)
module namespace math = "http://www.w3.org/2005/xpath-functions/math";

declare namespace a = "http://reecedunn.co.uk/xquery/annotations";
declare namespace o = "http://reecedunn.co.uk/xquery/options";

declare option o:requires "xpath-functions/3.0";

declare %a:since("xpath-functions", "3.0-20140408") function math:acos($arg as xs:double?) as xs:double? external;
declare %a:since("xpath-functions", "3.0-20140408") function math:asin($arg as xs:double?) as xs:double? external;
declare %a:since("xpath-functions", "3.0-20140408") function math:atan($arg as xs:double?) as xs:double? external;
declare %a:since("xpath-functions", "3.0-20140408") function math:atan2($y as xs:double, $x as xs:double) as xs:double external;
declare %a:since("xpath-functions", "3.0-20140408") function math:cos($θ as xs:double?) as xs:double? external;
declare %a:since("xpath-functions", "3.0-20140408") function math:exp($arg as xs:double?) as xs:double? external;
declare %a:since("xpath-functions", "3.0-20140408") function math:exp10($arg as xs:double?) as xs:double? external;
declare %a:since("xpath-functions", "3.0-20140408") function math:log($arg as xs:double?) as xs:double? external;
declare %a:since("xpath-functions", "3.0-20140408") function math:log10($arg as xs:double?) as xs:double? external;
declare %a:since("xpath-functions", "3.0-20140408") function math:pi() as xs:double external;
declare %a:since("xpath-functions", "3.0-20140408") function math:pow($x as xs:double?, $y as xs:numeric) as xs:double? external;
declare %a:since("xpath-functions", "3.0-20140408") function math:sin($θ as xs:double?) as xs:double? external;
declare %a:since("xpath-functions", "3.0-20140408") function math:sqrt($arg as xs:double?) as xs:double? external;
declare %a:since("xpath-functions", "3.0-20140408") function math:tan($θ as xs:double?) as xs:double? external;

xquery version "3.0" encoding "UTF-8";
(:~
 : XPath and XQuery Functions and Operators: Trigonometric and exponential functions
 :
 : @see https://www.w3.org/TR/2014/REC-xpath-functions-30-20140408/#trigonometry
 :
 : This documentation includes material copied from or derived from the XPath and
 : XQuery Functions and Operators 3.0 specification. Copyright © 2017 W3C®
 : (MIT, ERCIM, Keio, Beihang).
 :)
module namespace math = "http://www.w3.org/2005/xpath-functions/math";

declare namespace a = "http://reecedunn.co.uk/xquery/annotations";
declare namespace o = "http://reecedunn.co.uk/xquery/options";

declare option o:requires "xpath-functions/3.0";

(:~
 : Returns the arc cosine of the argument.
 :
 : If $arg is the empty sequence, the function returns the empty
 : sequence.
 :
 : Otherwise the result is the arc cosine of $arg, as defined in
 : the IEEE 754-2008 specification of the acos function applied to 64-bit binary
 : floating point values. The result is in the range zero to +π radians.
 :
 :
Notes

 :
 : If $arg is NaN, or if its absolute value is greater
 : than one, then the result is NaN.
 :
 : In other cases the result is an xs:double value representing an
 : angle θ in radians in the range 0 <= θ <= +π.
 :)
declare %a:since("xpath-functions", "3.0-20140408") function math:acos($arg as xs:double?) as xs:double? external;
(:~
 : Returns the arc sine of the argument.
 :
 : If $arg is the empty sequence, the function returns the empty
 : sequence.
 :
 : Otherwise the result is the arc sine of $arg, as defined in
 : the IEEE 754-2008 specification of the asin function applied to 64-bit binary
 : floating point values. The result is in the range -π/2 to +π/2 radians.
 :
 :
Notes

 :
 : If $arg is NaN, or if its absolute value is greater
 : than one, then the result is NaN.
 :
 : In other cases the result is an xs:double value representing an
 : angle θ in radians in the range -π/2 <= θ <= +π/2.
 :)
declare %a:since("xpath-functions", "3.0-20140408") function math:asin($arg as xs:double?) as xs:double? external;
(:~
 : Returns the arc tangent of the argument.
 :
 : If $arg is the empty sequence, the function returns the empty
 : sequence.
 :
 : Otherwise the result is the arc tangent of $arg, as defined in
 : the IEEE 754-2008 specification of the atan function applied to 64-bit binary
 : floating point values. The result is in the range -π/2 to +π/2 radians.
 :
 :
Notes

 :
 : If $arg is positive or negative infinity, the result is
 : positive or negative π/2.
 :
 : If $arg is positive or negative zero, the result is
 : $arg.
 :
 : If $arg is NaN, then the result is NaN.
 :
 : In other cases the result is an xs:double value representing an
 : angle θ in radians in the range -π/2 <= θ <= +π/2.
 :)
declare %a:since("xpath-functions", "3.0-20140408") function math:atan($arg as xs:double?) as xs:double? external;
(:~
 : Returns the angle in radians subtended at the origin by the point on a plane
 : with coordinates (x, y) and the positive x-axis.
 :
 : The result is the value of atan2(y, x) as defined in the IEEE
 : 754-2008 specification of the atan2 function applied to 64-bit
 : binary floating point values. The result is in the range -π to +π radians.
 :
 :
Notes

 :
 : If either argument is NaN then the result is NaN.
 :
 : If $y is positive or negative, and $x is positive
 : and finite, then (subject to rules for overflow, underflow and approximation)
 : the value of math:atan2($y, $x) is math:atan($y div $x).
 :
 : If $y is positive and $x is negative and finite,
 : then (subject to the same caveats) the value of math:atan2($y, $x)
 : is math:atan($y div $x) + math:pi().
 :
 : If $y is negative and $x is negative and finite,
 : then (subject to the same caveats) the value of math:atan2($y, $x)
 : is math:atan($y div $x) - math:pi().
 :)
declare %a:since("xpath-functions", "3.0-20140408") function math:atan2($y as xs:double, $x as xs:double) as xs:double external;
(:~
 : Returns the cosine of the argument. The argument is an angle in radians.
 :
 : If $θ is the empty sequence, the function returns the empty
 : sequence.
 :
 : Otherwise the result is the cosine of $θ (which is treated as
 : an angle in radians) as defined in the IEEE 754-2008 specification of the
 : cos function applied to 64-bit binary floating point values.
 :
 :
Notes

 :
 : If $θ is positive or negative zero, the result is $θ.
 :
 : If $θ is positive or negative infinity, or NaN,
 : then the result is NaN.
 :
 : Otherwise the result is always in the range -1.0e0 to +1.0e0.
 :)
declare %a:since("xpath-functions", "3.0-20140408") function math:cos($θ as xs:double?) as xs:double? external;
(:~
 : Returns the value of ex.
 :
 : If $arg is the empty sequence, the function returns the empty
 : sequence.
 :
 : Otherwise the result is the mathematical constant e raised to
 : the power of $arg, as defined in the IEEE 754-2008 specification
 : of the exp function applied to 64-bit binary floating point values.
 :)
declare %a:since("xpath-functions", "3.0-20140408") function math:exp($arg as xs:double?) as xs:double? external;
(:~
 : Returns the value of 10x.
 :
 : If $arg is the empty sequence, the function returns the empty
 : sequence.
 :
 : Otherwise the result is ten raised to the power of $arg, as
 : defined in the IEEE 754-2008 specification of the exp10 function
 : applied to 64-bit binary floating point values.
 :)
declare %a:since("xpath-functions", "3.0-20140408") function math:exp10($arg as xs:double?) as xs:double? external;
(:~
 : Returns the natural logarithm of the argument.
 :
 : If $arg is the empty sequence, the function returns the empty
 : sequence.
 :
 : Otherwise the result is the natural logarithm of $arg, as defined
 : in the IEEE 754-2008 specification of the log function applied to
 : 64-bit binary floating point values.
 :)
declare %a:since("xpath-functions", "3.0-20140408") function math:log($arg as xs:double?) as xs:double? external;
(:~
 : Returns the base-ten logarithm of the argument.
 :
 : If $arg is the empty sequence, the function returns the empty
 : sequence.
 :
 : Otherwise the result is the base-10 logarithm of $arg, as defined
 : in the IEEE 754-2008 specification of the log10 function applied
 : to 64-bit binary floating point values.
 :)
declare %a:since("xpath-functions", "3.0-20140408") function math:log10($arg as xs:double?) as xs:double? external;
(:~
 : Returns an approximation to the mathematical constant π.
 :
 : This function returns the xs:double value whose lexical
 : representation is 3.141592653589793e0.
 :)
declare %a:since("xpath-functions", "3.0-20140408") function math:pi() as xs:double external;
(:~
 : Returns the value of xy.
 :
 : If $x is the empty sequence, the function returns the empty
 : sequence.
 :
 : If $y is an instance of xs:integer, the result is
 : $x raised to the power of $y as defined in the IEEE
 : 754-2008 specification of the pown function applied to a 64-bit
 : binary floating point value and an integer.
 :
 : Otherwise $y is converted to an xs:double by
 : numeric promotion, and the result is the value of $x raised to
 : the power of $y as defined in the IEEE 754-2008 specification
 : of the pow function applied to two 64-bit binary floating point
 : values.
 :)
declare %a:since("xpath-functions", "3.0-20140408") function math:pow($x as xs:double?, $y as xs:numeric) as xs:double? external;
(:~
 : Returns the sine of the argument. The argument is an angle in radians.
 :
 : If $θ is the empty sequence, the function returns the empty
 : sequence.
 :
 : Otherwise the result is the sine of $θ (which is treated as
 : an angle in radians) as defined in the IEEE 754-2008 specification of the
 : sin function applied to 64-bit binary floating point values.
 :
 :
Notes

 :
 : If $θ is positive or negative zero, the result is $θ.
 :
 : If $θ is positive or negative infinity, or NaN,
 : then the result is NaN.
 :
 : Otherwise the result is always in the range -1.0e0 to +1.0e0.
 :)
declare %a:since("xpath-functions", "3.0-20140408") function math:sin($θ as xs:double?) as xs:double? external;
(:~
 : Returns the non-negative square root of the argument.
 :
 : If $arg is the empty sequence, the function returns the empty
 : sequence.
 :
 : Otherwise the result is the mathematical non-negative square root of
 : $arg as defined in the IEEE 754-2008 specification of the
 : squareRoot function applied to 64-bit binary floating point
 : values.
 :
 :
Notes

 :
 : If $arg is positive or negative zero, positive infinity, or
 : NaN, then the result is $arg. (Negative zero is
 : the only case where the result can have negative sign.)
 :
 : If $arg is negative infinity, then the result is NaN.
 :)
declare %a:since("xpath-functions", "3.0-20140408") function math:sqrt($arg as xs:double?) as xs:double? external;
(:~
 : Returns the tangent of the argument. The argument is an angle in radians.
 :
 : If $θ is the empty sequence, the function returns the empty
 : sequence.
 :
 : Otherwise the result is the tangent of $θ (which is treated as
 : an angle in radians) as defined in the IEEE 754-2008 specification of the
 : tan function applied to 64-bit binary floating point values.
 :
 :
Notes

 :
 : If $θ is positive or negative zero, the result is $θ.
 :
 : If $θ is positive or negative infinity, or NaN,
 : then the result is NaN.
 :)
declare %a:since("xpath-functions", "3.0-20140408") function math:tan($θ as xs:double?) as xs:double? external;


[ignore] handle SVN exceptions caused by network timeout.

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

xquery version "1.0";

(:
    This XQuery can be registered with the scheduler to poll
    revision log entries from a given SVN repository. The log
    entries are stored into an XML document. Whenever new revisions
    are available, they are appended to the existing document.

    For an example configuration (to be added to conf.xml) see below:

    <job type="user" class="/db/svn/svntask.xql"
        cron-trigger="40 * * * * ?">
        <parameter name="bindingPrefix" value="svnu"/>
        <parameter name="collection" value="/db/svn"/>
        <parameter name="adminPasswd" value=""/>
        <parameter name="uri" value="https://exist.svn.sourceforge.net/svnroot/exist/trunk/eXist"/>
        <parameter name="startRevision" value="7233"/>
    </job>
:)

declare namespace svnu="http://exist-db.org/svnutil";

import module namespace svn="http://exist-db.org/xquery/svn"
at "java:org.exist.xquery.modules.svn.SVNModule";
import module namespace xdb="http://exist-db.org/xquery/xmldb";

declare variable $svnu:collection external;
declare variable $svnu:adminPasswd external;
declare variable $svnu:uri external;
declare variable $svnu:startRevision external;

declare variable $svnu:USER := "anonymous";
declare variable $svnu:PASS := "anonymous";

declare function svnu:update() {
    let $log := /log[@uri = $svnu:uri][@start = $svnu:startRevision]
    return
        util:catch("java.lang.Exception",
            if ($log) then
                let $lastRev := max(for $rev in $log/entry/@rev return xs:integer($rev))
                let $updated := svn:log(xs:anyURI($svnu:uri), $svnu:USER, $svnu:PASS, $lastRev, ())
                let $entries := $updated/entry[@rev != $lastRev]
                return
                    if (count($entries) gt 0) then
                        update insert $entries into $log
                    else
                        ()
            else
                let $all :=
                    svn:log(xs:anyURI($svnu:uri), $svnu:USER, $svnu:PASS,
                        xs:integer($svnu:startRevision), 7250)
                let $l := util:log("DEBUG", $all)
                return
                    xdb:store($svnu:collection, (), $all),
            ()
        )
};

xdb:login($svnu:collection, "admin", $svnu:PASS),
svnu:update()

xquery version "1.0";

(:
    This XQuery can be registered with the scheduler to poll
    revision log entries from a given SVN repository. The log
    entries are stored into an XML document. Whenever new revisions
    are available, they are appended to the existing document.

    For an example configuration (to be added to conf.xml) see below:

    <job type="user" class="/db/svn/svntask.xql"
        cron-trigger="40 * * * * ?">
        <parameter name="bindingPrefix" value="svnu"/>
        <parameter name="collection" value="/db/svn"/>
        <parameter name="adminPasswd" value=""/>
        <parameter name="uri" value="https://exist.svn.sourceforge.net/svnroot/exist/trunk/eXist"/>
        <parameter name="startRevision" value="7233"/>
    </job>
:)

declare namespace svnu="http://exist-db.org/svnutil";

import module namespace svn="http://exist-db.org/xquery/svn"
at "java:org.exist.xquery.modules.svn.SVNModule";
import module namespace xdb="http://exist-db.org/xquery/xmldb";

declare variable $svnu:collection external;
declare variable $svnu:adminPasswd external;
declare variable $svnu:uri external;
declare variable $svnu:startRevision external;

declare variable $svnu:USER := "anonymous";
declare variable $svnu:PASS := "anonymous";

declare function svnu:update() {
    let $log := /log[@uri = $svnu:uri][@start = $svnu:startRevision]
    return
        if ($log) then
            let $lastRev := max(for $rev in $log/entry/@rev return xs:integer($rev))
            let $updated := svn:log(xs:anyURI($svnu:uri), $svnu:USER, $svnu:PASS, $lastRev + 1, ())
            let $l := util:log("DEBUG", $updated)
            return
                update insert $updated/entry into $log
        else
            let $all :=
                svn:log(xs:anyURI($svnu:uri), $svnu:USER, $svnu:PASS,
                    xs:integer($svnu:startRevision), 7250)
            let $l := util:log("DEBUG", $all)
            return
                xdb:store($svnu:collection, (), $all)
};

xdb:login($svnu:collection, "admin", $svnu:PASS),
svnu:update()