fixed issue with svs loading from file

fixed issue with svs loading from file

(:~
: This is the Care Services Discovery stored query registry
: @version 1.0
: @see https://github.com/openhie/openinfoman
:
:)
module namespace svs_lsvs = "https://github.com/openhie/openinfoman/svs_lsvs";
import module namespace file = "http://expath.org/ns/file";
import module namespace functx = "http://www.functx.com";
declare namespace svs = "urn:ihe:iti:svs:2008";

(:WARNING: ValueSet have @id while DesribedValueSet has @ID:)

declare variable $svs_lsvs:vers := "1.1";
declare variable $svs_lsvs:base_path := concat(file:current-dir() ,'../resources/shared_value_sets/');
declare variable $svs_lsvs:valuesets_doc := "DescribedSharedValueSets.xml";
declare variable $svs_lsvs:sample_value_set_list := svs_lsvs:get_sample_value_set_list();
declare variable $svs_lsvs:blank_valuesets :=  <DescribedValueSets version="{$svs_lsvs:vers}"/>;

declare updating function svs_lsvs:init_store($db) {
  if (not(db:is-xml($db,$svs_lsvs:valuesets_doc))) then
    db:add($db, $svs_lsvs:blank_valuesets,$svs_lsvs:valuesets_doc)
  else
    ()
};

declare function svs_lsvs:store_exists($db) {
  db:is-xml($db,$svs_lsvs:valuesets_doc)
};

declare function svs_lsvs:sample_sets() {
  for $file in file:list($svs_lsvs:base_path,true(),'*.xml')
  return file:resolve-path(concat($svs_lsvs:base_path,"/",$file))
};

declare function svs_lsvs:get_sample_value_set_list() {
<DescribedValueSets version="{$svs_lsvs:vers}">
  {
  for $file in svs_lsvs:sample_sets()
  let $value_set_doc := doc($file)
  let $id :=text{$value_set_doc/svs:ValueSet/@id} (:ValueSet have @id while DesribedValueSet has @ID:)
  let $displayName :=text{$value_set_doc/svs:ValueSet/@displayName}
  let $version :=text{$value_set_doc/svs:ValueSet/@version}
  return
    if ($id)
      then
      <svs:DescribedValueSet ID="{$id}" file="{$file}" displayName="{$displayName}" version="{$version}"/>
    else (<noID>{$file}</noID>)
  }
</DescribedValueSets>
};

