 <p>Sure! My existdb version is 2.2.</p> <pre><code>xquery version "3.0"; declare boundary-space preserve; declare namespace exist = "http://exist.sourceforge.net/NS/exist"; declare namespace request="http://exist-db.org/xquery/request"; declare namespace xmldb="http://exist-db.org/xquery/xmldb"; declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization"; declare option output:method "text"; declare option output:indent "yes"; import module namespace file="http://exist-db.org/xquery/file"; let $log-in := xmldb:login("/db", "admin", "admin") for $k in (35,36,37) let $AppletName := fn:doc(fn:concat('/db/project/AppletA_',$k,'.xml'))//APPLET/@NAME/string() let $collection := fn:concat('xmldb:exist:/db/project/Scripts_',$k) let $node :=(&lt;APPLET_SCRIPTS&gt;{ for $i in fn:doc(fn:concat('/db/project/AppletA_',$k,'.xml'))//APPLET_SERVER_SCRIPT let $MethodName := $i/@NAME let $MethodScript := string($i/@SCRIPT) let $file-name := fn:concat($MethodName,'.js') let $store := xmldb:store($collection,$file-name, $MethodScript) return &lt;div&gt;{$MethodScript}&lt;/div&gt; }&lt;/APPLET_SCRIPTS&gt;) return $node </code></pre> <p>A fragment of xml is:</p> <p><a href="http://i.stack.imgur.com/nAHKQ.png" rel="nofollow">screenshot xml fragment</a></p> <p>The content of file created is this one line string:</p> <p>function Prova() { try { var sVal = TheApplication().InvokeMethod("LookupValue","FS_ACTIVITY_CLASS","CIM_FAC18"); var recordFound = searchRecord(sVal); if(!recordFound) { this.InvokeMethod("NewRecordCustom"); this.BusComp().SetFieldValue("Class",sVal); this.BusComp().WriteRecord(); } } catch(e) { throw(e); } finally { sVal = null; } }</p>