How can database updating be done inside map:for-each ?
The higher-order functions of the XQuery spec are currently restricted to non-updating functions. You can circumvent this restriction by enabling the MIXUPDATES option, or using traditional iterations (which are, at least, not much slower than map:for-each):
for $key in map:keys($map) let $val := $map($key) return (...)