(:~
 : generate dotml from xqdoc source
 : apb Jan 2013
 :)
declare namespace doc = "http://www.xqdoc.org/1.0";
declare namespace dotml = "http://www.martin-loetzsch.de/DOTML";
declare namespace hash = "http://basex.org/modules/hash";
declare variable $ns-ignore := ("http://www.w3.org/2005/xpath-functions",
"http://www.w3.org/2010/xslt-xquery-serialization",
"http://www.w3.org/2001/XMLSchema",
"http://www.w3.org/2005/xpath-functions/math");
declare function local:sid($s as xs:string)
as xs:string { "A" || xs:hexBinary(hash:md5($s)) };
declare function local:fid($uri as xs:string, $name as xs:string,
                           $arity as xs:string) { let $a :=
    fn:trace(($uri || "*" || $name || "*" || $arity), "fid: ")
return "A" || xs:hexBinary(hash:md5($uri || $name || $arity)) };
declare function local:foo($dml) { 
    let $moduri :=
    $dml/doc:xqdoc/doc:module/doc:uri/fn:string()
return
  
{ for $import in $dml//doc:import
    let $ns := $import/doc:uri/fn:string()
    where some $call in $dml//doc:invoked satisfies $call/doc:uri =
        $import/doc:uri
    return
        
         { for $call in $dml//doc:invoked[doc:uri = $import/doc:uri]
        return
                 
        }
        
}
{ for $f in $dml//doc:function
    return
        ,
    for $v in $dml//doc:variable
    return
        
}
{ for $call in $dml//doc:invoked
    let $f := $call/..
    where not($call/doc:name/@uri/fn:string() = $ns-ignore)
    return
         }
 };
let $a := local:foo(/)
(:
 : let $req:=
 : data={fn:encode-for-uri(fn:serialize($a))}
 : 
 : let $ws:=
 : http:send-request($req,"http://localhost:8984/restxq/graphxq/api/dotml")
 : return file:write("aa.svg",$ws[2])
 :) return $a