module namespace local="http://127.0.0.1/library/simpack";
declare namespace wsdl="http://schemas.xmlsoap.org/wsdl/";
declare namespace xsd="http://www.w3.org/2001/XMLSchema";
declare namespace sawsdl="http://www.w3.org/ns/sawsdl";
declare function local:lev($arg1 as xs:string, $arg2 as xs:string) as xs:integer
{
  if(string-length($arg1) = 0) then string-length($arg2) else
  if(string-length($arg2) = 0) then string-length($arg1) else
  min((
    local:lev(substring($arg1, 2), $arg2)+1,
    local:lev($arg1, substring($arg2, 2))+1,
    local:lev(substring($arg1, 2), substring($arg2, 2))+
     (if(substring($arg1, 1, 1) = substring($arg2, 1, 1)) then 0 else 1)
  ))
};
declare function local:dice($str1 as xs:string, $ str2 as xs:string) as xs:double
{
   if(string-length($str1)<string-length($str2)) then
   (2*sum(for $i in 1 to (string-length($str1)-1) return
   if(contains($str2,substring($str1,$i,2))) then 1 else 0)
   div (string-length($str1)+string-length($str2)-2)) else
   (2*sum(for $i in 1 to (string-length($str2)-1) return
   if(contains($str1,substring($str2,$i,2))) then 1 else 0)
   div (string-length($str1)+string-length($str2)-2))  
};

declare function local:Name($str1 as node()) as xs:string
{
  $str1/wsdl:definitions/wsdl:service/@name
};
declare function local:DescriptionText($ser as node()) as xs:string
{
  $ser/wsdl:definitions/wsdl:documentation
};
declare function local:DescriptionTextAll($ser as node()) as xs:string
{
  string-join($ser/wsdl:definitions//*/wsdl:documentation | $ser/wsdl:definitions/wsdl:documentation | $ser/wsdl:definitions//*/*:annotation ,' ')
};
declare function local:InputMessageName($s as node()) as xs:string
 {
 substring-after($s/wsdl:definitions/wsdl:portType[1]/wsdl:operation/wsdl:input/@message,':')
 };
declare function local:InputMessagePart($s as node()) as node()*
 {
   $s/wsdl:definitions/wsdl:message[@name=local:InputMessageName($s)]/wsdl:part/@name
 };
 
 declare function local:InputConceptsOld($ser as node()) as node()*
 { 
	let $s:= $ser/wsdl:definitions
	let $si:=substring($s/wsdl:portType[1]/wsdl:operation/wsdl:input/@message,5)
	let $sim:=insert-before($s/wsdl:message[@name=$si]/wsdl:part/@type,1,$s/wsdl:message[@name=$si]/wsdl:part/@element)
	return insert-before($s/wsdl:types/xsd:schema/*[@name=substring($sim[1],5) or @name=substring($sim[2],5) or @name=substring($sim[3],5)]//*/@sawsdl:modelReference,1,$s/wsdl:message[@name=$si]/wsdl:part/@sawsdl:modelReference)
};
declare function local:OutputConceptsOld($ser as node()) as node()*
{
    let $s:= $ser/wsdl:definitions	
	let $si:=substring($s/wsdl:portType[1]/wsdl:operation/wsdl:output/@message,5)
	let $sim:=insert-before($s/wsdl:message[@name=$si]//wsdl:part[@type]/@type,1,$s/wsdl:message[@name=$si]//wsdl:part[@type]/@element)
	return insert-before($s/wsdl:types/xsd:schema/*[@name=substring($sim[1],5) or @name=substring($sim[2],5) or @name=substring($sim[3],5)]//*/@sawsdl:modelReference,1,$s/wsdl:message[@name=$si]/wsdl:part/@sawsdl:modelReference)
};
declare function local:temp($ser as node()) as node()*
{
  $ser/wsdl:definitions/wsdl:portType[1]/wsdl:operation/wsdl:input/@message
};

declare function local:inputPart($ser as node()) as node()*
{
	$ser/wsdl:definitions/wsdl:message[@name=substring-after(local:temp($ser),':')]//wsdl:part/@name
};

declare function local:InputConcepts($ser as document-node()) as node()*
{
for $p in $ser/wsdl:definitions/wsdl:message[@name=substring-after($ser/wsdl:definitions/wsdl:portType[1]/wsdl:operation/wsdl:input/@message,':')]//wsdl:part
return 
if(exists($p/@sawsdl:modelReference)) then $p/@sawsdl:modelReference
else 
    if (exists($p/@type)) then $ser/wsdl:definitions/wsdl:types/*:schema//(*:complexType|*:simpleType)[@name=substring-after($p/@type,':')]//@sawsdl:modelReference
    else
    	if (exists($p/@element)) then (
    			if (empty($ser/wsdl:definitions/wsdl:types/*:schema/*:element[@name=substring-after($p/@element,':')]//@sawsdl:modelReference))
	          		then $ser/wsdl:definitions/wsdl:types/*:schema/(*:complexType|*:simpleType)[@name=substring-after($ser/wsdl:definitions/wsdl:types/*:schema/*:element[@name=substring-after($p/@element,':')]/@type,':')]//@sawsdl:modelReference
	          	else $ser/wsdl:definitions/wsdl:types/*:schema/*:element[@name=substring-after($p/@element,':')]//@sawsdl:modelReference )
    	else
   		() 
};
declare function local:OutputConcepts($ser as document-node()) as node()*
{
for $p in $ser/wsdl:definitions/wsdl:message[@name=substring-after($ser/wsdl:definitions/wsdl:portType[1]/wsdl:operation/wsdl:output/@message,':')]//wsdl:part
return 
if(exists($p/@sawsdl:modelReference)) then $p/@sawsdl:modelReference
else 
    if (exists($p/@type)) then $ser/wsdl:definitions/wsdl:types/*:schema//(*:complexType|*:simpleType)[@name=substring-after($p/@type,':')]//@sawsdl:modelReference
    else
    	if (exists($p/@element)) then (
    			if (empty($ser/wsdl:definitions/wsdl:types/*:schema/*:element[@name=substring-after($p/@element,':')]//@sawsdl:modelReference))
	          		then $ser/wsdl:definitions/wsdl:types/*:schema/(*:complexType|*:simpleType)[@name=substring-after($ser/wsdl:definitions/wsdl:types/*:schema/*:element[@name=substring-after($p/@element,':')]/@type,':')]//@sawsdl:modelReference
	          	else $ser/wsdl:definitions/wsdl:types/*:schema/*:element[@name=substring-after($p/@element,':')]//@sawsdl:modelReference )
    	else
   		() 
};
declare function local:NamesinSchema($ser as document-node()) as node()*
{
    $ser/wsdl:definitions/wsdl:types/*:schema//*/@name
};