declare function svs_lsvs:get_all_value_set_list($db) {
  let $vsets := db:open($db,$svs_lsvs:valuesets_doc)
  let $existing_ids := distinct-values($vsets//svs:DescribedValueSet/@ID)
  let $available_ids := $svs_lsvs:sample_value_set_list//svs:DescribedValueSet/@ID
  return <DescribedValueSets version="{$svs_lsvs:vers}">
  {
    (
      for $id in $existing_ids
      return if ($id = $available_ids)
	then   $svs_lsvs:sample_value_set_list/svs:DescribedValueSet[@ID=$id]
      else  <svs:DescribedValueSet
	ID="{$id}"
	displayName="{$vsets//svs:DescribedValueSet[@ID = $id]/@displayName}"
	version="{$vsets//svs:DescribedValueSet[@ID = $id]/@version}"/>
      ,
      for $id in $available_ids
      where not($id = $existing_ids)
      return    $svs_lsvs:sample_value_set_list/svs:DescribedValueSet[@ID=$id]
      )
  }
  </DescribedValueSets>
};

declare function svs_lsvs:get_document_source($id) {
  text{$svs_lsvs:sample_value_set_list/svs:DescribedValueSet[@ID=$id]/@file}
};

declare function svs_lsvs:exists($db,$id) {
  svs_lsvs:store_exists($db)
  and
  exists(db:open($db,$svs_lsvs:valuesets_doc)//svs:DescribedValueSet[@ID = $id])
};

declare updating function svs_lsvs:load($db,$id) {
  let $doc_source := svs_lsvs:get_document_source($id)
  return if (not($doc_source))
    then  ()
  else
    let $svs_sets := db:open($db,$svs_lsvs:valuesets_doc)/DescribedValueSets
    let $svs := parse-xml(file:read-text($doc_source))
    for $value_set in $svs//svs:ValueSet
    return svs_lsvs:insert($db,$value_set)
};

declare updating function svs_lsvs:insert($db,$value_set) {
  let $vsets := db:open($db,$svs_lsvs:valuesets_doc)/DescribedValueSets
  let $id := $value_set/@id
  let $version := $value_set/@version
  let $existing :=  $vsets//svs:DescribedValueSet[@ID = $id and @version = $version]
  let $described_val_set :=
    <svs:DescribedValueSet ID="{$id}" displayName="{$value_set/@displayName}" >
      {if ($version) then $version else ()}
      {$value_set/*}
    </svs:DescribedValueSet>
  return
        if (exists($existing)) then
	  (
	    for $node in $existing return  delete node $node,
	    insert node $described_val_set into $vsets
	  )
      else
	insert node $described_val_set into $vsets
};

declare updating function svs_lsvs:reload($db,$id) {
  svs_lsvs:load($db,$id)
};

declare function svs_lsvs:get_multiple_described_value_sets($db,$filter) {
  let $dvs0 := db:open($db,$svs_lsvs:valuesets_doc)//svs:DescribedValueSet
  let $dvs1 := if (not($filter/@ID = '')) then $dvs0[@ID = $filter/@ID] else $dvs0
  let $dvs2 := if (not($filter/@DisplayNameContains = '')) then $dvs1[contains(@displayName,$filter/@DisplayNameContains)] else $dvs1
  let $dvs3 := if (not($filter/@SourceContains = '')) then $dvs2[contains(./svs:Source,$filter/@SourceContains)] else $dvs2
  let $dvs4 := if (not($filter/@PurposeContains = '')) then $dvs3[contains(./svs:Purpose,$filter/@PurposeContains)] else $dvs3
  let $dvs5 := if (not($filter/@DefinitionContains = '')) then $dvs4[contains(./svs:Definition,$filter/@DefinitionContains)] else $dvs4
  let $dvs6 := if (not($filter/@GroupContains = '')) then $dvs5[contains(./svs:Group,$filter/@GroupContains)] else $dvs5
  let $dvs7 := if (not($filter/@GroupOID = '')) then $dvs6[./svs:Group = $filter/@GroupOID] else $dvs6
  let $dvs8 := if (not($filter/@EffectiveDateBefore = '')) then $dvs7[./svs:EffectiveDate < @EffectiveDateBefore] else $dvs7
  let $dvs9 := if (not($filter/@EffectiveDateAfter = '')) then $dvs8[./svs:EffectiveDate > @EffectiveDateAfter] else $dvs8
  let $dvs10 := if (not($filter/@ExpirationDateBefore = '')) then $dvs9[./svs:ExpirationDate < @ExpirationDateBefore] else $dvs9
  let $dvs11 := if (not($filter/@ExpirationDateAfter = '')) then $dvs10[./svs:ExpirationDate > @ExpirationDateAfter] else $dvs10
  let $dvs12 := if (not($filter/@CreationDateBefore = '')) then $dvs11[./svs:CreationDate < @CreationDateBefore] else $dvs11
  let $dvs13 := if (not($filter/@CreationDateAfter = '')) then $dvs12[./svs:CreationDate > @CreationDateAfter] else $dvs12
  let $dvs14 := if (not($filter/@RevisionDateBefore = '')) then $dvs13[./svs:RevisionDate < @RevisionDateBefore] else $dvs13
  let $dvs15 := if (not($filter/@RevisionDateAfter = '')) then $dvs14[./svs:RevisionDate > @RevisionDateAfter] else $dvs14

  return  <svs:RetrieveMultipleValueSetsResponse >{$dvs15} </svs:RetrieveMultipleValueSetsResponse>

};

declare function svs_lsvs:get_single_version_value_set($db,$id) {
  svs_lsvs:get_single_version_value_set($db,$id,false())
};

declare function svs_lsvs:get_single_version_value_set($db,$id,$version) {
  svs_lsvs:get_single_version_value_set($db,$id,$version,false)
};

declare function svs_lsvs:get_single_version_value_set($db,$id, $version,$lang) {
  let $vs0 := db:open($db,$svs_lsvs:valuesets_doc)//svs:DescribedValueSet[@ID=$id]
  let $vers := if ($version)
     then $version
     else   functx:max-string ($vs0[@version != '']/@version)  (:NEEDS TO CHANGE:)
  let $vs1:= $vs0[@version = $vers]

  let $concept_lists :=
    if ($lang )
      then
      $vs1/svs:ConceptList[@xml:lang = $lang]
    else
      $vs1/svs:ConceptList

  return
  <svs:RetrieveValueSetResponse version="{$vers}" id="{$id}">
    {
      for $concept_list in $concept_lists
      let $val_set := $concept_list/..
      return
	(:ValueSet have @id while DesribedValueSet has @ID:)
	<svs:ValueSet id="{$val_set/@ID}" displayName="{$val_set/@displayName}" version="{$val_set/@version}">
	  {$concept_list}
	</svs:ValueSet>

    }
  </svs:RetrieveValueSetResponse>
};

declare function svs_lsvs:lookup_code($db,$code,$codeSystem) {
  svs_lsvs:lookup_code($db,$code,$codeSystem, false)
};

declare function svs_lsvs:lookup_code($db,$code,$codeSystem, $lang) {
  let $vs0 := db:open($db,$svs_lsvs:valuesets_doc)//svs:DescribedValueSet

  let $concept_lists :=
    if ($lang)
      then
      $vs0/svs:ConceptList[@xml:lang = $lang]
    else
      $vs0/svs:ConceptList

  let $concept :=  ($concept_lists/svs:Concept[@code = $code and @codeSystem = $codeSystem])[1]
  return string($concept/@displayName)

};

declare function svs_lsvs:get_single_version_code($db,$code,$id) {
  svs_lsvs:get_single_version_code($db,$code,$id, false())
};

declare function svs_lsvs:get_single_version_code($db,$code,$id, $version) {
  svs_lsvs:get_single_version_code($db,$code,$id, $version,false())
};

declare function svs_lsvs:get_single_version_code($db,$code,$id, $version,$lang) {
  let $vs0 := db:open($db,$svs_lsvs:valuesets_doc)//svs:DescribedValueSet[@ID=$id]
  let $vers := if (not($version = '')) then $version else max(xs:int($vs0/@version))
  let $vs1:= $vs0[@version = $vers]

  let $concept_lists :=
    if ($lang)
      then
      $vs1/svs:ConceptList[@xml:lang = $lang]
    else
      $vs1/svs:ConceptList

  let $concepts :=  $concept_lists/svs:Concept[@code = $code]

  return <RetrieveValueSetCodeResponse> (:Not to SVS standard :)
  {
    for $concept in $concepts
    let $concept_list := $concept/..
    let $val_set := $concept_list/..
    return
      (:ValueSet have @id while DesribedValueSet has @ID:)
    <svs:ValueSet id="{$val_set/@ID}" displayName="{$val_set/@displayName}" version="{$val_set/@version}">
      <svs:ConceptList xml:lang="$concept_list/@xml:lang">
	{$concept}
      </svs:ConceptList>
    </svs:ValueSet>
  }
  </RetrieveValueSetCodeResponse>
};

(:~
: This is the Care Services Discovery stored query registry
: @version 1.0
: @see https://github.com/openhie/openinfoman
:
:)
module namespace svs_lsvs = "https://github.com/openhie/openinfoman/svs_lsvs";
import module namespace file = "http://expath.org/ns/file";
import module namespace functx = "http://www.functx.com";
declare namespace svs = "urn:ihe:iti:svs:2008";

(:WARNING: ValueSet have @id while DesribedValueSet has @ID:)

declare variable $svs_lsvs:vers := "1.1";
declare variable $svs_lsvs:base_path := concat(file:current-dir() ,'../resources/shared_value_sets/');
declare variable $svs_lsvs:valuesets_doc := "DescribedSharedValueSets.xml";
declare variable $svs_lsvs:sample_value_set_list := svs_lsvs:get_sample_value_set_list();
declare variable $svs_lsvs:blank_valuesets :=  <DescribedValueSets version="{$svs_lsvs:vers}"/>;

declare updating function svs_lsvs:init_store($db) {
  if (not(db:is-xml($db,$svs_lsvs:valuesets_doc))) then
    db:add($db, $svs_lsvs:blank_valuesets,$svs_lsvs:valuesets_doc)
  else
    ()
};

declare function svs_lsvs:store_exists($db) {
  db:is-xml($db,$svs_lsvs:valuesets_doc)
};

declare function svs_lsvs:sample_sets() {
  for $file in file:list($svs_lsvs:base_path,true(),'*.xml')
  return file:resolve-path(concat($svs_lsvs:base_path,"/",$file))
};

declare function svs_lsvs:get_sample_value_set_list() {
<DescribedValueSets version="{$svs_lsvs:vers}">
  {
  for $file in svs_lsvs:sample_sets()
  let $value_set_doc := doc($file)
  let $id :=text{$value_set_doc/svs:ValueSet/@id} (:ValueSet have @id while DesribedValueSet has @ID:)
  let $displayName :=text{$value_set_doc/svs:ValueSet/@displayName}
  let $version :=text{$value_set_doc/svs:ValueSet/@version}
  return
    if ($id)
      then
      <svs:DescribedValueSet ID="{$id}" file="{$file}" displayName="{$displayName}" version="{$version}"/>
    else (<noID>{$file}</noID>)
  }
</DescribedValueSets>
};

declare function svs_lsvs:get_all_value_set_list($db) {
  let $vsets := db:open($db,$svs_lsvs:valuesets_doc)
  let $existing_ids := distinct-values($vsets//svs:DescribedValueSet/@ID)
  let $available_ids := $svs_lsvs:sample_value_set_list//svs:DescribedValueSet/@ID
  return <DescribedValueSets version="{$svs_lsvs:vers}">
  {
    (
      for $id in $existing_ids
      return if ($id = $available_ids)
	then   $svs_lsvs:sample_value_set_list/svs:DescribedValueSet[@ID=$id]
      else  <svs:DescribedValueSet
	ID="{$id}"
	displayName="{$vsets//svs:DescribedValueSet[@ID = $id]/@displayName}"
	version="{$vsets//svs:DescribedValueSet[@ID = $id]/@version}"/>
      ,
      for $id in $available_ids
      where not($id = $existing_ids)
      return    $svs_lsvs:sample_value_set_list/svs:DescribedValueSet[@ID=$id]
      )
  }
  </DescribedValueSets>
};

declare function svs_lsvs:get_document_source($id) {
  text{$svs_lsvs:sample_value_set_list/svs:DescribedValueSet[@ID=$id]/@file}
};

declare function svs_lsvs:exists($db,$id) {
  svs_lsvs:store_exists($db)
  and
  exists(db:open($db,$svs_lsvs:valuesets_doc)//svs:DescribedValueSet[@ID = $id])
};

declare updating function svs_lsvs:load($db,$id) {
  if (not(svs_lsvs:exists($db,$id)))  then
    let $doc_source := svs_lsvs:get_document_source($id)
    return if (not($doc_source))
      then  ()
    else
      let $svs_sets := db:open($db,$svs_lsvs:valuesets_doc)/DescribedValueSets
      let $svs := parse-xml(file:read-text($doc_source))
      for $value_set in $svs//svs:ValueSet
      return svs_lsvs:insert($db,$value_set)
  else
    ()
};

declare updating function svs_lsvs:insert($db,$value_set) {
  let $vsets := db:open($db,$svs_lsvs:valuesets_doc)
  let $id := $value_set/@id
  let $version := $value_set/@version
  let $existing :=  $vsets//svs:DescribedValueSet[@ID = $id and @version = $version]
  let $described_val_set :=
    <svs:DescribedValueSet ID="{$id}" displayName="{$value_set/@displayName}" version="{$version}">
      {$value_set/*}
    </svs:DescribedValueSet>
  return
        if (exists($existing)) then
	  (
	    for $node in $existing return  delete node $node,
	    insert node $described_val_set into $vsets
	  )
      else
	insert node $described_val_set into $vsets
};

declare updating function svs_lsvs:reload($db,$id) {
  svs_lsvs:load($db,$id)
};

declare function svs_lsvs:get_multiple_described_value_sets($db,$filter) {
  let $dvs0 := db:open($db,$svs_lsvs:valuesets_doc)//svs:DescribedValueSet
  let $dvs1 := if (not($filter/@ID = '')) then $dvs0[@ID = $filter/@ID] else $dvs0
  let $dvs2 := if (not($filter/@DisplayNameContains = '')) then $dvs1[contains(@displayName,$filter/@DisplayNameContains)] else $dvs1
  let $dvs3 := if (not($filter/@SourceContains = '')) then $dvs2[contains(./svs:Source,$filter/@SourceContains)] else $dvs2
  let $dvs4 := if (not($filter/@PurposeContains = '')) then $dvs3[contains(./svs:Purpose,$filter/@PurposeContains)] else $dvs3
  let $dvs5 := if (not($filter/@DefinitionContains = '')) then $dvs4[contains(./svs:Definition,$filter/@DefinitionContains)] else $dvs4
  let $dvs6 := if (not($filter/@GroupContains = '')) then $dvs5[contains(./svs:Group,$filter/@GroupContains)] else $dvs5
  let $dvs7 := if (not($filter/@GroupOID = '')) then $dvs6[./svs:Group = $filter/@GroupOID] else $dvs6
  let $dvs8 := if (not($filter/@EffectiveDateBefore = '')) then $dvs7[./svs:EffectiveDate < @EffectiveDateBefore] else $dvs7
  let $dvs9 := if (not($filter/@EffectiveDateAfter = '')) then $dvs8[./svs:EffectiveDate > @EffectiveDateAfter] else $dvs8
  let $dvs10 := if (not($filter/@ExpirationDateBefore = '')) then $dvs9[./svs:ExpirationDate < @ExpirationDateBefore] else $dvs9
  let $dvs11 := if (not($filter/@ExpirationDateAfter = '')) then $dvs10[./svs:ExpirationDate > @ExpirationDateAfter] else $dvs10
  let $dvs12 := if (not($filter/@CreationDateBefore = '')) then $dvs11[./svs:CreationDate < @CreationDateBefore] else $dvs11
  let $dvs13 := if (not($filter/@CreationDateAfter = '')) then $dvs12[./svs:CreationDate > @CreationDateAfter] else $dvs12
  let $dvs14 := if (not($filter/@RevisionDateBefore = '')) then $dvs13[./svs:RevisionDate < @RevisionDateBefore] else $dvs13
  let $dvs15 := if (not($filter/@RevisionDateAfter = '')) then $dvs14[./svs:RevisionDate > @RevisionDateAfter] else $dvs14

  return  <svs:RetrieveMultipleValueSetsResponse >{$dvs15} </svs:RetrieveMultipleValueSetsResponse>

};

declare function svs_lsvs:get_single_version_value_set($db,$id) {
  svs_lsvs:get_single_version_value_set($db,$id,false())
};

declare function svs_lsvs:get_single_version_value_set($db,$id,$version) {
  svs_lsvs:get_single_version_value_set($db,$id,$version,false)
};

declare function svs_lsvs:get_single_version_value_set($db,$id, $version,$lang) {
  let $vs0 := db:open($db,$svs_lsvs:valuesets_doc)//svs:DescribedValueSet[@ID=$id]
  let $vers := if ($version)
     then $version
     else   functx:max-string ($vs0/@version)  (:NEEDS TO CHANGE:)
  let $vs1:= $vs0[@version = $vers]

  let $concept_lists :=
    if ($lang )
      then
      $vs1/svs:ConceptList[@xml:lang = $lang]
    else
      $vs1/svs:ConceptList

  return
  <svs:RetrieveValueSetResponse version="{$vers}" id="{$id}">
    {
      for $concept_list in $concept_lists
      let $val_set := $concept_list/..
      return
	(:ValueSet have @id while DesribedValueSet has @ID:)
	<svs:ValueSet id="{$val_set/@ID}" displayName="{$val_set/@displayName}" version="{$val_set/@version}">
	  {$concept_list}
	</svs:ValueSet>

    }
  </svs:RetrieveValueSetResponse>
};

declare function svs_lsvs:lookup_code($db,$code,$codeSystem) {
  svs_lsvs:lookup_code($db,$code,$codeSystem, false)
};

declare function svs_lsvs:lookup_code($db,$code,$codeSystem, $lang) {
  let $vs0 := db:open($db,$svs_lsvs:valuesets_doc)//svs:DescribedValueSet

  let $concept_lists :=
    if ($lang)
      then
      $vs0/svs:ConceptList[@xml:lang = $lang]
    else
      $vs0/svs:ConceptList

  let $concept :=  ($concept_lists/svs:Concept[@code = $code and @codeSystem = $codeSystem])[1]
  return string($concept/@displayName)

};

declare function svs_lsvs:get_single_version_code($db,$code,$id) {
  svs_lsvs:get_single_version_code($db,$code,$id, false())
};

declare function svs_lsvs:get_single_version_code($db,$code,$id, $version) {
  svs_lsvs:get_single_version_code($db,$code,$id, $version,false())
};

declare function svs_lsvs:get_single_version_code($db,$code,$id, $version,$lang) {
  let $vs0 := db:open($db,$svs_lsvs:valuesets_doc)//svs:DescribedValueSet[@ID=$id]
  let $vers := if (not($version = '')) then $version else max(xs:int($vs0/@version))
  let $vs1:= $vs0[@version = $vers]

  let $concept_lists :=
    if ($lang)
      then
      $vs1/svs:ConceptList[@xml:lang = $lang]
    else
      $vs1/svs:ConceptList

  let $concepts :=  $concept_lists/svs:Concept[@code = $code]

  return <RetrieveValueSetCodeResponse> (:Not to SVS standard :)
  {
    for $concept in $concepts
    let $concept_list := $concept/..
    let $val_set := $concept_list/..
    return
      (:ValueSet have @id while DesribedValueSet has @ID:)
    <svs:ValueSet id="{$val_set/@ID}" displayName="{$val_set/@displayName}" version="{$val_set/@version}">
      <svs:ConceptList xml:lang="$concept_list/@xml:lang">
	{$concept}
      </svs:ConceptList>
    </svs:ValueSet>
  }
  </RetrieveValueSetCodeResponse>
};


Links to stuff.

Links to stuff.

xquery version "3.0";

(:
 : Copyright 2006-2011 The FLWOR Foundation.
 :
 : Licensed under the Apache License, Version 2.0 (the "License");
 : you may not use this file except in compliance with the License.
 : You may obtain a copy of the License at
 :
 : http://www.apache.org/licenses/LICENSE-2.0
 :
 : Unless required by applicable law or agreed to in writing, software
 : distributed under the License is distributed on an "AS IS" BASIS,
 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 : See the License for the specific language governing permissions and
 : limitations under the License.
 :)

(:~
 : This module provides an XQuery API to full-text functions.
 :
Notes on the thesaurus

 : The ft:thesaurus-lookup function has "levels"
 : and "relationship" parameters.
 : The values for these are implementation-defined.
 : Zorba's default implementation uses the
 : WordNet lexical database,
 : version 3.0.
 :

 : In WordNet,
 : the number of "levels" that two phrases are apart
 : are how many hierarchical meanings apart they are.
 : For example,
 : "canary" is 5 levels away from "vertebrate"
 : (carary > finch > oscine > passerine > bird > vertebrate).
 :

 : When using the WordNet implementation,
 : Zorba supports all of the relationships (and their abbreviations)
 : specified by
 : ISO 2788
 : and
 : ANSI/NISO Z39.19-2005
 : with the exceptions of "HN" (history note)
 : and "X SN" (see scope note for).
 : Additionally, Zorba also supports all WordNet relationships.
 :

 :

 :     Rel.
 :     Meaning
 :     WordNet Rel.
 :

 :

 :     BT
 :     broader term
 :     hypernym
 :

 :

 :     BTG
 :     broader term generic
 :     hypernym
 :

 :

 :     BTI
 :     broader term instance
 :     instance hypernym
 :

 :

 :     BTP
 :     broader term partitive
 :     part meronym
 :

 :

 :     NT
 :     narrower term
 :     hyponym
 :

 :

 :     NTG
 :     narrower term generic
 :     hyponym
 :

 :

 :     NTI
 :     narrower term instance
 :     instance hyponym
 :

 :

 :     NTP
 :     narrower term partitive
 :     part holonym
 :

 :

 :     RT
 :     related term
 :     also see
 :

 :

 :     SN
 :     scope note
 :     n/a
 :

 :

 :     TT
 :     top term
 :     hypernym
 :

 :

 :     UF
 :     non-preferred term
 :     n/a
 :

 :

 :     USE
 :     preferred term
 :     n/a
 :

 :

 : Relationships are case-insensitive.
 :
Notes on tokens

 : The ft:tokenize()
 : and ft:tokenize-string() functions
 : return a sequence of <token> elements
 : containing attributes for each token:
 :

 :

 :      Attribute
 :      Type
 :      Meaning
 :

 :

 :      lang
 :      xs:language
 :      The language of the token.
 :

 :

 :      sentence
 :      xs:nonNegativeInteger
 :      The sentence number.
 :

 :

 :      paragraph
 :      xs:nonNegativeInteger
 :      The paragraph number.
 :

 :

 :      value
 :      xs:string
 :      The token string value.
 :

 :

 :      node-ref
 :      xs:string
 :      A reference to the originating node.
 :

 :

 :
Notes on tokenizer properties

 : The ft:tokenizer-properties() functions
 : return a single <tokenizer-properties> element
 : containing attributes that describe the tokenizer's properties:
 :

 :

 :      Attribute
 :      Type
 :      Meaning
 :

 :

 :      elements-separate-tokens
 :      xs:boolean
 :
 :        If true,
 :        XML elements separate tokens,
 :        for example
 :        <b>B</b>old
 :        would be 2 tokens instead of 1.
 :
 :

 :

 :)
module namespace ft = "http://www.zorba-xquery.com/modules/full-text";

import schema namespace ft-schema =
  "http://www.zorba-xquery.com/modules/full-text";

declare namespace err = "http://www.w3.org/2005/xqt-errors";
declare namespace zerr = "http://www.zorba-xquery.com/errors";

declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
declare option ver:module-version "2.0";

(:~
 : Gets the current
 : language:
 : either the langauge specified by the
 : declare ft-option using language statement (if any)
 : or the one returned by ft:host-lang() (if none).
 :
 : @return said language.
 :)
declare function ft:current-lang()
  as xs:language external;

(:~
 : Gets the host's current
 : language.
 : The "host" is the computer on which Zorba is running.
 : The host's current language is obtained as follows:
 :

 :

 :      For *nix systems:
 :

 :

 :          If setlocale(3) returns non-null,
 :          the language corresponding to that locale is used.
 :

 :

 :          Else, if the LANG environment variable is set,
 :          that language is ued.
 :

 :

 :          Otherwise, there is no default language.
 :

 :

 :

 :

 :      For Windows systems, the language corresponding to the locale returned
 :      by the GetLocaleInfoA() function is used.
 :

 :

 : @return said language.
 :)
declare function ft:host-lang()
  as xs:language external;

(:~
 : Checks whether the given
 : language
 : is supported for stemming.
 :
 : @param $lang The language to check.
 : @return true only if the language is supported.
 :)
declare function ft:is-stem-lang-supported( $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given word is a stop-word.
 :
 : @param $word The word to check.
 : @param $lang The
 : language
 : of $word.
 : @return true only if $word is a stop-word.
 : @error zerr:ZXQP8405 if $lang is not supported for stop-words.
 :)
declare function ft:is-stop-word( $word as xs:string, $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given word is a stop-word.
 :
 : @param $word The word to check.
 : The word's language
 : is assumed to be the one returned by ft:current-lang().
 : @return true only if $word is a stop-word.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 : @error zerr:ZXQP8405 if ft:current-lang() is not supported for
 : stop-words specifically.
 :)
declare function ft:is-stop-word( $word as xs:string )
  as xs:boolean
{
  ft:is-stop-word( $word, ft:current-lang() )
};

(:~
 : Checks whether the given
 : language
 : is supported for stop words.
 :
 : @param $lang The language to check.
 : @return true only if the language is supported.
 :)
declare function ft:is-stop-word-lang-supported( $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given
 : language
 : is supported for look-up using the default thesaurus.
 :
 : @param $lang The language to check.
 : @return true only if the language is supported.
 :)
declare function ft:is-thesaurus-lang-supported( $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given
 : language
 : is supported for look-up using the thesaurus specified by the given URI.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $lang The language to check.
 : @return true only if the language is supported.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 :)
declare function ft:is-thesaurus-lang-supported( $uri as xs:string,
                                                 $lang as xs:language )
  as xs:boolean external;

(:~
 : Stems the given word.
 :
 : @param $word The word to stem.
 : @param $lang The
 : language
 : of $word.
 : @return the stem of $word.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error zerr:ZXQP8404 if $lang is not supported for stemming
 : specifically.
 :)
declare function ft:stem( $word as xs:string, $lang as xs:language )
  as xs:string external;

(:~
 : Stems the given word.
 :
 : @param $word The word to stem.
 : The word's language
 : is assumed to be the one returned by ft:current-lang().
 : @return the stem of $word.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 : @error zerr:ZXQP8404 if ft:current-lang() is not supported for
 : stemming specifically.
 :)
declare function ft:stem( $word as xs:string )
  as xs:string
{
  ft:stem( $word, ft:current-lang() )
};

(:~
 : Strips all diacritical marks from all characters.
 :
 : @param $string The string to strip diacritical marks from.
 : @return $string with diacritical marks stripped.
 :)
declare function ft:strip-diacritics( $string as xs:string )
  as xs:string external;

(:~
 : Looks-up the given phrase in the default thesaurus.
 :
 : @param $phrase The phrase to look up.
 : The phrase's
 : language
 : is assumed to be the one returned by ft:current-lang().
 : @return the original and related phrases.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data.
 : @error zerr:ZXQP8405 if ft:current-lang() is not supported for
 : thesaurus look-up specifically.
 :)
declare function ft:thesaurus-lookup( $phrase as xs:string )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in the thesaurus specified by the given URI.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The
 : language
 : of $phrase.
 : @return the original and related phrases.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if $lang is not supported for thesaurus
 : look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : The phrase's
 : language
 : is assumed to be the one the one returned by ft:current-lang().
 : @return the original and related phrases.
 : @error err:FTST0009 if ft:current-lang() is unsupported in
 : general.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if ft:current-lang() is not supported for
 : thesaurus look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string )
  as xs:string+
{
  ft:thesaurus-lookup( $uri, $phrase, ft:current-lang() )
};

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The
 : language
 : of $phrase.
 : @param $relationship The relationship the results are to have to
 : $phrase.
 : @return the original and related phrases.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if $lang is not supported for thesaurus
 : look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language,
                                      $relationship as xs:string )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The
 : language
 : of $phrase.
 : @param $relationship The relationship the results are to have to
 : $phrase.
 : @param $level-least The minimum number of levels within the thesaurus to be
 : travers$ed.
 : @param $level-most The maximum number of levels within the thesaurus to be
 : traversed.
 : @return the original and related phrases.
 : @error err:FOCA0003 if either $level-least or
 : $level-most is either negative or too large.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if $lang is not supported for thesaurus
 : look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language,
                                      $relationship as xs:string,
                                      $level-least as xs:integer,
                                      $level-most as xs:integer )
  as xs:string+ external;

(:~
 : Tokenizes the given document.
 :
 : @param $doc The document to tokenize.
 : @param $lang The default
 : language
 : of $doc.
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if $lang is not supported in general.
 :)
declare function ft:tokenize( $doc as node(), $lang as xs:language )
  as node()* external;

(:~
 : Tokenizes the given document.
 :
 : @param $doc The XML document to tokenize.
 : The document's default
 : language
 : is assumed to be the one returned by ft:current-lang().
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 :)
declare function ft:tokenize( $doc as node() )
  as node()*
{
  ft:tokenize( $doc, ft:current-lang() )
};

(:~
 : Tokenizes the given string.
 :
 : @param $string The string to tokenize.
 : @param $lang The default
 : language
 : of $string.
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if $lang is not supported in general.
 :)
declare function ft:tokenize-string( $string as xs:string,
                                     $lang as xs:language )
  as xs:string*
{
  () (: TODO :)
};

(:~
 : Tokenizes the given string.
 :
 : @param $string The string to tokenize.
 : The string's default
 : language
 : is assumed to be the one returned by ft:current-lang().
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 :)
declare function ft:tokenize-string( $string as xs:string )
  as xs:string*
{
  ft:tokenize-string( $string, ft:current-lang() )
};

(:~
 : Gets properties of the tokenizer for the given
 : language.
 :
 : @param $lang The langauage of the tokenizer to get the properties of.
 : @return said properties.
 : @error err:FTST0009 if $lang is not supported in general.
 :)
declare function ft:tokenizer-properties( $lang as xs:language )
  as node() external;

(:~
 : Gets properties of the tokenizer for the
 : language
 : returned by ft:current-lang().
 :
 : @return said properties.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 :)
declare function ft:tokenizer-properties()
  as node()
{
  ft:tokenizer-properties( ft:current-lang() )
};

(: vim:set et sw=2 ts=2: :)

xquery version "3.0";

(:
 : Copyright 2006-2011 The FLWOR Foundation.
 :
 : Licensed under the Apache License, Version 2.0 (the "License");
 : you may not use this file except in compliance with the License.
 : You may obtain a copy of the License at
 :
 : http://www.apache.org/licenses/LICENSE-2.0
 :
 : Unless required by applicable law or agreed to in writing, software
 : distributed under the License is distributed on an "AS IS" BASIS,
 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 : See the License for the specific language governing permissions and
 : limitations under the License.
 :)

(:~
 : This module provides an XQuery API to full-text functions.
 :
Notes on the thesaurus

 : The ft:thesaurus-lookup function has "levels"
 : and "relationship" parameters.
 : The values for these are implementation-defined.
 : Zorba's default implementation uses the
 : WordNet lexical database,
 : version 3.0.
 :

 : In WordNet,
 : the number of "levels" that two phrases are apart
 : are how many hierarchical meanings apart they are.
 : For example,
 : "canary" is 5 levels away from "vertebrate"
 : (carary > finch > oscine > passerine > bird > vertebrate).
 :

 : When using the WordNet implementation,
 : Zorba supports all of the relationships (and their abbreviations)
 : specified by ISO 2788 and ANSI/NISO Z39.19-2005
 : with the exceptions of "HN" (history note)
 : and "X SN" (see scope note for).
 : Additionally, Zorba also supports all WordNet relationships.
 :

 :

 :     Rel.
 :     Meaning
 :     WordNet Rel.
 :

 :

 :     BT
 :     broader term
 :     hypernym
 :

 :

 :     BTG
 :     broader term generic
 :     hypernym
 :

 :

 :     BTI
 :     broader term instance
 :     instance hypernym
 :

 :

 :     BTP
 :     broader term partitive
 :     part meronym
 :

 :

 :     NT
 :     narrower term
 :     hyponym
 :

 :

 :     NTG
 :     narrower term generic
 :     hyponym
 :

 :

 :     NTI
 :     narrower term instance
 :     instance hyponym
 :

 :

 :     NTP
 :     narrower term partitive
 :     part holonym
 :

 :

 :     RT
 :     related term
 :     also see
 :

 :

 :     SN
 :     scope note
 :     n/a
 :

 :

 :     TT
 :     top term
 :     hypernym
 :

 :

 :     UF
 :     non-preferred term
 :     n/a
 :

 :

 :     USE
 :     preferred term
 :     n/a
 :

 :

 : Relationships are case-insensitive.
 :
Notes on tokens

 : The ft:tokenize()
 : and ft:tokenize-string() functions
 : return a sequence of <token> elements
 : containing attributes for each token:
 :

 :

 :      Attribute
 :      Type
 :      Meaning
 :

 :

 :      lang
 :      xs:language
 :      The language of the token.
 :

 :

 :      sentence
 :      xs:nonNegativeInteger
 :      The sentence number.
 :

 :

 :      paragraph
 :      xs:nonNegativeInteger
 :      The paragraph number.
 :

 :

 :      value
 :      xs:string
 :      The token string value.
 :

 :

 :      node-ref
 :      xs:string
 :      A reference to the originating node.
 :

 :

 :
Notes on tokenizer properties

 : The ft:tokenizer-properties() functions
 : return a single <tokenizer-properties> element
 : containing attributes that describe the tokenizer's properties:
 :

 :

 :      Attribute
 :      Type
 :      Meaning
 :

 :

 :      elements-separate-tokens
 :      xs:boolean
 :
 :        If true,
 :        XML elements separate tokens,
 :        for example
 :        <b>B</b>old
 :        would be 2 tokens instead of 1.
 :
 :

 :

 :)
module namespace ft = "http://www.zorba-xquery.com/modules/full-text";

import schema namespace ft-schema =
  "http://www.zorba-xquery.com/modules/full-text";

declare namespace err = "http://www.w3.org/2005/xqt-errors";
declare namespace zerr = "http://www.zorba-xquery.com/errors";

declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
declare option ver:module-version "2.0";

(:~
 : Gets the current language: either the langauge specified by the
 : declare ft-option using language statement (if any)
 : or the one returned by ft:host-lang() (if none).
 :
 : @return said language.
 :)
declare function ft:current-lang()
  as xs:language external;

(:~
 : Gets the host's current language.  The "host" is the computer on which Zorba
 : is running.  The host's current language is obtained as follows:
 :

 :

 :      For *nix systems:
 :

 :

 :          If setlocale(3) returns non-null,
 :          the language corresponding to that locale is used.
 :

 :

 :          Else, if the LANG environment variable is set,
 :          that language is ued.
 :

 :

 :          Otherwise, there is no default language.
 :

 :

 :

 :

 :      For Windows systems, the language corresponding to the locale returned
 :      by the GetLocaleInfoA() function is used.
 :

 :

 : @return said language.
 :)
declare function ft:host-lang()
  as xs:language external;

(:~
 : Checks whether the given language is supported for stemming.
 :
 : @param $lang The language to check.
 : @return true only if the language is supported.
 :)
declare function ft:is-stem-lang-supported( $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given word is a stop-word.
 :
 : @param $word The word to check.
 : @param $lang The language of $word.
 : @return true only if $word is a stop-word.
 : @error zerr:ZXQP8405 if $lang is not supported for stop-words.
 :)
declare function ft:is-stop-word( $word as xs:string, $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given word is a stop-word.
 :
 : @param $word The word to check.  The word's language is assumed to be the
 : one returned by ft:current-lang().
 : @return true only if $word is a stop-word.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 : @error zerr:ZXQP8405 if ft:current-lang() is not supported for
 : stop-words specifically.
 :)
declare function ft:is-stop-word( $word as xs:string )
  as xs:boolean
{
  ft:is-stop-word( $word, ft:current-lang() )
};

(:~
 : Checks whether the given language is supported for stop words.
 :
 : @param $lang The language to check.
 : @return true only if the language is supported.
 :)
declare function ft:is-stop-word-lang-supported( $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given language is supported for look-up using the default
 : thesaurus.
 :
 : @param $lang The language to check.
 : @return true only if the language is supported.
 :)
declare function ft:is-thesaurus-lang-supported( $lang as xs:language )
  as xs:boolean external;

(:~
 : Checks whether the given language is supported for look-up using the
 : thesaurus specified by the given URI.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $lang The language to check.
 : @return true only if the language is supported.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 :)
declare function ft:is-thesaurus-lang-supported( $uri as xs:string,
                                                 $lang as xs:language )
  as xs:boolean external;

(:~
 : Stems the given word.
 :
 : @param $word The word to stem.
 : @param $lang The language of $word.
 : @return the stem of $word.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error zerr:ZXQP8404 if $lang is not supported for stemming
 : specifically.
 :)
declare function ft:stem( $word as xs:string, $lang as xs:language )
  as xs:string external;

(:~
 : Stems the given word.
 :
 : @param $word The word to stem.  The word's language is assumed to be the
 : one returned by ft:current-lang().
 : @return the stem of $word.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 : @error zerr:ZXQP8404 if ft:current-lang() is not supported for
 : stemming specifically.
 :)
declare function ft:stem( $word as xs:string )
  as xs:string
{
  ft:stem( $word, ft:current-lang() )
};

(:~
 : Strips all diacritical marks from all characters.
 :
 : @param $string The string to strip diacritical marks from.
 : @return $string with diacritical marks stripped.
 :)
declare function ft:strip-diacritics( $string as xs:string )
  as xs:string external;

(:~
 : Looks-up the given phrase in the default thesaurus.
 :
 : @param $phrase The phrase to look up.  The phrase's language is assumed to
 : be the one returned by ft:current-lang().
 : @return the original and related phrases.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data.
 : @error zerr:ZXQP8405 if ft:current-lang() is not supported for
 : thesaurus look-up specifically.
 :)
declare function ft:thesaurus-lookup( $phrase as xs:string )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in the thesaurus specified by the given URI.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The language of $phrase.
 : @return the original and related phrases.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if $lang is not supported for thesaurus
 : look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.  The phrase's language is assumed to
 : be the one the one returned by ft:current-lang().
 : @return the original and related phrases.
 : @error err:FTST0009 if ft:current-lang() is unsupported in
 : general.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if ft:current-lang() is not supported for
 : thesaurus look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string )
  as xs:string+
{
  ft:thesaurus-lookup( $uri, $phrase, ft:current-lang() )
};

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The language of $phrase.
 : @param $relationship The relationship the results are to have to
 : $phrase.
 : @return the original and related phrases.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if $lang is not supported for thesaurus
 : look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language,
                                      $relationship as xs:string )
  as xs:string+ external;

(:~
 : Looks-up the given phrase in a thesaurus.
 :
 : @param $uri The URI specifying the thesaurus to use.
 : @param $phrase The phrase to look up.
 : @param $lang The language of $phrase.
 : @param $relationship The relationship the results are to have to
 : $phrase.
 : @param $level-least The minimum number of levels within the thesaurus to be
 : travers$ed.
 : @param $level-most The maximum number of levels within the thesaurus to be
 : traversed.
 : @return the original and related phrases.
 : @error err:FOCA0003 if either $level-least or
 : $level-most is either negative or too large.
 : @error err:FTST0018 if $uri refers to a thesaurus
 : that is not found in the statically known thesauri.
 : @error err:FTST0009 if $lang is not supported in general.
 : @error zerr:ZOSE0001 if the thesaurus data file could not be found.
 : @error zerr:ZOSE0002 if the thesaurus data file is not a plain file.
 : @error zerr:ZXQP8401 if the thesaurus data file's version is not supported
 : by the currently running version of Zorba.
 : @error zerr:ZXQP8402 if the thesaurus data file's endianness does not match
 : that of the CPU on which Zorba is currently running.
 : @error zerr:ZXQP8403 if there was an error reading the thesaurus data file.
 : @error zerr:ZXQP8405 if $lang is not supported for thesaurus
 : look-up specifically.
 :)
declare function ft:thesaurus-lookup( $uri as xs:string, $phrase as xs:string,
                                      $lang as xs:language,
                                      $relationship as xs:string,
                                      $level-least as xs:integer,
                                      $level-most as xs:integer )
  as xs:string+ external;

(:~
 : Tokenizes the given document.
 :
 : @param $doc The document to tokenize.
 : @param $lang The default language of $doc.
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if $lang is not supported in general.
 :)
declare function ft:tokenize( $doc as node(), $lang as xs:language )
  as node()* external;

(:~
 : Tokenizes the given document.
 :
 : @param $doc The XML document to tokenize.  The document's default language
 : is assumed to be the one returned by ft:current-lang().
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 :)
declare function ft:tokenize( $doc as node() )
  as node()*
{
  ft:tokenize( $doc, ft:current-lang() )
};

(:~
 : Tokenizes the given string.
 :
 : @param $string The string to tokenize.
 : @param $lang The default language of $string.
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if $lang is not supported in general.
 :)
declare function ft:tokenize-string( $string as xs:string,
                                     $lang as xs:language )
  as xs:string*
{
  () (: TODO :)
};

(:~
 : Tokenizes the given string.
 :
 : @param $string The string to tokenize.  The string's default language is
 : assumed to be the one returned by ft:current-lang().
 : @return a (possibly empty) sequence of tokens.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 :)
declare function ft:tokenize-string( $string as xs:string )
  as xs:string*
{
  ft:tokenize-string( $string, ft:current-lang() )
};

(:~
 : Gets properties of the tokenizer for the given language.
 :
 : @param $lang The langauage of the tokenizer to get the properties of.
 : @return said properties.
 : @error err:FTST0009 if $lang is not supported in general.
 :)
declare function ft:tokenizer-properties( $lang as xs:language )
  as node() external;

(:~
 : Gets properties of the tokenizer for the language returned by
 : ft:current-lang().
 :
 : @return said properties.
 : @error err:FTST0009 if ft:current-lang() is not supported in
 : general.
 :)
declare function ft:tokenizer-properties()
  as node()
{
  ft:tokenizer-properties( ft:current-lang() )
};

(: vim:set et sw=2 ts=2: :)


[refactor] Delete old fn:path() tests.

[refactor] Delete old fn:path() tests.

(:
 : eXist-db Open Source Native XML Database
 : Copyright (C) 2001 The eXist-db Authors
 :
 : info@exist-db.org
 : http://www.exist-db.org
 :
 : This library is free software; you can redistribute it and/or
 : modify it under the terms of the GNU Lesser General Public
 : License as published by the Free Software Foundation; either
 : version 2.1 of the License, or (at your option) any later version.
 :
 : This library is distributed in the hope that it will be useful,
 : but WITHOUT ANY WARRANTY; without even the implied warranty of
 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 : Lesser General Public License for more details.
 :
 : You should have received a copy of the GNU Lesser General Public
 : License along with this library; if not, write to the Free Software
 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 :)
xquery version "3.1";

module namespace fnp="http://exist-db.org/xquery/test/function_sort";

declare namespace test="http://exist-db.org/xquery/xqsuite";

declare
    %test:assertEmpty
function fnp:empty() {
    fn:path()
};


fix invalid picture string

fix invalid picture string

xquery version "3.0";

module namespace fd="http://exist-db.org/xquery/test/format-dates";

declare namespace test="http://exist-db.org/xquery/xqsuite";

declare
    %test:args("2012-06-26")
    %test:assertEquals("26.6.2012")
    %test:args("2012-06-01")
    %test:assertEquals("1.6.2012")
function fd:simple-date($date as xs:date) {
    format-date($date, "[D].[M].[Y]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("26.06.2012")
    %test:args("2012-06-01")
    %test:assertEquals("01.06.2012")
function fd:simple-format($date as xs:date) {
    format-date($date, "[D00].[M01].[Y0]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("26.6.2012")
    %test:args("2012-06-01")
    %test:assertEquals("1.6.2012")
function fd:format-month-numeric-width($date as xs:date) {
    format-date($date, "[D,1-2].[M,*-*].[Y,*]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("26 JUN 2012")
    %test:args("1970-10-07")
    %test:assertEquals("07 OCT 1970")
function fd:format-month-string-width-caps($date as xs:date) {
    format-date($date, "[D01] [MN,*-3] [Y0001]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("26 Jun 2012")
    %test:args("1970-10-07")
    %test:assertEquals("07 Oct 1970")
function fd:format-month-string-width-title($date as xs:date) {
    format-date($date, "[D01] [MNn,*-3] [Y0001]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("Tuesday, June 26, 2012")
    %test:args("1970-10-07")
    %test:assertEquals("Wednesday, October 7, 1970")
function fd:format-names($date as xs:date) {
    format-date($date, "[FNn], [MNn] [D], [Y]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("Tue Jun 26 2012")
    %test:args("1970-10-07")
    %test:assertEquals("Wed Oct 7 1970")
function fd:format-names-width($date as xs:date) {
    format-date($date, "[FNn,*-3] [MNn,*-3] [D] [Y]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("Tuesday, June 26th, 2012")
    %test:args("2012-06-01")
    %test:assertEquals("Friday, June 1st, 2012")
    %test:args("2012-06-02")
    %test:assertEquals("Saturday, June 2nd, 2012")
    %test:args("2012-06-03")
    %test:assertEquals("Sunday, June 3rd, 2012")
function fd:format-ordinal($date as xs:date) {
    format-date($date, "[FNn], [MNn] [D0o], [Y]")
};

declare
    %test:args("2002-12-31")
    %test:assertEquals("[2002-12-31]")
function fd:format-square-brackets($date as xs:date) {
    format-date($date, "[[[Y0001]-[M01]-[D01]]]")
};

declare
    %test:args("2012-06-26T23:14:22.566+02:00")
    %test:assertEquals("11:14 pm on Tuesday, June 26th, 2012")
function fd:format-dateTime($date as xs:dateTime) {
    format-dateTime($date, "[h00]:[m00] [P] on [FNn], [MNn] [D1o], [Y]")
};

declare
    %test:args("2012-06-26+02:00")
    %test:assertEquals("20120626GMT+02:00")
function fd:format-date-z($date as xs:date) {
    format-date($date, "[Y0001][M01][D01][z]")
};

declare
    %test:args("2012-06-26+02:00")
    %test:assertEquals("20120626+02:00")
function fd:format-date-Z($date as xs:date) {
    format-date($date, "[Y0001][M01][D01][Z]")
};

declare
    %test:args("2012-06-26T23:14:22.566+02:00")
    %test:assertEquals("20120626231422GMT+02:00")
function fd:format-dateTime-z($date as xs:dateTime) {
    format-dateTime($date, "[Y0001][M01][D01][H01][m01][s01][z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("GMT-10:00")
function fd:format-dateTime-z-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("GMT-05:00")
function fd:format-dateTime-z-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("GMT+00:00")
function fd:format-dateTime-z-zero($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("GMT+05:30")
function fd:format-dateTime-z-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("GMT+13:00")
function fd:format-dateTime-z-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T23:14:22.566+02:00")
    %test:assertEquals("20120626231422+02:00")
function fd:format-dateTime-Z($date as xs:dateTime) {
    format-dateTime($date, "[Y0001][M01][D01][H01][m01][s01][Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10:00")
function fd:format-dateTime-Z-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-05:00")
function fd:format-dateTime-Z-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+0")
function fd:format-dateTime-Z-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+05:30")
function fd:format-dateTime-Z-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-Z-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z]")
};

declare
    %test:args("2012-06-26T23:14:22.566")
    %test:assertEquals("20120626231422")
function fd:format-dateTime-no-tz-z($date as xs:dateTime) {
    format-dateTime($date, "[Y0001][M01][D01][H01][m01][s01][z]")
};

declare
    %test:args("2012-06-26T23:14:22.566")
    %test:assertEquals("20120626231422")
function fd:format-dateTime-no-tz-Z($date as xs:dateTime) {
    format-dateTime($date, "[Y0001][M01][D01][H01][m01][s01][Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10")
function fd:format-dateTime-Z0-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-5")
function fd:format-dateTime-Z0-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+0")
function fd:format-dateTime-Z0-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+5:30")
function fd:format-dateTime-Z0-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13")
function fd:format-dateTime-Z0-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10:00")
function fd:format-dateTime-Z0-00-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-5:00")
function fd:format-dateTime-Z0-00-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+0:00")
function fd:format-dateTime-Z0-00-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+5:30")
function fd:format-dateTime-Z0-00-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-Z0-00-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10:00")
function fd:format-dateTime-Z00-00-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-05:00")
function fd:format-dateTime-Z00-00-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+00:00")
function fd:format-dateTime-Z00-00-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+05:30")
function fd:format-dateTime-Z00-00-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-Z00-00-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-1000")
function fd:format-dateTime-Z0000-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-0500")
function fd:format-dateTime-Z0000-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+0000")
function fd:format-dateTime-Z0000-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+0530")
function fd:format-dateTime-Z0000-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+1300")
function fd:format-dateTime-Z0000-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10:00")
function fd:format-dateTime-Z00-00t-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-05:00")
function fd:format-dateTime-Z00-00t-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("Z")
function fd:format-dateTime-Z00-00t-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+05:30")
function fd:format-dateTime-Z00-00t-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-Z00-00t-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("W")
function fd:format-dateTime-ZZ-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("R")
function fd:format-dateTime-ZZ-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("Z")
function fd:format-dateTime-ZZ-zero($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+05:30")
function fd:format-dateTime-ZZ-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-ZZ-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("HST")
function fd:format-dateTime-ZN-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("EST")
function fd:format-dateTime-ZN-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("GMT")
function fd:format-dateTime-ZN-zero($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("IST")
function fd:format-dateTime-ZN-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-ZN-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("06:00 EST")
function fd:format-dateTime-ZN-NY-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("12:00 EST")
function fd:format-dateTime-ZN-NY-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("07:00 EST")
function fd:format-dateTime-ZN-NY-zero($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("01:30 EST")
function fd:format-dateTime-ZN-NY-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("18:00 EST")
function fd:format-dateTime-ZN-NY-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:args("17:45:50")
    %test:assertEquals("17:45:50")
    %test:args("09:45:50")
    %test:assertEquals("09:45:50")
function fd:simple-time($time as xs:time) {
    format-time($time, "[H00]:[m00]:[s]")
};

declare
    %test:args("17:45:50")
    %test:assertEquals("05:45 pm")
    %test:args("09:45:50")
    %test:assertEquals("09:45 am")
function fd:time-am-pm($time as xs:time) {
    format-time($time, "[h00]:[m00] [P]")
};

declare
    %test:assertError
function fd:date-format-in-format-time() {
    format-time(current-time(), "[Y]")
};

declare
    %test:assertError
function fd:time-format-in-format-date() {
    format-date(current-dateTime(), "[H]")
};

declare
    %test:args("2012-06-27T10:21:55.082+02:00")
    %test:assertEquals("Mittwoch, 27. Juni 2012")
    %test:args("1970-10-07T10:21:55.082+02:00")
    %test:assertEquals("Mittwoch, 7. Oktober 1970")
function fd:language($date as xs:dateTime) {
    format-date($date, "[FNn], [D1o] [MNn] [Y]", "de", (), ())
};

declare
    %test:args("2012-06-27T10:21:55.082+02:00")
    %test:assertEquals("Среда, 27 Июня 2012")
    %test:args("1970-10-07T10:21:55.082+02:00")
    %test:assertEquals("Среда, 7 Октября 1970")
function fd:language-ru($date as xs:dateTime) {
    format-date($date, "[FNn], [D1o] [MNn] [Y]", "ru", (), ())
};

xquery version "3.0";

module namespace fd="http://exist-db.org/xquery/test/format-dates";

declare namespace test="http://exist-db.org/xquery/xqsuite";

declare
    %test:args("2012-06-26")
    %test:assertEquals("26.6.2012")
    %test:args("2012-06-01")
    %test:assertEquals("1.6.2012")
function fd:simple-date($date as xs:date) {
    format-date($date, "[D].[M].[Y]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("26.06.2012")
    %test:args("2012-06-01")
    %test:assertEquals("01.06.2012")
function fd:simple-format($date as xs:date) {
    format-date($date, "[D00].[M01].[Y0]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("26.6.2012")
    %test:args("2012-06-01")
    %test:assertEquals("1.6.2012")
function fd:format-month-numeric-width($date as xs:date) {
    format-date($date, "[D,1-2].[M,*-*].[Y,*]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("26 JUN 2012")
    %test:args("1970-10-07")
    %test:assertEquals("07 OCT 1970")
function fd:format-month-string-width-caps($date as xs:date) {
    format-date($date, "[D01] [MN,*-3] [Y0001]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("26 Jun 2012")
    %test:args("1970-10-07")
    %test:assertEquals("07 Oct 1970")
function fd:format-month-string-width-title($date as xs:date) {
    format-date($date, "[D01] [MNn,*-3] [Y0001]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("Tuesday, June 26, 2012")
    %test:args("1970-10-07")
    %test:assertEquals("Wednesday, October 7, 1970")
function fd:format-names($date as xs:date) {
    format-date($date, "[FNn], [MNn] [D], [Y]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("Tue Jun 26 2012")
    %test:args("1970-10-07")
    %test:assertEquals("Wed Oct 7 1970")
function fd:format-names-width($date as xs:date) {
    format-date($date, "[FNn,*-3] [MNn,*-3] [D] [Y]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("Tuesday, June 26th, 2012")
    %test:args("2012-06-01")
    %test:assertEquals("Friday, June 1st, 2012")
    %test:args("2012-06-02")
    %test:assertEquals("Saturday, June 2nd, 2012")
    %test:args("2012-06-03")
    %test:assertEquals("Sunday, June 3rd, 2012")
function fd:format-ordinal($date as xs:date) {
    format-date($date, "[FNn], [MNn] [D0o], [Y]")
};

declare
    %test:args("2002-12-31")
    %test:assertEquals("[2002-12-31]")
function fd:format-square-brackets($date as xs:date) {
    format-date($date, "[[[Y0001]-[M01]-[D01]]]")
};

declare
    %test:args("2012-06-26T23:14:22.566+02:00")
    %test:assertEquals("11:14 pm on Tuesday, June 26th, 2012")
function fd:format-dateTime($date as xs:dateTime) {
    format-dateTime($date, "[h00]:[m00] [P] on [FNn], [MNn] [D1o], [Y]")
};

declare
    %test:args("2012-06-26+02:00")
    %test:assertEquals("20120626GMT+02:00")
function fd:format-date-z($date as xs:date) {
    format-date($date, "[Y0001][M01][D01][z]")
};

declare
    %test:args("2012-06-26+02:00")
    %test:assertEquals("20120626+02:00")
function fd:format-date-Z($date as xs:date) {
    format-date($date, "[Y0001][M01][D01][Z]")
};

declare
    %test:args("2012-06-26T23:14:22.566+02:00")
    %test:assertEquals("20120626231422GMT+02:00")
function fd:format-dateTime-z($date as xs:dateTime) {
    format-dateTime($date, "[Y0001][M01][D01][H01][m01][s01][z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("GMT-10:00")
function fd:format-dateTime-z-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("GMT-05:00")
function fd:format-dateTime-z-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("GMT+00:00")
function fd:format-dateTime-z-zero($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("GMT+05:30")
function fd:format-dateTime-z-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("GMT+13:00")
function fd:format-dateTime-z-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T23:14:22.566+02:00")
    %test:assertEquals("20120626231422+02:00")
function fd:format-dateTime-Z($date as xs:dateTime) {
    format-dateTime($date, "[Y0001][M01][D01][H01][m01][s01][Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10:00")
function fd:format-dateTime-Z-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-05:00")
function fd:format-dateTime-Z-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+0")
function fd:format-dateTime-Z-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+05:30")
function fd:format-dateTime-Z-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-Z-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z]")
};

declare
    %test:args("2012-06-26T23:14:22.566")
    %test:assertEquals("20120626231422")
function fd:format-dateTime-no-tz-z($date as xs:dateTime) {
    format-dateTime($date, "[Y0001][M01][D01][H01][m01][s01][z]")
};

declare
    %test:args("2012-06-26T23:14:22.566")
    %test:assertEquals("20120626231422")
function fd:format-dateTime-no-tz-Z($date as xs:dateTime) {
    format-dateTime($date, "[Y0001][M01][D01][H01][m01][s01][Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10")
function fd:format-dateTime-Z0-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-5")
function fd:format-dateTime-Z0-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+0")
function fd:format-dateTime-Z0-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+5:30")
function fd:format-dateTime-Z0-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13")
function fd:format-dateTime-Z0-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10:00")
function fd:format-dateTime-Z0-00-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-5:00")
function fd:format-dateTime-Z0-00-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+0:00")
function fd:format-dateTime-Z0-00-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+5:30")
function fd:format-dateTime-Z0-00-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-Z0-00-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10:00")
function fd:format-dateTime-Z00-00-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-05:00")
function fd:format-dateTime-Z00-00-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+00:00")
function fd:format-dateTime-Z00-00-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+05:30")
function fd:format-dateTime-Z00-00-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-Z00-00-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-1000")
function fd:format-dateTime-Z0000-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-0500")
function fd:format-dateTime-Z0000-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+0000")
function fd:format-dateTime-Z0000-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+0530")
function fd:format-dateTime-Z0000-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+1300")
function fd:format-dateTime-Z0000-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10:00")
function fd:format-dateTime-Z00-00t-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-05:00")
function fd:format-dateTime-Z00-00t-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("Z")
function fd:format-dateTime-Z00-00t-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+05:30")
function fd:format-dateTime-Z00-00t-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-Z00-00t-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("W")
function fd:format-dateTime-ZZ-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("R")
function fd:format-dateTime-ZZ-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("Z")
function fd:format-dateTime-ZZ-zero($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+05:30")
function fd:format-dateTime-ZZ-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-ZZ-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("HST")
function fd:format-dateTime-ZN-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("EST")
function fd:format-dateTime-ZN-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("GMT")
function fd:format-dateTime-ZN-zero($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("IST")
function fd:format-dateTime-ZN-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-ZN-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("06:00 EST")
function fd:format-dateTime-ZN-NY-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("12:00 EST")
function fd:format-dateTime-ZN-NY-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("07:00 EST")
function fd:format-dateTime-ZN-NY-zero($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("01:30 EST")
function fd:format-dateTime-ZN-NY-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("18:00 EST")
function fd:format-dateTime-ZN-NY-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:args("17:45:50")
    %test:assertEquals("17:45:50")
    %test:args("09:45:50")
    %test:assertEquals("09:45:50")
function fd:simple-time($time as xs:time) {
    format-time($time, "[H00]:[m00]:[s]")
};

declare
    %test:args("17:45:50")
    %test:assertEquals("05:45 pm")
    %test:args("09:45:50")
    %test:assertEquals("09:45 am")
function fd:time-am-pm($time as xs:time) {
    format-time($time, "[h00]:[m00] [P]")
};

declare
    %test:assertError
function fd:date-format-in-format-time() {
    format-time(current-time(), "[Y]")
};

declare
    %test:assertError
function fd:time-format-in-format-date() {
    format-date(current-dateTime(), "[H]")
};

declare
    %test:args("2012-06-27T10:21:55.082+02:00")
    %test:assertEquals("Mittwoch, 27. Juni 2012")
    %test:args("1970-10-07T10:21:55.082+02:00")
    %test:assertEquals("Mittwoch, 7. Oktober 1970")
function fd:language($date as xs:dateTime) {
    format-date($date, "[FNn], [D1o] [MNn] [Y]", "de", (), ())
};

declare
    %test:args("2012-06-27T10:21:55.082+02:00")
    %test:assertEquals("Среда, 27 Июня 2012")
    %test:args("1970-10-07T10:21:55.082+02:00")
    %test:assertEquals("Среда, 7 Октября 1970")
function fd:language-ru($date as xs:dateTime) {
    format-date($date, "[FNn], [D1o] [MNn] [Y]", "ru", (), ())
};